473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Linked List Help please

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
5 1789
> #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

"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
11
by: fighterman19 | last post by:
because in linked list each node contains only one digit like curr->nodevalue_1= 0 curr->nodevalue_2=1 sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2 so when the number go up to >10...
1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
6
by: Rylios | last post by:
I am trying to make a very basic text editor using a linked list, fstream, sorting... This is what i have so far...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.