Skip to main content
gamedev.net
🔒 Locked

iterators: operators

Started by AngelForce 7 replies 750+ views
Original Post
AngelForce
AngelForce
Ok, to me it seems, that you can only use the operator++ (and -- in bidirectional iterators) when dealing with iterators... But what should I do, when I want to access every second element of a list?? with vector I could write
        
std::vector<int> vec;
int i;
for(i=0; i<what_ever; i+=2)
    vec[i] = what_ever_2;
       
but a list doesn't have random access iterators and I want a way to solve the problem with iterators like
    
std::vector<int> vec;
std::vector<int>::iterator it;
for(it=vec.begin(); it!=vec.end();//vec.end() alone wouldn't work     but that's

                        //not the point

                       it+=2)
    *it = what_ever_2;
      
Is there a way, or would i have to use:
  
for(it=vec.begin(); it!=vec.end(); ++it)
{
     ++it;
     *it = what_ever_ever_ever;
}
      
????? Plz help me and thx for your answers! AngelForce EDIT: Fixed a source tag EDIT: these stupid tags... -- If you find any mistakes, you're allowed to keep 'em! 'When I look back I am lost.' [edited by - AngelForce on December 17, 2002 2:20:33 PM] [edited by - AngelForce on December 17, 2002 2:22:26 PM] [edited by - AngelForce on December 17, 2002 2:44:26 PM]
AngelForce--"When I look back I am lost." - Daenerys Targaryen
Sneftel
Sneftel
For list iterators, there's no shortcut for skipping forwards; iteration is a O(n) operation. I would do this:

for(it=vec.begin(); it!=vec.end(); ++it, ++it)
{
*it = what_ever_ever_ever;
}

just because it's clearer. Look up the comma operator in a C++ book if you're unsure of its syntax.

EDIT: make sure you've got the it=vec.begin() there.


Don't listen to me. I've had too much coffee.

[edited by - sneftel on December 17, 2002 2:27:34 PM]
AngelForce
AngelForce
quote:
Original post by Sneftel
For list iterators, there''s no shortcut for skipping forwards; iteration is a O(n) operation. I would do this:

for(it=vec.begin(); it!=vec.end(); ++it, ++it)
{
*it = what_ever_ever_ever;
}

just because it''s clearer. Look up the comma operator in a C++ book if you''re unsure of its syntax.

EDIT: make sure you''ve got the it=vec.begin() there.


Don''t listen to me. I''ve had too much coffee.

[edited by - sneftel on December 17, 2002 2:27:34 PM]


Sorry for the not existing .begin()....I wrote it in haste(as we all do ), but I''ll edit it!

Thanks for your answer, but I''m looking for another way...I know that I can do this, but I''d like to know if there''s a way to access every second(or third, or 4th...) element in O(n) (when writing
 ++it, ++it 
, I would need twice as much time)
AngelForce--"When I look back I am lost." - Daenerys Targaryen
civguy
civguy
++++it

Anyway, if you want to skip more, just make your own lil' function for that

    
template<class T>
inline T& increment(T& t, int amount) {
for (int i=0; i<amount; ++i) {
++t;
}
return t;
}

for(it=vec.begin(); it!=vec.end(); increment(it, 10)) {}
(name it better than I did)

Or even (don't do this plz!)

  
template <class T, class A>
inline std::list<T,A>::iterator& operator+=(std::list<T,A>::iterator& it, int amount) {
for (int i=0; i<amount; ++i) {
++it;
}
return it;
}

for(it=lst.begin(); it!=lst.end(); it+=10) {}

Also read what Fruny says below

[edited by - civguy on December 17, 2002 2:55:06 PM]
Fruny
Fruny
quote:
Original post by Sneftel

for(it=vec.begin(); it!=vec.end(); ++it, ++it)
{
*it = what_ever_ever_ever;
}




Odd number of elements + test for end() with != → boom.

Not that you have a choice in your test...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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
DrPizza
DrPizza
quote:
Anyway, if you want to skip more, just make your own lil'' function for that

Is there something wrong with std::advance()?

But, no, such an approach is no good for this problem.

Because you have to handle odd-sized lists:

for(container::iterator it = container.begin(), end = container.end(); ; )
{
// do something with it
// advance once and test
++it;
if(it == end) { break; }
// advance again and test
++it;
if(it == end) { break; }
}


I would prefer not to put any advancing or testing in the for''s conditions, simply because I don''t like splitting them up.
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 ***/
civguy
civguy
quote:
Original post by DrPizza
Is there something wrong with std::advance()?
Yea: It''s existence is not known

AngelForce
AngelForce
quote:
Original post by civguy
[quote]Original post by DrPizza
Is there something wrong with std::advance()?
Yea: It''s existence is not known



  
//MS implementation:----------------------

// TEMPLATE FUNCTION advance

template<class _InIt, class _Diff> inline
void advance(_InIt& _Where, _Diff _Off)
{ // increment iterator by offset, arbitrary iterators

_Advance(_Where, _Off, _Iter_cat(_Where));
}

template<class _InIt, class _Diff> inline
void _Advance(_InIt& _Where, _Diff _Off, input_iterator_tag)
{ // increment iterator by offset, input iterators

for (; 0 < _Off; --_Off)
++_Where;
}

//SGI implementation:---------------------

template <class _InputIter, class _Distance>
inline void __advance(_InputIter& __i, _Distance __n, input_iterator_tag) {
while (__n--) ++__i;
}


Ok, they aren''t doing it different...thanks for your answers guys, now I know a little bit more!


AngelForce

--
If you find any mistakes, you''re allowed to keep ''em!

''When I look back I am lost.''
AngelForce--"When I look back I am lost." - Daenerys Targaryen
civguy
civguy
AngelForce, I didn''t mean it doesn''t exist or do the job correctly. I just meant I didn''t know about it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.