Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: jwalker on March 28, 2012, 02:07:38 pm
-
here is what i would like to know...
if i made a class would i be able to put them into a list and then access members if i were to loop through the list?
say i have a class called person and the members are int age, and string name;
i have a list list<Player> p;
could i loop throug it like this:
for (int i = 0; i < p.size(); i++)
{
cout << somethingtodowithp.name;
}
-
Since the server crash deleted my response, if you need me to I can repost my answer if you need to refer to it. ;)
-
What was the compiler option to enable the use of a regular iterator?
-
it would work if you made an array named p with the variable type members.
Even thought I would do it with vectors if the size shall change during the progam
-
A regular iterator doesn't need a compiler option, the keyword auto needs the 0x11 standard though.
For MinGW:
-std=c++0x11
-
yea, i might need the code you posted to reference to it, i cant remember exactly what it was
-
list<Player> p;
//Fill p with Players
for (int i = 0; i < p.size(); i++)
{
cout << p[i].name;
}
//Or
for (auto i = p.begin(); i < p.end(); i++)//Auto just makes declaration SO much easier
{
cout << *i.name;
}
//Without Auto
for (list<Player>::iterator i = p.begin(); i < p.end(); i++)
{
cout << *i.name;
}