1. ## 2D Vectors

I have a usual vector that I have declared like this:

size_t size = 100000;
std::vector<int> Test(size);

Now I want to have a 2D vector instead of this.
Like an ordinary array it would look like for example:
Test[5][5];

But now if I want to create a 2D for the vector with the same size(100000).
How would the syntax look like for this ?

2. Code:
`std::vector< std::vector<int> > v;`
Also, you don't need to specify a size for vector. It's dynamic, so it will resize itself if needed.
But also need to do:
Code:
`v.push_back( std::vector() );`
To create the first dimension so you can access the second dimension:
Code:
```v[0].push_back(0);
int n = v[0][0]```

3. A 2D vector would be a vector of vectors (of int).

Code:
`std::vector<std::vector<int> > vec2d(100, std::vector<int>(1000));`
Typedefs can reduce some typing:
Code:
```typedef std::vector<int> IntVector
typedef std::vector<IntVector> IntVector2D
IntVector2D vec2d(100, IntVector(1000));```
Now vec2d holds 100 vectors each holding 1000 integers.

4. I think I understand. To be sure, if I take the example for:

std::vector<std::vector<int> > vec2d(100, std::vector<int>(1000));

Will I access the 2D vector like this:

vec2d[100][1000];

5. Yes, but again, with a vector, you do no have to specify a size! Unless you use it as a buffer, you never need to specify size.

6. more like vec2d[99][999];

7. Originally Posted by robwhit
vec2d[99][999];
How do you figure that?
Sure, the highest index will be 99 and 999, but the declaration or definition should be vec2d[100][1000].

8. Yes I will change my code to not specify size.
If I dont specify size, I am not really sure how it should be written.

Should it be written like this if you dont specify size:
std::vector<std::vector<int> > vec2d((), std::vector<int>());

2D Vectors

10. Yes ofcourse, now I understand and ofcourse when you access the 2D-array with size 100 and 1000 it should be written like this instead:
vec2d[99][999];
As the first position starts at 0.

11. This:
Code:
`std::vector<std::vector<int> > vec2d(100, std::vector<int>(1000));`
is equivalent to this:
Code:
`int arr2d[100][1000];`
You should specify the size if you know what it is before hand. If you don't know, then you can use push_back to add elements.

To access existing elements in a 2-D array or vector, you use:
Code:
```std::cout << vec2d[0][0];
std::cout << vec2d[99][999];
std::cout << arr2d[0][0];
std::cout << arr2d[99][999];```
But remember that you can only access the elements in the vector(s) if they exist. So if you don't specify a size you can't access those elements until you've push_back'd enough items.

12. Originally Posted by Daved
You should specify the size if you know what it is before hand. If you don't know, then you can use push_back to add elements.
Kind of depends on the performance of your application. In typical programs, it won't matter, since it's lightning fast anyway,
You would get what some call a memory hog

13. It also depends on what it is meant for. The dimensions can have a known fixed size and you may want to be able to start accessing things right away.

14. >>vec2d[99][999];
>How do you figure that?
the op asked how to access it, not how to declare/define it.

15. I see, so you were basing it on access and not declaration/definition.