tags:

views:

48

answers:

4

i would like to improve my c++ code style so i decided that i definitively have to deep into stl...at first, as i need it in a real case, i woudl like to know if it is available some kind of container that hold a current index inside...

for example i mean some container class i can navigate with next()/prev() but i can also ask for get() to retrieve the current one , without the need to store the current index/pointer in my own class member

(already taken a look at stl vector/deque , hopefully i didn't read doc carefully)

+1  A: 

It sounds like you want an iterator.

An iterator is used with a container. It acts as a pointer to a position in the container, and you can increment (next) and decrement (prev) it.

JoshD
so you suggest instead to use it for loops i should declare an iterator in my class as member for example vector<int>::iterator my_it and let it too maintain the current position ...does it sound correct?
luca
@luca: Probably not a good idea for a vector, whose iterators get invalidated easily (the iterator is little more than a pointer into the vector's allocated array, which it may move to other place as it grows). Why not simply store the index?
UncleBens
@UncleBens: wouldn't storing the index have the same liability? It would become invalid after insertion or deletion just as an iterator would.
JoshD
It can become invalid, but not as easily as a vector iterator. Even after a `push_back`, the 3rd item is still the 3rd item.
UncleBens
@UncleBens: Unless I'm mistaken, a `push_back` doesn't invalidate an iterator (except one at `end()`). Like you said, the 3rd item is still the 3rd item; an iterator pointing to it should work just as well as a pointer. Have I missed something?
JoshD
`push_back` invalidates *vector's* iterators (if the buffer is reallocated, all iterators into a vector will be pointing to the old memory location).
UncleBens
+1  A: 

Yes, if I understand your question correctly you want to look into iterators. Here's an example:

std::vector<int> someVector;
// add elements to the vector here
// ...
std::vector<int>::iterator start = someVector.begin();
std::vector<int>::iterator end = someVector.end();
while(start != end)
{
    std::cout << *start << std::endl;
    ++start;
}

They behave similarly to pointers and all of the stl containers have iterators. Beware that there are different types of iterators (reverse, const, bidirectional, forward, random access, etc) and that they have different operations available to them.

Niki Yoshiuchi
+1  A: 

If I understand your needs correctly, then no, there is no stl container that stores an iterator to some sort of current element. But I can't see why you are reluctant to store that iterator yourself? Like this:

ContainerType cont;
ContainerType::iterator current;

now you can do ++current; --current (if the container has bidir iterators); and *current

Armen Tsirunyan
++ -- *, so concise ... so cool , thanks
luca
+1  A: 

The short answer is no. One basic design precept of the standard containers is to separate iteration from containment -- i.e., you always need a separate iterator to keep track of a current "spot" in any given container.

Containers that combined containment with iteration was quite common in older designs, but this makes it nearly impossible for different parts of the code to be iterating through a particular container at the same time.

Jerry Coffin
good point , thanks ".. but this makes it nearly impossible for different parts of the code to be iterating through a particular container at the same time...."
luca