vector of vector syntax + understanding header files

Started by
5 comments, last by Andre the Giant 21 years, 7 months ago
I wanted to make a 2d array whose size didn't need to be known at compile time. After looking around a bit, I was convinced to go with a vector of vectors. (Side note: it seems that every time I think I need an array for something, someone or some reference always tells me I would be better off using a vector. Should I just assume vectors are always better and never even consider using an array again?) Anyway, my reference book gives this example code for creating a vector of vectors: vector > myVector(3, vector(5)); I ended up using a slight variation on this, since I wanted this vector of vectors to be a member variable of a class, so I had to declare it in one spot (header file): vector< vector > myVector; and define it in another spot (constructor): myVector = vector< vector >( 3, vector(5) ); This worked, but it looks kind of ugly and I don't think I understand the syntax completely. The '(3, vector(5))' are the arguments to the vector constructor, right? First of all, I don't understand why you couldn't just say '(3, 5)'. What is the significance of sticking in the seemingly redundant vector ? More importantly, how do I figure out stuff like this on my own? I spent quite a bit of time going through the VECTOR include file located in the "Microsoft Visual Studio\VC98\Include" directory of my MSVC++ 6 installation. Was this not the right place to look? The contents were pretty confusing to me, but I'm pretty sure that I didn't see any constructor that took 2 such arguments as the '( 3, vector(5) )' suggests. I would love to be able to look at the standard include files to sort out problems like this for myself, but it is so convoluted! I think that by the time I'm smart enough to read the header files, I won't need to Please shed a little light on my situation, as I am so obviously confused! Thanks! Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for life (and he'll quit bugging you with dumb questions!) EDIT: Ok i just re read this post and It seems that what I put for my vector example is not what's showing up in the forum. I guess it must have to do with the less then / greater then signs? I don't know how to fix that. [edited by - Andre the Giant on August 26, 2002 2:44:51 PM]
Its not my fault I''m the biggest and the strongest; I don''t even exercise.
Advertisement

      vector<vector<int> > myVector(3, vector<int>(5));// I ended up using a slight variation   on this, since I wanted // this vector of vectors to be a member variable of a class, so // I had to declare it in   one spot (header file):vector< vector<int> > myVector;// and define it in another spot (constructor):myVector = vector< vector<int> >( 3, vector<int>(5) );// ROB use the `source` and forward-slash-source flags to do this (square brackets around them)// - click on the reply button and you'll see what I mean .... /ROB  


To answer your question, i actually don't like using a vector of vectors or list of vectors like that - the syntax is just plain ugly....

I tend to do something like this (if I really really have to)


        struct row{	std::vector<int> Rows;};struct 2Darray{	2Darray(unsigned int rws,unsigned int cols)	{		Columns.resize(cols);		std::vector<row>::iterator it = Columns.begin();		for(;it != Columns.end();++it)		{			it->Rows.resize(rws);		}	}	std::vector<row> Columns; };// then something for construction like2Darray myArray(3,5);// and element access likemyArray.Columns[2].Rows[1] = 5;       


To be honest I don't really like that very much either, I'm kindof a bit resentful if I ever have to use it. Usually I'd use a single vector and use indices within the array.

as an additional point, I hope you are including vector and not vector.h, and using std::vector not the old vector type?

Generaly you shouldn't have to go through the header files to figure stuff out, msdn is much nicer for that stuff....

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfvectorclass.asp

edit: damn html tags /edit

[edited by - RobTheBloke on August 26, 2002 4:32:05 PM]
Thanks for the reply!

You''d think there would be an easier way to create a 2d array of something, since I doubt it is that rare that people need them.

Yes, I was talking about Vector and not Vector.h

I thought maybe you could clarify what you meant by this comment:

"Usually I''d use a single vector and use indices within the array."

Did you mean just create a big vector that is (rows*cols) long and use a seperate array to keep track of what row you are on? If so, that seems like a more confusing method to me.

Anyway, thanks again.



Its not my fault I''''m the biggest and the strongest; I don''''t even exercise.
Its not my fault I''m the biggest and the strongest; I don''t even exercise.
quote:Original post by Andre the Giant
(Side note: it seems that every time I think I need an array for something, someone or some reference always tells me I would be better off using a vector. Should I just assume vectors are always better and never even consider using an array again?)

Not quite. The rules are (or should be) to prefer a vector whenever you need an array-like structure. Then, you only choose an array when the vector doesn''t meet your actual needs, which will be very rarely.

This might help: Why should I use container classes rather than simple arrays?
quote:
I guess it must have to do with the less then / greater then signs? I don''t know how to fix that.

Use the source tags, which you can read about in the Gamedev FAQ.

quote:Original post by RobTheBloke
i actually don''t like using a vector of vectors or list of vectors like that - the syntax is just plain ugly....

The syntax for dynamic arrays is even uglier, and they behave in a rather brain-damaged manner.
If not for education purposes , then std::vector is preferable.
"after many years of singularity, i'm still searching on the event horizon"
For a little better syntax you can overload operator[] :

  class Row {vector<int> Data;public:int& operator[](int k) {return Data[k];}};class Array {vector<Row> Data;public:Row& operator[](int k) {return Data[k];}};Array A(5,5);A[4][4] = 10;  

Written on the fly, so expect incorrect code - but the basic idea is right...not sure about the references (but hey! on the fly code...)
also you can do bounds checking if you want..
of course you have to define constructos, etc

C++Freak (can''t login in cuz password changes...and I''m lazy. I''m not emailing dave. Yet.)
As for the long redundant template defintions: there''s this handy keyword called "typedef"


  struct MyClass{typedef std::vector<int> vector_t;typedef std::vector<vector_t> vector2D_t;vector2D_t some_vector;void ResizeVector(int x, int y);void FillVector(int x);};void MyClass::ResizeVector(int x, int y){this->some_vector.resize(x);vector2D_t::iterator it, itEnd = this->some_vector.end();for(it = this->some_vector.begin(); it!=itEnd; ++it)   it->resize(y);}void MyClass::FillVector(int x){int n = this->some_vector->size();for(int i=0; i<n; ++i)	int m = this->some_vector[i]->size();	for(int j=0; j<m; ++j)		this->some_vector[i][j] = x;}  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement