By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Linked List Help please

P: n/a
Thanks in advance for the help.

I'm new to the STL and havig a bit of trouble figuring out the whole
iterator thing. I am using the <list> template and I can insert
elements into the list just fine. I just can't get them out. It's
like the roach motel! (uh, you can go in, but don't come out...
anyway...)

Actually, I have been able to get some help from
http://www.sgi.com/tech/stl/List.html

I can get the forst element or the last, but I can't find one
in-between or pick one out like you would in an array by referencing
the element number.

Can someone give me a hand or point me to a tutorial? I have found a
lot of tutorials, but all of them use ints as elements in the list,
and I need to store objects.

#import<list>

list<People::People> PP;

//person is a People
person.setName("Shane");

PP.push_back(person);

Now the person has been added to the linked list. I can get it out by
saying

person2 = PP.front();
cout << person2.getName();

but this is only if the person is at the front of the linked list.

Any suggestions? I know it's all there at
http://www.sgi.com/tech/stl/List.html in black and white. I just
don't understand it.

Shane
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> #import<list>

list<People::People> PP;

//person is a People
person.setName("Shane");

PP.push_back(person);

Now the person has been added to the linked list. I can get it out by saying

person2 = PP.front();
cout << person2.getName();

// Looping through all items
for (list<People::People>::iterator it = PP.begin(); it!=PP.end();
++it)
{
person2 = *it; // Get the nth person
cout << person2.getName();
}
// Getting at any index:
list<People::People>::iterator it = PP.begin();
std::advance(it, 3); // Get list element[3];

HTH,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #2

P: n/a

"Shane" <de**********@zaft.com> wrote in message
news:d0*************************@posting.google.co m...
Thanks in advance for the help.

I'm new to the STL and havig a bit of trouble figuring out the whole
iterator thing. I am using the <list> template and I can insert
elements into the list just fine. I just can't get them out. It's
like the roach motel! (uh, you can go in, but don't come out...
anyway...)

Actually, I have been able to get some help from
http://www.sgi.com/tech/stl/List.html

I can get the forst element or the last, but I can't find one
in-between
To pick by a criteria:

#include <map>

std::list<std::string, People::People> PP;

PP[person.getName()] = person;

cout << PP["Shane"].getName();
or pick one out like you would in an array by referencing
the element number.


To pick by an index:

#include <vector>

std::vector<People::People> PP;

PP.push_back(person);

cout << PP[0].getName();

To iterate (_any_ container)...

for(std::vector<People::People>::iterator it = PP.begin(); it != PP.end();
++it)
cout << *it << endl; // (almost any - map is different here)

Which container type you pick depends on how you intend to use it. list make
inserting middle nodes very fast, and random access impossible. Vectors make
inserting middle nodes very slow (but not slow enough anyone would ever
notice), and make random access very fast.

Please read more than one STL tutorial before continuing.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3

P: n/a
"Shane" <de**********@zaft.com> wrote in message
I can get the forst element or the last, but I can't find one
in-between or pick one out like you would in an array by referencing
the element number.
You have to iterate through the elements in the list to find the one you
want. For example, if you want the 10th person, do

assert(PP.size() >= 10);
list<People>::const_iterator iter = PP.begin();
for (int i=0; i<9; ++i) ++iter;
cout << iter->GetName();

You can also use std::advance, but be aware that it is no faster than the
above,

assert(PP.size() >= 10);
list<People>::const_iterator iter = PP.begin();
std::advance(iter, 9);
cout << iter->GetName();

#import<list>
Should that be #include
list<People::People> PP;

//person is a People
person.setName("Shane");

PP.push_back(person);

Now the person has been added to the linked list. I can get it out by
saying

person2 = PP.front();
cout << person2.getName();

but this is only if the person is at the front of the linked list.

Any suggestions? I know it's all there at
http://www.sgi.com/tech/stl/List.html in black and white. I just
don't understand it.


The other thing is that you may want to find people by first or last name or
something else. In that case you have to iterate through all elements in
the list, and just pick out the ones whose name matches.

The above approach could be slow if we have thousands of people but are
looking for just one. There are then other fast approaches you can take.
Jul 22 '05 #4

P: n/a
On Tue, 10 Aug 2004 23:16:15 -0700, Shane wrote:
I can get the forst element or the last, but I can't find one
in-between or pick one out like you would in an array by referencing
the element number.
The common way of accessing the elements of standard containers is to
use iterators returned from the begin() and end() member functions.

Briefly, iterators are generalized pointers that provide access to the
object they point to through operator* and get to the next object
through operator++.

std::list is not the right container if you need to access objects
randomly. A list of objects is accessed from beginning to the end in
order.

If you need random access as in an array, std::vector or std::deque
would be a better choice.
#import<list>
You meant #include :)
list<People::People> PP;

//person is a People
person.setName("Shane");

PP.push_back(person);

Now the person has been added to the linked list. I can get it out by
saying

person2 = PP.front();
cout << person2.getName();

but this is only if the person is at the front of the linked list.

Any suggestions? I know it's all there at
http://www.sgi.com/tech/stl/List.html in black and white. I just
don't understand it.


Don't worry... You will see the colors soon. :)

Here is a program that accesses the object of a list in three
different ways.

1) Explicit for loop is a little wordy. It requires that the iterator
type of the container be spelled out.

2) The for_each algorithm communicates the intent better but requires
that the actual job defined in a function (dumpPerson below) or a
functor.

3) The copy algorithm is strange but tells us how dumping a collection
of objects can be thought of copying them to an output stream through
an output stream iterator (ostream_iterator).

#include <list>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

class Person
{
string name_;

friend ostream & operator<< (ostream &, Person const &);

public:

explicit Person(string const & name)
:
name_(name)
{}
};

ostream & operator<< (ostream & os, Person const & person)
{
return os << person.name_;
}

// typedefs help with readability and give flexibility
typedef list<Person> People;
typedef People::const_iterator PeopleConstIter;

void with_for_loop(People const & people)
{
for (PeopleConstIter it = people.begin();
it != people.end();
++it)
{
// Accessing the object with the operator*
cout << *it << '\n';
}
}

void dumpPerson(Person const & person)
{
cout << person << '\n';
}

void with_for_each_algorithm(People const & people)
{
for_each(people.begin(), people.end(), dumpPerson);
}

void with_copy_algorithm(People const & people)
{
copy(people.begin(),
people.end(),
ostream_iterator<Person>(cout, "\n"));
}

int main()
{
// Build the list
People people;
people.push_back(Person("Ali"));
people.push_back(Person("Veli"));

// Output with three different methods
with_for_loop(people);
cout << '\n';

with_for_each_algorithm(people);
cout << '\n';

with_copy_algorithm(people);
cout << '\n';
}

Ali

Jul 22 '05 #5

P: n/a
Tons of help as always! Thanks for the point in the right direction to all
who answered. Good call on the #include :) I guess I've been drinking too
much Java lately, eh?

Shane
"Ali Cehreli" <ac******@yahoo.com> wrote in message
news:pa****************************@yahoo.com...
On Tue, 10 Aug 2004 23:16:15 -0700, Shane wrote:
I can get the forst element or the last, but I can't find one
in-between or pick one out like you would in an array by referencing
the element number.


The common way of accessing the elements of standard containers is to
use iterators returned from the begin() and end() member functions.

Briefly, iterators are generalized pointers that provide access to the
object they point to through operator* and get to the next object
through operator++.

std::list is not the right container if you need to access objects
randomly. A list of objects is accessed from beginning to the end in
order.

If you need random access as in an array, std::vector or std::deque
would be a better choice.
#import<list>


You meant #include :)
list<People::People> PP;

//person is a People
person.setName("Shane");

PP.push_back(person);

Now the person has been added to the linked list. I can get it out by
saying

person2 = PP.front();
cout << person2.getName();

but this is only if the person is at the front of the linked list.

Any suggestions? I know it's all there at
http://www.sgi.com/tech/stl/List.html in black and white. I just
don't understand it.


Don't worry... You will see the colors soon. :)

Here is a program that accesses the object of a list in three
different ways.

1) Explicit for loop is a little wordy. It requires that the iterator
type of the container be spelled out.

2) The for_each algorithm communicates the intent better but requires
that the actual job defined in a function (dumpPerson below) or a
functor.

3) The copy algorithm is strange but tells us how dumping a collection
of objects can be thought of copying them to an output stream through
an output stream iterator (ostream_iterator).

#include <list>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

class Person
{
string name_;

friend ostream & operator<< (ostream &, Person const &);

public:

explicit Person(string const & name)
:
name_(name)
{}
};

ostream & operator<< (ostream & os, Person const & person)
{
return os << person.name_;
}

// typedefs help with readability and give flexibility
typedef list<Person> People;
typedef People::const_iterator PeopleConstIter;

void with_for_loop(People const & people)
{
for (PeopleConstIter it = people.begin();
it != people.end();
++it)
{
// Accessing the object with the operator*
cout << *it << '\n';
}
}

void dumpPerson(Person const & person)
{
cout << person << '\n';
}

void with_for_each_algorithm(People const & people)
{
for_each(people.begin(), people.end(), dumpPerson);
}

void with_copy_algorithm(People const & people)
{
copy(people.begin(),
people.end(),
ostream_iterator<Person>(cout, "\n"));
}

int main()
{
// Build the list
People people;
people.push_back(Person("Ali"));
people.push_back(Person("Veli"));

// Output with three different methods
with_for_loop(people);
cout << '\n';

with_for_each_algorithm(people);
cout << '\n';

with_copy_algorithm(people);
cout << '\n';
}

Ali

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.