# vectors a couple questions

• 09-13-2003
curlious
vectors a couple questions
can vectors be n dimensional?
ie
std::vector<int> map[10][10];

and can you assign elements to a vector using array notation ie
map[0][0]=10;

How do I initialize a vector?
I saw an example where an array was created then used in the declaration of the vector.
integer array[10]={1,2,3,4,5,6,7,8,9,10};
std::vector<int> map(array);//somthing like this is this the only way or even correct as im going off my own feeble memory.
• 09-13-2003
Zach L.
Well, you can have vectors of vectors. For example, a matrix (can be represented as): vector<vector<int> >

As for initialization, you can fill the whole thing with a given value on creation: vector<int> array(10, 2); // 10 elements containing the value 2

You might want to look at Blitz++. It might be more suited to your purposed if you are dealing with matrices and tensors: http://www.oonumerics.org/blitz/
• 09-13-2003
Hunter2
Quote:

can vectors be n dimensional?
ie
std::vector<int> map[10][10];
No, but then arrays can't be n-dimensional either, technically; you can only have arrays of arrays, which in most cases amounts to pretty much the same as a 2d array. Similarly, you can make a vector of vectors, but not really a "2d" vector.

std::vector<std::vector<int>> map;

Your code, if I'm interpreting it correctly, will create an array of 10 arrays of 10 vector<int>'s. In other words, that will create a 10X10 "2d" array of vectors.

Quote:

and can you assign elements to a vector using array notation ie
map[0][0]=10;
Yes, if you make a vector of vectors like I said above. Otherwise you would be assigning the value 10 to a vector, not an int variable.

Quote:

How do I initialize a vector?
I saw an example where an array was created then used in the declaration of the vector.
I don't think you can initialize it with an array, although that might be possible. You can initialize it with another vector though, and it will copy the other vector's contents.
• 09-13-2003
curlious
Thanks for the replies. I am not working with Matrices I just thought I would use a vector for a 2d map to create a map generator/solver.

Again thanks for helping clear things up.
oh btw found where I saw the code for initializing a vector with an array fig 21.15 pg 1110 Deitel & Deitel C++ How to Program
here is the excerpt

Code:

```. . . 15    int array[SIZE]={1,2,3,4,5,6}; 16 17    std::vector<int> integers(array,array+SIZE); . . .```
• 09-13-2003
Hunter2
Ah, yes. I thought that was only for copying a section of a vector (using begin() etc. to get the "iterators"), but then an iterator is just a pointer, so I suppose the array works too...
• 09-13-2003
curlious
Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; #include <stdlib.h> #include <vector> #include <algorithm> int main(int argc, char *argv[]) {   std::vector<std::vector<int> > map;     map[0][0]=10;     cout << "Vector contents:\n";             system("PAUSE");          return 0; }```
ok I am able to create a vector of vectors and access an individual member but I have a couple of questions/problems.

I created the vector of vectors but do not know how to declare the number of elements in the vectors or how to initialize say a vector of a int vector of 10 elements to a given number. I understood the simple initialization ie std::vector<int> map(10,2); but how do I do it with a vector of vectors.

Secondly since I am accessing an element that hasn't been declared I assume it is dynamicaly creating the first element similar to function push_back();

finally if I try this code
std::ostream_iterator<int> output(cout, " ");

I get the compiler error:
main.cpp:13: `ostream_iterator' undeclared in namespace `std'

Is this not valid?
• 09-13-2003
Hunter2
>but how do I do it with a vector of vectors.
Usually I just use single vectors, and use push_back when I want to add an object. But you can use resize() to allocate memory for more objects. Example:
Code:

```std::vector<std::vector<int>> intVect; intVect.resize(10); //now it holds 10 vector<int>'s for(int i = 0; i < intVect.size(); ++i) {     intVect[i ].resize(10); //now each of 10 vector<int>'s holds 10 ints } //so now we have a 10X10 "2D" vector.```
I usually don't like using resize though.

>Secondly since I am accessing an element that hasn't been declared I assume it is dynamicaly creating the first element similar to function push_back();

Quote:

vector::operator[]
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);

The member function returns a reference to the element of the controlled sequence at position pos. If that position is invalid, the behavior is undefined.
• 09-13-2003
Cat
If you want to create a vector of nRow x nCol, then this will work:

typedef std::vector<int> iRow;
typedef std::vector<iRow> iArray;

int nRow = 5, nCol = 10;

iArray myArray(nRow, iRow(nCol));

This creates a '2D array' of ints from myArray[0][0] to myArray[nRow-1][nCol-1].
• 09-13-2003
curlious
Thanks again for the further response. This answered my questions!
• 09-13-2003
Hunter2
... Is there any good reason for doing things in this way? To me it looks as bad as using a couple of #define's or even worse :eek: It looks like it works, but laying it out this way becomes a "Memorize this!" type of thing for beginners, and a lot of people won't bother looking into exactly how it works (which is a lot different than it appears on the surface).
• 09-14-2003
Cat
Which way, the one I said? I just think the typedefs are easier to follow, as opposed to:

std::vector<std::vector<int> > myArray(nRow, std::vector<int>(nCol));

but that works, too. I tend to typedef a lot, especially with templates, because I would hate to do something like:

std::vector<std::vector<boost::shared_ptr<MyClass> > > myArray(nRow, std::vector<boost::shared_ptr<MyClass> >(nCol));

which is actually a reasonable thing to use (a 2D array of pointers).
• 09-14-2003
Hunter2
True... although I don't see why you don't just use an array in that case. If you're going to need a 2D grid of mostly anything, most likely you won't be dynamically adding/removing things - otherwise your 2D rectangle would probably become rather skewed quite quickly. And besides, wouldn't an array be more efficient (since you don't need all the extra functionality that a vector gives you)?

boost::shared_ptr<MyClass> myArray[nRow, nCol];

**EDIT**
Oops, forgot you can't create an array like that... Well, I guess you do have sort of a point, although I thought people usually don't need the std::/boost:: in front of everything (using namespace xxx).

vector<vector<shared_ptr<MyClass>>> myArray(nRow, vector<shared_ptr<MyClass>>(nCol));
• 09-14-2003
Cat
Well, I *never* use arrays of dynamically allocated memory unless I really need to; vectors are virtually the same performance and they clean up after themselves.

A nice feature about std::vector is that it knows its own size -- so it can easily be passed anywhere, and the size goes along with it.

Apart from some memory overhead, a vector is practically as good as an array, and safer to boot.

Oh, I never use "using namespace xxx" unless I was porting legacy code. I much prefer to always see the std::, because there are cases (vector being one of them) where I have multiple libraries with the same class names.
• 09-14-2003
Hunter2
>>I much prefer to always see the std::
Me too, I'm just trying to be a pain :D

You do have a good point about the performance being virtually the same though, I always subconsciously thought that vectors would be slower than arrays, but then vectors are pretty array-wrappers so...