Use if Iterator

This is a discussion on Use if Iterator within the C++ Programming forums, part of the General Programming Boards category; 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 = ...

  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,893
    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,344
    >> 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,893
    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
    Posts
    22,915
    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
    22,157
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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.

Page 1 of 2 12 LastLast
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, 09:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-20-2008, 12:16 AM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 09:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM

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