Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of vectors [closed]

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?

like image 594
Jacob Moulton Rhodes Avatar asked Nov 23 '25 03:11

Jacob Moulton Rhodes


2 Answers

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.

like image 126
taocp Avatar answered Nov 25 '25 16:11

taocp


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;
}
like image 39
masoud Avatar answered Nov 25 '25 16:11

masoud