943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1861
  • C++ RSS
Oct 14th, 2005
0

Timesaving Tips

Expand Post »
Hello,
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int a[] = {1,2,3,4,5};
  10. vector<int> v(a, a + 5);
  11.  
  12. vector<int>::const_iterator it, end = v.end();
  13. for (it = v.begin(); it < end; ++it)
  14. {
  15. cout << *it << endl;
  16. }
  17. }
This line is interesting:
C++ Syntax (Toggle Plain Text)
  1. for (it = v.begin(); it < end; ++it)
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:
C++ Syntax (Toggle Plain Text)
  1. 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)
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int a[] = {1,2,3,4,5};
  9. vector<int> v(a, a + 5);
  10.  
  11. vector<int>::const_iterator it, end = v.end();
  12. for (it = v.begin(); it != end; ++it)
  13. {
  14. cout << *it << endl;
  15. }
  16. }

A small bonus:
Did you notice that some programmers use this form of for loop:
C++ Syntax (Toggle Plain Text)
  1. int i;
  2. for ( i = 0; i < 10; i++ )
  3. /*do something*/
when i is int,
and this form:
C++ Syntax (Toggle Plain Text)
  1. for ( i = v.begin (); i != v.end (); ++i )
  2. /*do something*/
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 ++=:
C++ Syntax (Toggle Plain Text)
  1. class Integer
  2. {
  3. int x;
  4. public:
  5. Integer ( int i ) : x ( i ) { };
  6.  
  7. Integer& operator ++ () //prefix
  8. {
  9. x++;
  10. return *this;
  11. }
  12.  
  13. Integer operator ++ (int) //postfix
  14. {
  15.  
  16. return Integer ( x++ );
  17. }
  18. };

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
Similar Threads
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Oct 14th, 2005
0

Re: Timesaving Tips

Quote originally posted by Micko ...
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula

C++ Syntax (Toggle Plain Text)
  1. for (it = v.begin(); it < end; ++it)
Oops. [Damn copy/paste coding habits.] Thanks for spotting that -- I'll fix that post.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: OpenGL
Next Thread in C++ Forum Timeline: deleting spaces in an ansistring





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC