•
•
•
•
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
![]() |
•
•
Join Date: Sep 2006
Location: Michigan State University, United States
Posts: 232
Reputation:
Rep Power: 3
Solved Threads: 1
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:
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
Any help is appreciated.
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!
•
•
Join Date: Dec 2006
Location: india
Posts: 1,084
Reputation:
Rep Power: 9
Solved Threads: 163
if the iterator is a random_access iterator one + 1 is supported.
if not, you could write
if you want to figure out what kind of iterator it is, use std::iterator_traits<>. for example
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
c++ Syntax (Toggle Plain Text)
#include <iterator> #include <iostream> #include <vector> #include <list> using namespace std ; template< typename iterator_type > inline void bar( iterator_type iterator, random_access_iterator_tag ) { cout << "random access iterator!\n" ; } template< typename iterator_type > inline void bar( iterator_type iterator, bidirectional_iterator_tag ) { cout << "bidirectional iterator!\n" ; } // etc template< typename iterator_type > inline void foo( iterator_type iterator ) { bar( iterator, typename iterator_traits<iterator_type>::iterator_category() ) ; } int main() { vector<int> vec ; list<int> lst ; cout << "vector<int>::reverse_iterator: " ; foo( vec.rbegin() ) ; cout << "list<int>::iterator: " ; foo( lst.begin() ) ; }
Last edited by vijayan121 : Dec 3rd, 2007 at 9:27 pm.
•
•
Join Date: Sep 2006
Location: Michigan State University, United States
Posts: 232
Reputation:
Rep Power: 3
Solved Threads: 1
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: iterator error of unknown meaning - STL map
- Next Thread: Polymorphism 2



Linear Mode