for, while with Vectors

This is a discussion on for, while with Vectors within the C++ Programming forums, part of the General Programming Boards category; My question is this, why would I prefer one over the other ? Code: vector<int> v(10); vector<int>::iterator p; int i ...

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    for, while with Vectors

    My question is this, why would I prefer one over the other ?
    Code:
    vector<int> v(10);
    vector<int>::iterator p;
    
    int i = 0;
    
    p = v.begin();
    while( p != v.end() ) {
    *p = i;
    cout << *p << " ";
    p++;
    i++;
    }
    versus this.
    Code:
    vector<int>v(10);
    vector<int>::iterator p;
    int i = 0;
    
    for( p = v.begin(); p != v.end(); p++ ) {
    *p = i;
    cout << *p << " ";
    i++;
    }
    Is it best to use a while loop when dealing with containers?
    big146

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The second is clearer, that's why you would prefer it. It's what the for-loop is for, after all. You have initialization, condition and incrementation all in one place, instead of three places like with the while-loop.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    So synaticly both versions do the same thing on the container vector? I was'nt sure if the for loop had some upredictable side effect. The book Im going through right now( STL programing from the ground up ) uses the while loop to intialize the vector and display the contents. It also demonstrates the for loop but not with iterators. It indexes them like an array.

    thanks for the reply!
    big146

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    To simplify this difficult decision, it is recommended to use the for_each algorithm instead of user written loops.

    mfg JJ

  5. #5
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Ya i seen that too.
    Code:
    for_each( v.begin(), v.end(), show )
    That is next

    I was just curious about why the while loop was used so much in the book im reading through...so i thought maybe there was a good reason for it.
    big146

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    No, I think that's mainly the author's preference (and a stupid one at that...).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21