I am trying to figure out how to properly use a list of vectors.
I understand how to declare
list< vector<int> > variableName;
but I do not know how to actually set anything to it or pull any information out of it.
More specifically, I am trying to make a list of vectors of objects and I would like to be able to set and pull information from this.
list< vector<ClassObject> > listOfVectorsOfClass;
Can anyone help me out?
You can access information with iterators:
list< vector<ClassObject> >::iterator list_it;
vector<ClassObject>::iterator vec_it;
for (list_it = listOfVectorOfClass.begin(); list_it != listOfVectorOfClass.end();
++ list_it)
{
for (vec_it = list_it->begin(); vec_it != list_it->end(); ++ vec_it)
{
//do something with vec_it
//for example call member function of Class
(*vec_it).print();
}
}//can use const_iterator depends on what you will do on class objects
It is the same thing as you access list of vectors of int.
Do you want a sample ?!
list< vector<int> > variableName;
variableName.push_back({1, 2, 3});
variableName.push_back({4, 2, 6});
for (auto &v : variableName)
{
for (auto &x : v)
cout << x << " ";
cout << endl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With