Now, in this particular one, I'm attempting to make a two-dimensional container that stores vectors. Of course, I wanted both dimensions to be dynamic at first, but once convinced that even attempting to do a 2D vector was nearly impossible or required a heap of used memory via the resize() function, I went with an array of vectors.

Now, the problem comes; I made the array of pointers to vectors (only way it could be done, apparently, since I can't make up variable names on the spot using concatenation + variables) just fine. However, I want to return this same array of pointers. Unfortunately, vector<type>[] as a return type didn't exactly work. Any suggestions?

Dani AI

Generated

Short answer: don’t try to return a raw array type like vector<type>[]. In C++ arrays don’t behave like assignable/returnable objects, so returning them is awkward and error‑prone. Use std::vector<std::vector<T>> as your 2‑D container and return that by value (for example, std::vector<std::vector<int>> makeGrid();). Returning a std::vector is efficient in practice because compilers usually elide copies (RVO) and, since C++11, moves avoid unnecessary copying.

If you care about memory layout or performance, be aware of tradeoffs: vector<vector<T>> gives variable row lengths but creates one allocation per row (outer vector + each inner vector). For a contiguous 2‑D block use a single std::vector<T> sized rows*cols and index with data[row*cols + col]. For large or hot loops preallocate with outer.reserve(nrows) and inner.reserve(ncols) (or reserve the single buffer once) to avoid repeated reallocations.

About the compiler errors: correctly pointed out the old > > issue — compilers before C++11 require a space between nested closing angle brackets (> >), while C++11 and later accept >>. DevC++ historically bundled older toolchains, so enabling a newer standard (e.g., set C++11 or later) or inserting the space fixes that. Also double‑check that using directives or std:: qualify are in scope and that an unrelated syntax error earlier in the file isn’t misleading the compiler. ’s example demonstrates construction/iteration; combine that pattern with reserve or a single contiguous buffer depending on your performance needs.

Recommended Answers

All 8 Replies

Why not a vector of vectors (of vectors...)? Your profiling indicated performance requirement issues?

Why not a vector of vectors (of vectors...)? Your profiling indicated performance requirement issues?

I was steered away from dimensional vectors before I could find out the syntax, so how would one create a vector of vectors? I found that trying to specify a vector as the type of the container vector didn't work.

Hmm. Let me try this.

#include <iostream>
#include <vector>

int main()
{
   std::vector<int> row;
   std::vector<std::vector<int> > array2d;
   row.push_back(1);
   row.push_back(2);
   row.push_back(3);
   array2d.push_back(row);
   row.clear();
   row.push_back(4);
   row.push_back(5);
   array2d.push_back(row);
   row.clear();
   row.push_back(6);
   row.push_back(7);
   row.push_back(8);
   row.push_back(9);
   array2d.push_back(row);

   for ( std::vector<std::vector<int> >::iterator it = array2d.begin(); it != array2d.end(); ++it )
   {
      for ( std::vector<int>::iterator jt = it->begin(); jt != it->end(); ++jt )
      {
         std::cout << *jt << ' ';
      }
      std::cout << std::endl;
   }
   return 0;
}

/* my output
1 2 3
4 5
6 7 8 9
*/

Likely someone will later come along and point out issues.

std::vector<std::vector<int> >

Kaza, your problem from before might have been that you didn't have a space between these two right angle brackets. You need a space. Without a space, the angle brackets get interpreted as a '>>' operator.

Hmm. Let me try this.

#include <iostream>
#include <vector>

int main()
{
   std::vector<int> row;
   std::vector<std::vector<int> > array2d;
   row.push_back(1);
   row.push_back(2);
   row.push_back(3);
   array2d.push_back(row);
   row.clear();
   row.push_back(4);
   row.push_back(5);
   array2d.push_back(row);
   row.clear();
   row.push_back(6);
   row.push_back(7);
   row.push_back(8);
   row.push_back(9);
   array2d.push_back(row);

   for ( std::vector<std::vector<int> >::iterator it = array2d.begin(); it != array2d.end(); ++it )
   {
      for ( std::vector<int>::iterator jt = it->begin(); jt != it->end(); ++jt )
      {
         std::cout << *jt << ' ';
      }
      std::cout << std::endl;
   }
   return 0;
}

/* my output
1 2 3
4 5
6 7 8 9
*/

Likely someone will later come along and point out issues.

That's funny... that same syntax had DevC++ yelling at me for no ends yesterday, and yet it goes now. Is the std class identifier preceding the methods/templates that important?

I hope you noticed my other comment.

'std' is a namespace, not a class. When using headers with names like <vector> instead of <vector.h>, you need to use it. You can put 'using namespace std;' in your program to avoid having to write 'std' everywhere. You could also write 'using std::vector;' to throw out the requirement that std:: be used in front of 'vector'. I think the latter method, of importing names one by one, is better, because later, the standard library might be expanded, and you don't want names of new standard library objects to collide with ones you're using. (That's what happens with wanton disuse of the namespaces).

Is the std class identifier preceding the methods/templates that important?

Very much so. Pay close attention to the top part of [post=122322]this[/post], where 3 methods are described. [edit]Or [post=97182]this[/post].

I hope you noticed my other comment.

'std' is a namespace, not a class. When using headers with names like <vector> instead of <vector.h>, you need to use it. You can put 'using namespace std;' in your program to avoid having to write 'std' everywhere. You could also write 'using std::vector;' to throw out the requirement that std:: be used in front of 'vector'. I think the latter method, of importing names one by one, is better, because later, the standard library might be expanded, and you don't want names of new standard library objects to collide with ones you're using. (That's what happens with wanton disuse of the namespaces).

The thing, however, is that using namespace std; was already invoked in the code, so I didn't see the need to do it again - though it worked with the extra namespace declaration. >> was spaced out in the time it didn't work yesterday, so that wasn't the problem. Rather strange. Perhaps it was some other part of the code that I fixed today, though the compiler error pointed at the vector in vector declaration pretty clearly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.