question on iterators

Started by
9 comments, last by wild_pointer 21 years, 6 months ago
If I have something like
    
for( std::vector<std::string>::iterator v_iter = some_vec.begin(); v_iter < some_vec.end(); v_iter ++ )
    {
        // Stuff

    }
    
How would I figure out which iteration I am on? I suppose I could declare a counter and increment it each time, but that seems redundant. [My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people [edited by - wild_pointer on September 29, 2002 4:31:47 AM]
[size=2]
Advertisement
a) std::vector<std::string>::difference_type d = v_iter - some_vec.begin(); but that's only possible for Random Iterators (such as those for arrays, vectors and deque). Or use distance

b) calling some_vec.end() at each iteration is inefficient.

c) Prefer standard algorithms : for_each( some_vec.begin(), some_vec.end(), DoStuff ); where DoStuff it a function that is the body of your loop.

d) Or use a function object for better performance


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on September 29, 2002 5:05:32 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by wild_pointer
How would I figure out which iteration I am on?

Ancillary to Fruny''s comments, you usually don''t need to know which iteration you''re on. If you absolutely must, you may use the expression ( v_iter - vec.begin() ), but only for random iterators or forward iterators in a strictly forward progressing loopm and only if the sequence held by the container is not modified.

All the conditions make it obvious that an alternative solution is a better idea.
I see... I guess I''ll rethink what I''m doing. Thank you.


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
what was your reason for wanting to know?

if you need to know - of course put a counter in

but it might not be necessary to know

also prefer ++v_iter as it doesn''t make a copy of the iterator and is more efficient.

peace
quote:Original post by wild_pointer
v_iter < some_vec.end();

Bad, bad! *slaps wrists*

Use v_iter != some_vec.end(); instead. Otherwise when you start using list iterators you''ll get into trouble, as asking whether one is ''less than'' another is not guaranteed to give you the correct order.



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
quote:Original post by Fruny
b) calling some_vec.end() at each iteration is inefficient.
You sure about that? I thought x.end() for any container is just some inlined function that returns a member variable and calculates nothing. So it''s as fast as it gets (unless the STL implementation is crappy, or I''m missing something).
It has to construct the iterator and return it by value each and every time.

That''s probably less than good.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
For readability, you should probably stick to increment/decrement counters. Plus theres the performance issue that a cache''d loop counter would be faster than some Accessor function.
william bubel
If you absolutely must:


  using namespace std;vector<string>::iterator iter = some_vec.begin();vector<string>::iterator end = some_vec.end();for(int i=0;iter!=end;++iter,++i){  // i will now represent the current iteration}  
daerid@gmail.com

This topic is closed to new replies.

Advertisement