Linked List Help please

S

Shane

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
 
P

Phlip

Shane said:
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.
 
G

Gernot Frisch

#import said:
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
 
S

Siemel Naran

Shane said:
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);
#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.
 
A

Ali Cehreli

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
 
S

Shane

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top