List of vectors
15,772
Solution 1
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
.
Solution 2
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;
}
Solution 3
#include <cstdio>
#include <list>
#include <vector>
#include <iterator>
int main(){
std::list< std::vector<int> > li;
li.push_back(std::vector<int>());
li.push_back(std::vector<int>());
std::vector<int> v3(3);
v3.push_back(1);
v3.push_back(2);
v3.push_back(3);
li.push_back(v3);
for (std::list<std::vector<int> >::iterator it1 = li.begin(); it1 != li.end(); ++it1){
std::vector<int>::iterator it2;
for (it2 = (*it1).begin(); it2 != (*it1).end(); ++ it2){
std::printf("element: %d\n",(*it2));
}
}
return OK;
}
if you are familiar with new C++11 syntax then it could be even more simple:
std::list< std::vector<int> > li;
li.push_back({1, 2, 3});//push_back vector
li.push_back({4, 5, 6});//again, vector implicitly
for (auto &v : li)
{
//even better auto const since we need read only access to elements
for (auto const &i : v)
std::printf("element: %d\n",i);
}
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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?
-
Mahesh about 11 years+1 But you could directly use
list_it->begin()
in the inner loop. -
taocp about 11 years@Mahesh yeah, agree. Will update it now
-
Christian Rau about 11 yearsBy the way, why on earth declare the iterators outside of their for loops?
-
Christian Rau about 11 yearsC++11 and
printf
, ouch! -
4pie0 about 11 yearsI know it is little mixed, but not forbidden, right?
-
Christian Rau about 11 yearsOf course it's not forbidden (that's why it's not worth a downvote anyway, though unfortunately not an upvote either). But if you
re using the C++ version from
<cstdio>` and notusing namespace std
, you should at least qualify it appropriately (std::printf
). -
4pie0 about 11 years@ChristianRau well you can always find some post to downvote this is not difficult
-
Admin about 11 yearsThis is perfect! A list of vectors is really confusing but you really laid out the foundation for me. After showing me it like this, it makes much more sense. Still a little confusing so I have to move slowly but I think I can manage it from here. Thank you guys so much!
-
Christian Rau about 11 years@tacp And that is because...?
-
taocp about 11 years@ChristianRau I updated the post. Probably because of my bad coding style. I agree it should be put outside the first for loop.
-
Christian Rau about 11 years@tacp Wow, that's even worse.