How to give a size to a vector array? - c++

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];

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.

Related

Difference between vector <int> V[] and vector< vector<int> > V

vector <int> V[] and vector< vector<int> > V both are 2D arrays.
But what is the difference between them and where do we use each one? Please give a brief explanation.
vector<int> V[] is an array of vectors.
vector< vector<int> > V is a vector of vectors.
Using arrays are C-style coding, using vectors are C++-style coding.
Quoting cplusplus.com ,
Vectors are sequence containers representing arrays that can change in
size.
Just like arrays, vectors use contiguous storage locations for their
elements, which means that their elements can also be accessed using
offsets on regular pointers to its elements, and just as efficiently
as in arrays. But unlike arrays, their size can change dynamically,
with their storage being handled automatically by the container.
TL;DR:
When you want to work with a fixed number of std::vector elements, you can use vector <int> V[].
When you want to work with a dynamic array of std::vector, you can use vector< vector<int> > V.
One difference would be that although both can be initialized in the same way, e.g.
vector<int> V1[] {{1,2,3}, {4,5,6}};
vector<vector<int>> V2 {{1,2,3}, {4,5,6}};
and accessed
cout << V1[0].back() << endl;
cout << V2[0].back() << endl;
the V1 can't grow. You cannot make V1.push_back(...) as its not a vector object. Its just an array. Second one is dynamic. You can grow it as you please.
vector<vector<int>> v(26); is a vector containing vectors. In this example, you have a vector with 26 vectors contained in it. The code v[1].push_back(x) means that x is pushed back to the first vector within the vectors.
vector<int> a[26]; is an array of vectors. In other words, a one-dimensional array containing 26 vectors of integers. The code a[1].push_back(x); has x being pushed back to the first index of the array.
vector<int> v[] is an array of vectors. That is, it is an array which contains vectors as its elements.
So, you cannot change the size of the array part, but we can add to its elements which is vector.
For example,
1.vector<int> v1[] = {{1},{2},{3}}; // array that contains 3 vector elements.
2.vector<vector<int>> v2 = {{1},{2},{3}}; // vector that contains 3 vector elements.
So for the first we cannot change the size of v but we can add or delete elements to its elements since it is a vector.
v1.push_back(4); // this will give an error since we cannot the predefined size of array.
v1[1].push_back(4); // this is acceptable since we are changing the vector part.
This makes the v1 {{1},{2,4},{3}}
For the second one, we can change both the overall size and its elements.
v2.push_back(4); // this is acceptable since it is vector.
vector V[] is just a fixed array; and so you can add/modify only till the upper limit. It is not a vector per se, and so has a fixed size limit.
However vector< vector > V is a dynamic vector and its size can be increased dynamically.

Change from static array to vector

here's my code:
vector<int> Edge[1000000]; //size of array must be very high
scanf("%d%d",&N,&M );
//N = size of workable index numbers for Edge
for( i=1;i<=M;i++){
scanf("%d%d",&u,&v );
Edge[u].push_back( v );
}
But as you can see, it's a static array of vectors.
If i change it to this:
vector<vector<int>> Edge;
How can i do that for cycle and pushback? I need to create a vector of N+1 size and each position is also a vector.
I need to create a vector of N+1 size and each position is also a vector.
Considering that you know the size of the external container at compile time, you can also use std::array to store the vectors:
std::array<std::vector<int>, N + 1> Edge;
If you don't know the size at compile time, you could use:
std::vector<std::vector<int>> Edge;
but as a general rule of thumb, tend to avoid vectors of vectors, as they don't usually work nicely.
You can construct vectors of a given size by passing an int to their constructor. Then afterwards you can use a loop to initialize the subvectors.
int size;
vector<vector<int>> Edge(size); //Initializes Outer Array to have 'size' elements
for(auto i:Edge)
{
// i is a vector within Edge
}
The advantage is that you don't have to push back. If you still wanted to push_back in order (not have to track an iterator/int) you could use vector.reserve() instead.

vector of vectors push_back

I'm designing a multilevel queue process simulator in C++ but I've got a problem when trying to implement several queues (my queues are vectors).So,
"multilevel" is a 4 elements array (not vector). Inside each of those elements there is a vector (type t_PCB).
vector<vector<t_PCB>> multilevel[4];
My question is: How can i insert an element at the end of one these 4 t_PCB vectors? Thank you in advance.
I've tried the code line below but it doesn't work (error: not matching member function for call 'push_back')
multilevel[0].push_back(p); //where "p" is a t_PCB object
The line from above can not be used when talking about "multilevel" because this array only accepts arguments type: vector < t_PCB >
So, as I ask at the beginning: how can I push an object type "t_PCB" inside "multilevel"?
By doing this:
vector<vector<t_PCB> > multilevel[4];
You declare an array of four zero-sized vectors, each of which can contain objects of type vector<t_PCB>. What you probably wanted to do is rather:
vector<vector<t_PCB> > multilevel(4);
// ^^^
This will instantiate a vector of four default-initialized objects of type vector<t_PCB>. Then, you can just do:
multilevel[size].push_back(p);
Notice, though, that vector indices (like array indices) are zero-based, so size must be less than the size of the vector.
In the above expression, the sub-expression multilevel[size] returns a reference to the size-th vector inside multilevel, and on that vector you are then invoking the member function push_back(p), which appends element p to it.
Declaring a two dimensional vector is similar to declaring an array. You can also use it in same way...
vector<vector<int> > vec;
for(int i = 0; i < 5; i++)
{
vector<int> row;
vec.push_back(row);
}
vec[0].push_back(5);
cout << vec[0][0] << endl;
You are creating a array of vector<vector<t_PCB>> instead of a single object.
I think the right way to do what you want is:
vector<vector<t_PCB>> multilevel(4);
multilevel[0].push_back(p)
You can create a vector instead of array:
std::vector< std::vector<t_PCB>> multilevel(4); // 2 dim array, 1st dim is 4
and then you can push_back at the end of the vector indexed with WHICH this way:
multilevel[WHICH].push_back(p)
And just to put it out there, to access the vector of vectors:
multilevel[outer][inner]
where outer will return the vector at that index and further indexing
with inner will return the t_PCB object. You could also replace the array-style indexing with the .at() function for bounds checks.
vector<vector<int>> vec; // declare 2D vector
for (int i=0; i<=3; i++) {
vector<int> row; // create a row vector which adds a row to vec
for (int j=0; j<=4; j++) {
row.push_back(j*10); // push elements 0,10,20,30,40 to row
}
vec.push_back(row); // add this row to vec
// Repeat this procedure 4 times to make a 4*5 2D vector
}
cout<<"output is "<<vec[2][4]; // output is 40

How to create a vector of user defined size but with no predefined values?

In C++ one can create an array of predefined size, such as 20, with int myarray[20]. However, the online documentation on vectors doesn't show an alike way of initialising vectors: Instead, a vector should be initialised with, for example, std::vector<int> myvector (4, 100);. This gives a vector of size 4 with all elements being the value 100.
How can a vector be initialised with only a predefined size and no predefined value, like with arrays?
With the constructor:
// create a vector with 20 integer elements
std::vector<int> arr(20);
for(int x = 0; x < 20; ++x)
arr[x] = x;

assigning vector

how I can assign v[i] to an series of integers ( type of v is vector ) without initially filling inside
Do you mean initializing std::vector to a series of integers?
int i[] = {1, 2, 3, 4, 5};
std::vector<int> myVector(i, i+ (sizeof(i)/sizeof(int)));
If you meant to create a vector of some elements so you can perform the assignment using their index values. Here, the following statement declares and initializes a vector with its elements being default initialized to 0.
std::vector<int> myVector(5); // constructs a vector of size five integers.
for (int x = 0; x < 5; x++)
myVector[x] = i[x]; // assign values using subscript [..]
But I think the even better way to go would be as #CashCow mentioned in his answer.
Also, note that you can also pre-allocate memory to add elements into the vector with avoiding any repeated memory allocations.
For example:
std::vector<int> myVector; // empty vector for integers
myVector.reserve(5); // pre-allocates memory for five integers
for (int i = 0; i < 5; i++) // now, you can add your elements
myVector.push_back(i);
It is usually a good idea to pre-allocate memory if you know the size of elements i.e in case of large number of elements when the performance is an important factor.
If you have anything that has the traits of an iterator you can use vector's assign method:
std::vector<int> v;
v.assign( iterStart, iterEnd );
iterStart should be such that *iterStart is the first value you want to add.
iterEnd should be one past the end, it is a terminating condition
++iter would move you to the next iterator in the input series.
I don't know what you mean by assign v[i] though. You cannot assign an element to a series. If you want to write the series at a location into an existing vector you can use insert instead of assign.
common way of adding items is calling std::vector<>push_back() method.
std::vector<int> myVector;
myVector.push_back(5);
myVector.push_back(10);
myVector.push_back(3);