Thread: Use if Iterator

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Use if Iterator

    Considering this example...
    Code:
      vector<int> the_vector;
      vector<int>::iterator the_iterator;
    
      for( int i=0; i < 10; i++ )
        the_vector.push_back(i);
    
      int total = 0;
    
      the_iterator = the_vector.begin();
    
      while( the_iterator != the_vector.end() ) 
      {
        total += *the_iterator;
        the_iterator++;
      }
      
     cout << "Total=" << total << endl;
    The dereferencing "*the_iterator" is used... Is this another way of accessing the_vector contents? Why not just use the_vector to add to total?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can pass the iterator to algorithms, and they don't care what container it came from.

    The little program you wrote could be written thus:
    Code:
    #include <vector>
    #include <numeric>
    #include <iostream>
    #include <boost/iterator/counting_iterator.hpp>
    
    std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(10));
    int total = std::accumulate(v.begin(), v.end(), 0);
    std::cout << "Total = " << total << '\n';
    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
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by CornedBee View Post
    You can pass the iterator to algorithms, and they don't care what container it came from.

    The little program you wrote could be written thus:
    Code:
    #include <vector>
    #include <numeric>
    #include <iostream>
    #include <boost/iterator/counting_iterator.hpp>
    
    std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(10));
    int total = std::accumulate(v.begin(), v.end(), 0);
    std::cout << "Total = " << total << '\n';
    I'm not sure how this answers my Q.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A non-iterator method of solving the problem would be:
    Code:
    ...
      int total = 0;
    
      for(int i = 0; i < the_vector.size(); i++)
        total += the_vector.at(i);    // or the_vector[i]
    As a side note, I prefer to write iterator loops using for:
    Code:
      for(the_iterator = the_vector.begin(); the_iterator != the_vector.end(); the_iterator++)
      {
        total += *the_iterator;
      }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    A non-iterator method of solving the problem would be:
    Code:
    ...
      int total = 0;
    
      for(int i = 0; i < the_vector.size(); i++)
        total += the_vector.at(i);    // or the_vector[i]
    --
    Mats
    k, cool, this is what i was asking... thnx

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm not sure how this answers my Q.

    It shows "another way of accessing the_vector contents".

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    More importantly, it justifies the existence of iterators.
    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

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Why use the boost iterators in the example of CornedBee
    Code:
    std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(10));
    I only know boost a little, and cannot find good documentation about it (like Meyers or Josuttis wrote books on STL). Is it performance? Portability? In this case I cannot think of any difference..

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by MarkZWEERS View Post
    Why use the boost iterators in the example of CornedBee

    I only know boost a little, and cannot find good documentation about it (like Meyers or Josuttis wrote books on STL). Is it performance? Portability? In this case I cannot think of any difference..
    TO add to your questions, is it Metaprogramming?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is not meta programming.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    please.. answer my question as well... :-)

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why use the boost iterators in the example of CornedBee
    One reason is that you avoid writing an explicit loop and thus think in terms of the result, not the process. This is a form of abstraction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, to demonstrate that iterators offer greatly varying capabilities behind the same interface. Consider: the vector's iterators and the counting iterators have, as far as reading them is concerned, the same interface. You can pass them to algorithms in exactly the same way.

    Oh, and yes, passing the two iterators to the vector constructor is indeed potentially faster than a push_back loop, since it only allocates memory once. The push_back loop might allocate more than once. (With 10 elements, though, it's unlikely.)
    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

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Nice! Very nice!! From now on I'll use boost everywhere.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Quote Originally Posted by MarkZWEERS View Post
    Nice! Very nice!! From now on I'll use boost everywhere.
    This is purely my personal opinion. I would suggest you stop immediately with this mindset. Boost is a very great tool like STL. However, it is a very large library and may not be ideal for every environment that you may encounter in your software development life. Also, considering that it sounds like you are just starting out in C++ I personally think it would be to your best interest to try to do things yourself so that you'll have a better understanding of how things work in C++. You will inevitably encounter a situation where Boost will expect you to know some basic C++ syntax-related stuff that you failed to see by relying on Boost too much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-19-2008, 11:16 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM