Thread: iterators

  1. #1
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012


    I have the following code
    //p.345 ex1
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <list>
    #include <vector>
    #define RND_RANGE 10000
    #define TIMES (RND_RANGE)
    using namespace std;
    int main(void){
        //generate 10,000 numbers between 0 and 9,999
        list<int> the_list;
        list<int>::iterator iter;
        vector<int> freqs;
        vector<int>::iterator p;
        //place in list<int> container
        for(int i = 0; i < TIMES; i++){
            int j = (rand() % RND_RANGE);
        //iterate over values and display them
        int x = 0;
        for(iter = the_list.begin(); iter < the_list.end(); iter++){
            cout << "item: " << x << " = " << *iter << "..." << endl;
            if(x % 10 == 0)
        //print the median value
        //computer number freqencies
        return 0;
    when I comile it i get an error namely
    /usr/include/c++/4.7/bits/stl_list.h:1603:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
    /usr/include/c++/4.7/bits/stl_list.h:1603:5: note:   template argument deduction/substitution failed:
    p345no1.cpp:33:51: note:   ‘std::list<int>::iterator {aka std::_List_iterator<int>}’ is not derived from ‘const std::list<_Tp, _Alloc>’
    is what I'm thinking is most significant.

    My question is it not possible to use
    for(iter = the_list.begin(); iter < the_list.end(); iter++)
    do you have to use the synatx
    for(iter = the_list.begin(); iter != the_list.end(); iter++)
    why is that? I assumed since i was iterating over it like a pointer using the increment operators it was probably stored in contiguous memory. Could have sworn I saw the < and > operators used to test it before.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Until one of the resident experts responds, I think you will have to use (at least try this to see if it fixes your compile error):

    for(iter = the_list.begin(); iter != the_list.end(); iter++)
    I don't think that LIST overloads < and > operators (I'm not even sure if this would be possible).

  3. #3
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    thanks rcgldr. That's what i used to get it to compile and run. I thought maybe the same as you too however I wanted to find out if that was truly the case. Thanks again.

  4. #4
    Registered User
    Join Date
    Oct 2006
    two things that are worth pointing out:

    1. iterators are not pointers
    2. lists are not guaranteed, nor are they likely to have contiguous storage, due to their typical implementation as a doubly linked list.

    there's no way to know, without iterating through the remainder of the list. whether a particular node is less than the last+1 node. that is why the list iterator does not implement > or < operators.

    Edit: it should also be noted that it is never correct to say <= list.end(), even if it was possible to do so. end() returns the iterator that is one past the last element. you're going to run into all sorts of trouble if you try to use end() as the last element.
    Last edited by Elkvis; 05-13-2013 at 06:45 AM. Reason: additional info
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    You should declare variables near first use. The iterator, for example, only needs be in the loop.
    You can also make it easier by using "auto", eg auto it = the_list.begin() (C++11 feature).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with C# iterators
    By dudeomanodude in forum C# Programming
    Replies: 1
    Last Post: 04-28-2008, 03:23 AM
  2. Iterators
    By manutd in forum C++ Programming
    Replies: 5
    Last Post: 12-17-2006, 11:13 AM
  3. Iterators
    By achy1729 in forum C++ Programming
    Replies: 8
    Last Post: 09-06-2006, 04:06 AM
  4. iterators
    By Raven Arkadon in forum C++ Programming
    Replies: 6
    Last Post: 06-28-2005, 05:44 PM
  5. iterators
    By strickey in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2005, 12:45 PM