User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,795 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 784 | Replies: 3
Reply
Join Date: Sep 2006
Location: Michigan State University, United States
Posts: 232
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

iterator addition - How does it work?

  #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:
for( iterator_type one ; /*...*/)
{
       for( iterator_type two = one + 1 ; /*...*/)
       {
/*...*/
        }
}

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 the man! Save the Empire!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,084
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 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: iterator addition - How does it work?

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

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  
Join Date: Sep 2006
Location: Michigan State University, United States
Posts: 232
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: iterator addition - How does it work?

  #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 the man! Save the Empire!
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,084
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 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: iterator addition - How does it work?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC