Basic vector qeustion

What does the [100] mean?
 
vector<int> desks[100];
That line declares an array of 100 std::vectors (as opposed to one std::vector with 100 elements).

-Albatross
Is that the same thing as a 2d vector?
Is that the same thing as a 2d vector?

Yes, and yet no.

A "true" 2D vector could be declared thusly (based on what you posted):

std::vector<std::vector<int>> desks(100, std::vector<int>);

You will have 100 vectors (rows), each containing an empty vector. 100 rows, zero columns. You will now have to add (push_back) elements into the empty vectors.

The "best" way to instantiate a 2D vector is with known row and column sizes:

std::vector<std::vector<int>> aVector(row_size, std::vector<int>(col_size));

This initializes all the elements to zero, since the type of the 2D vector is int.

You could create a completely empty 2D vector:

std::vector<std::vector<int>> aVector;

No rows, no columns. You have to create a temp vector with the column elements and then push that temp vector into aVector.

Lather, Rinse, Repeat. Until you have all your row vectors added.

I prefer working with known row and column sizes, far less work.
If you were creating this as a C style 2D array:

int desks[100][0]; // illegal!!

A vector can be instantiated with no elements, size = 0. Can't create a zero-sized array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

int main()
{
   std::vector<int> desks[5];

   std::vector<int> temp { 1, 2, 3, 4, 5 };

   desks[2] = temp;

   for (size_t loop = 0; loop < 5; loop++)
   {
      std::cout << '#' << loop << ": " << desks[loop].size() << '\n';
   }
}
#0: 0
#1: 0
#2: 5
#3: 0
#4: 0
What does the [100] mean?

vector<int> desks[100];

this is a 1-d array of 100 items. It is not 2-d at all, though you can 'interpret' it as 2-d yourself, as show above.

[100] is an initial size. just like an array, you know the size ahead of time and just use it. Unlike an array, this can be a variable in c++ (though compiler extensions allow variables on arrays, its technically not allowed). Unlike an array, you can change this size if you want to. Unlike arrays, the current size is part of the object -- you don't need a constant for the size or to track it yourself in the code or to pass it to all consumer functions.


without the initial size, you can use push-back or reserve or other vector methods to allocate space and size it for storing your data. push back will grow it for you, item by item. As with dynamic memory 'arrays', constantly resizing is expensive, so you want to think about that as you design code around vectors to avoid resize as much as possible for large #s of items.

the [size] vector lets you very easily convert old code with arrays to use vectors. the use notation is the same .. x[index] = value or the like; but you will need to adjust all the parameter passing and any pointer style access.
Last edited on
afatperson wrote:
What does the [100] mean?
vector<int> desks[100];

Lets check, do you know what the [100] means in:
double chairs[100];
What is the type of chairs?
@afatperson,

It would probably move the discussion forward in a better direction if you'd hint as to what you want to do.

It could be you found this in some code and just want to understand. Without more code, we can't really figure out why this was what the original author thought was useful.

It could be that you tried this toward some goal and need to figure out if it is useful.

Is your goal a 2D array-like storage?
Last edited on
Topic archived. No new replies allowed.