Konroi
Konroi

Reputation: 9

How to give a size to a vector array?

Hello I would like to set 18 elements in my vector array but i'm unsure on how to do it exactly. Would it be like this?

vector<byte> Clients(18);

or like this?

vector<byte> Clients[18];

Upvotes: 0

Views: 1097

Answers (1)

Coda17
Coda17

Reputation: 599

Vectors are not called vector arrays. They are usually implemented as arrays in the background, but that doesn't change what they are called.

To answer your question, to change the size of a vector you can use the resize member function.

std::vector<int> myvector;
myvector.resize(5);

You can also use resize to initialize all the values. For instance, in the following example there are five 0's in the vector.

std::vector<int> myvector;
myvector.resize(5, 0);

Usually, you just push things into a vector and don't have to set a size. For example:

std::vector<int> myvector;
for (int i = 0; i < 5; i++) {
    myvector.push_back(
}
// you now have a vector with a size of 5 that has initialized 
// values at indices 0 through 4, inclusive.

Upvotes: 1

Related Questions