iterator addition - How does it work?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

iterator addition - How does it work?

 
0
  #1
Dec 3rd, 2007
Great, more iterator trouble.

First we have an iterator of any value. I want to make a second iterator that starts one higher than the first one. Look-see:
  1. for( iterator_type one ; /*...*/)
  2. {
  3. for( iterator_type two = one + 1 ; /*...*/)
  4. {
  5. /*...*/
  6. }
  7. }

That's the gist, but I know this way doesn't work. How would I do it? I would try the ++ operator on one instead of one + 1, but wouldn't that change the value of one?

Any help is appreciated.
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: iterator addition - How does it work?

 
0
  #2
Dec 3rd, 2007
if the iterator is a random_access iterator one + 1 is supported.
if not, you could write
  1. for( iterator_type one ; /*...*/)
  2. {
  3. iterator_type two = one ;
  4. for( ++two ; /*...*/)
  5. {
  6. /*...*/
  7. }
  8. }

if you want to figure out what kind of iterator it is, use std::iterator_traits<>. for example
  1. #include <iterator>
  2. #include <iostream>
  3. #include <vector>
  4. #include <list>
  5. using namespace std ;
  6.  
  7. template< typename iterator_type > inline
  8. void bar( iterator_type iterator, random_access_iterator_tag )
  9. { cout << "random access iterator!\n" ; }
  10.  
  11. template< typename iterator_type > inline
  12. void bar( iterator_type iterator, bidirectional_iterator_tag )
  13. { cout << "bidirectional iterator!\n" ; }
  14. // etc
  15.  
  16. template< typename iterator_type >
  17. inline void foo( iterator_type iterator )
  18. {
  19. bar( iterator,
  20. typename iterator_traits<iterator_type>::iterator_category()
  21. ) ;
  22. }
  23.  
  24. int main()
  25. {
  26. vector<int> vec ;
  27. list<int> lst ;
  28. cout << "vector<int>::reverse_iterator: " ;
  29. foo( vec.rbegin() ) ;
  30. cout << "list<int>::iterator: " ;
  31. foo( lst.begin() ) ;
  32. }
Last edited by vijayan121; Dec 3rd, 2007 at 9:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: iterator addition - How does it work?

 
0
  #3
Dec 3rd, 2007
Thanks again; that one was a little to simple for me to be so stuck on ^_^U

How do you know so much about iterators, anyways? You must need to use them all the time if you can so quickly come up with all the answers.
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: iterator addition - How does it work?

 
0
  #4
Dec 3rd, 2007
> How do you know so much about iterators, anyways? You must need to use them all the time
a c++ programmer would use iterators extensively. and so gets to be quite familiar with them.
Last edited by vijayan121; Dec 3rd, 2007 at 9:31 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC