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