Timesaving Tips

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Timesaving Tips

 
0
  #1
Oct 14th, 2005
Hello,
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula
  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:
  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:
  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:
  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:
  1. int i;
  2. for ( i = 0; i < 10; i++ )
  3. /*do something*/
when i is int,
and this form:
  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 ++=:
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Timesaving Tips

 
0
  #2
Oct 14th, 2005
Originally Posted by Micko
I've been reading Narue's Timesaving Tips and found this code from Dave Sinkula

  1. for (it = v.begin(); it < end; ++it)
Oops. [Damn copy/paste coding habits.] Thanks for spotting that -- I'll fix that post.
"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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC