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?

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.

This article has been dead for over six months. Start a new discussion instead.