| | |
Timesaving Tips
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
Hello,
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula
This line is interesting:
This works for vectors, but generally it's a bad programmer practice to use
relation operator < with iterators because it won't work with all containers.
Operator < is provided for random access iterators so this will not work with list
container.
I think, in order to write generic code, it's better practice to get used to uniform
approach in for different kind of containers and that's:
And BTW, there is no need to include header <iterator> in this case since iterator class
is nested in vector class.
So, finally, this code example is:
A small bonus:
Did you notice that some programmers use this form of for loop:
when i is int,
and this form:
when i is an iterator?
Notice prefix and postfix ++i and i++. Are these forms same when using in loops?
Answer is depends on what counter i is. If i is an integer then there is no difference,
but if i is iterator, then there could be difference. As you probably know iterator is
often an object i.e. instance of a class and in that case there is difference whether using
i++ or ++i form.
Look at this small example (clasical implementation of operator ++=:
As you can see postfix version include creation of new object so we can conclude
that prefix form is faster.
If this issue is discussed before, I apologize but it's good to know.
- Micko
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <iterator> using namespace std; int main() { int a[] = {1,2,3,4,5}; vector<int> v(a, a + 5); vector<int>::const_iterator it, end = v.end(); for (it = v.begin(); it < end; ++it) { cout << *it << endl; } }
C++ Syntax (Toggle Plain Text)
for (it = v.begin(); it < end; ++it)
relation operator < with iterators because it won't work with all containers.
Operator < is provided for random access iterators so this will not work with list
container.
I think, in order to write generic code, it's better practice to get used to uniform
approach in for different kind of containers and that's:
C++ Syntax (Toggle Plain Text)
for (it = v.begin(); it != end; ++it)
And BTW, there is no need to include header <iterator> in this case since iterator class
is nested in vector class.
So, finally, this code example is:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { int a[] = {1,2,3,4,5}; vector<int> v(a, a + 5); vector<int>::const_iterator it, end = v.end(); for (it = v.begin(); it != end; ++it) { cout << *it << endl; } }
A small bonus:
Did you notice that some programmers use this form of for loop:
C++ Syntax (Toggle Plain Text)
int i; for ( i = 0; i < 10; i++ ) /*do something*/
and this form:
C++ Syntax (Toggle Plain Text)
for ( i = v.begin (); i != v.end (); ++i ) /*do something*/
Notice prefix and postfix ++i and i++. Are these forms same when using in loops?
Answer is depends on what counter i is. If i is an integer then there is no difference,
but if i is iterator, then there could be difference. As you probably know iterator is
often an object i.e. instance of a class and in that case there is difference whether using
i++ or ++i form.
Look at this small example (clasical implementation of operator ++=:
C++ Syntax (Toggle Plain Text)
class Integer { int x; public: Integer ( int i ) : x ( i ) { }; Integer& operator ++ () //prefix { x++; return *this; } Integer operator ++ (int) //postfix { return Integer ( x++ ); } };
As you can see postfix version include creation of new object so we can conclude
that prefix form is faster.
If this issue is discussed before, I apologize but it's good to know.
- Micko
•
•
•
•
Originally Posted by Micko
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula
C++ Syntax (Toggle Plain Text)
for (it = v.begin(); it < end; ++it)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: OpenGL
- Next Thread: deleting spaces in an ansistring
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






