| | |
iterator addition - How does it work?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
C++ Syntax (Toggle Plain Text)
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 computer! It ate everything!
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: iterator error of unknown meaning - STL map
- Next Thread: Polymorphism 2
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game generator getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






