A question about the "list" in C++ STL

X

xz

From the perspective of data structure, I thought it should be esay to
scan a linked list. You simply scan from the head, and scan head-
successor, and scan the successor's successor and so on.

However, for STL's list, I have not found a way to directly scan the
list.

So what can I do to scan a list from STL? Should I pop_front(), scan
it and push_back() it?

Or should I just choose another class?
 
N

newbarker

From the perspective of data structure, I thought it should be esay to
scan a linked list. You simply scan from the head, and scan head-


However, for STL's list, I have not found a way to directly scan the
list.

So what can I do to scan a list from STL? Should I pop_front(), scan
it and push_back() it?

Or should I just choose another class?

Have you encountered iterators? That's the way to scan through a
container (list,set,vector, etc.)

Pete
 
K

Keith Halligan

From the perspective of data structure, I thought it should be esay to
scan a linked list. You simply scan from the head, and scan head-


However, for STL's list, I have not found a way to directly scan the
list.

So what can I do to scan a list from STL? Should I pop_front(), scan
it and push_back() it?

Or should I just choose another class?

You use an iterator to scan through the list, and compare it to
list::end()

eg.

std::list<int> my_list;

my_list.push_back(10);
my_list.push_back(20);
my_list.push_back(30);

list<int>::iterator it = my_list.begin();

while (it != my_list.end())
{
// Whatever you need to do at each element, it points to each
element in the list.
it++;
}
 
K

Kai-Uwe Bux

xz said:
From the perspective of data structure, I thought it should be esay to
scan a linked list. You simply scan from the head, and scan head-
successor, and scan the successor's successor and so on.

What do you mean by "scan"? If you want to traverse the list, use iterators.
All STL containers present themselves as ranges that can be traversed by
means of iterators. The begin() member function returns an iterator to the
first element. Thus, you can do:

for ( ListType::iterator iter = my_list.begin();
iter != my_list.end(); ++iter ) {
do_something( *iter );
}

Alternatively,

foreach( my_list.begin(), my_list.end(), some_function_object );

or many more possibilities.

However, for STL's list, I have not found a way to directly scan the
list.

So what can I do to scan a list from STL? Should I pop_front(), scan
it and push_back() it?
No.

Or should I just choose another class?

Wouldn't change anything. Vector and deque also use the iterator interface.


Best

Kai-Uwe Bux
 
X

xz

You use an iterator to scan through the list, and compare it to
list::end()

eg.

std::list<int> my_list;

my_list.push_back(10);
my_list.push_back(20);
my_list.push_back(30);

list<int>::iterator it = my_list.begin();

while (it != my_list.end())
{
   // Whatever you need to do at each element, it points to each
element in the list.
   it++;

}
One more question:

Is this *it* just like a real pointer. In other words, if I have

int * a = .....

may I do the following:

if((int *) it == a) {
...
}
 
J

Jeff Schwab

xz said:
One more question:

Is

[ a standard list iterator ]
just like a real pointer. In other words, if I have

int * a = .....

may I do the following:

if((int *) it == a) {
...
}

Nope. Try:

assert(it != my_list.end());

if (&*it == a) {
// ...
}
 
M

Michael.Boehnisch

However, for STL's list, I have not found a way to directly scan the
list.

two alternatives come to mind:

#include <list>
#include <iostream>
#include <algorithm>

int main() {

// example list
int init[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
std::list<int> mylist( init, init + 10 );

// use iterator or const_iterator and a for loop.
for ( std::list<int>::iterator p = mylist.begin(); p !=
mylist.end(); ++p ) {
std::cout << *p << ' ';
}
std::cout << std::endl;

// use STL style: functor and std::for_each()
class myfunctor {
public:
void operator() ( int x ) { std::cout << x << ' '; }
};

std::for_each( mylist.begin(), mylist.end(), myfunctor() );
std::cout << std::endl;

return 0;
}

best,

Michael
 

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

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top