Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: jwalker on March 28, 2012, 02:07:38 pm

Title: will a list work for this
Post 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;
}
Title: Re: will a list work for this
Post by: Scipi on April 25, 2012, 08:31:11 am
Since the server crash deleted my response, if you need me to I can repost my answer if you need to refer to it. ;)
Title: Re: will a list work for this
Post by: jwalker on April 25, 2012, 09:08:35 am
What was the compiler option to enable the use of a regular iterator?
Title: Re: will a list work for this
Post by: Sorunome on April 25, 2012, 09:29:20 am
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
Title: Re: will a list work for this
Post by: Scipi on April 25, 2012, 09:32:28 am
A regular iterator doesn't need a compiler option, the keyword auto needs the 0x11 standard though.

For MinGW:
Code: [Select]
-std=c++0x11
Title: Re: will a list work for this
Post by: jwalker on April 25, 2012, 11:21:41 am
yea, i might need the code you posted to reference to it, i cant remember exactly what it was
Title: Re: will a list work for this
Post by: Scipi on April 25, 2012, 01:24:45 pm
Code: [Select]
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;
}