In the main function, I'm initializing a variable of type std::vector <int>
like this:
std::vector <int> edges[9]
and then add my elements to it.
what i wanna know is there a way to initialize edges with variables ?? To do something like this: (already know that this writing gives error)
int n=9;
std::vector <int> edges[n]
If you want to set the size of the vector, use std::vector<int> foo(9)
. What you are doing is initializing an array with nine separate vectors.
For a variable, you can do std::vector<int> foo(n)
.
If I understood your question correctly, you want to have a vector initialized with "9" int space and you want to initialize the same with a particular value "10" for example
Here is how you can achieve the same.
std::vector <int> edges;
edges.resize(9);
cout<<"size =>"<<edges.size()<<endl;
std::fill(edges.begin(), edges.end(), 10);
resize
reserves the initial size and std::fill
can be used to fill the same with desired value.
Hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.