How to test if an iterator is at the end of the container

Discussion in 'C++' started by Enselic, Dec 27, 2006.

  1. Enselic

    Enselic Guest

    I have problems finding out how to test weather an iterator are at the
    end of its container.

    I'd like to do something like the following:

    std::list<int>::iterator it = intList.begin();
    while (it != intList.end()) {
    if (!it.has_next())
    std::cout << "This is at the last item in the container\n";

    std::cout << "Number: " << *it << std::endl;

    it++;
    }

    How would I do this the Best way? Am I looking past something obvious
    here?
    Enselic, Dec 27, 2006
    #1
    1. Advertising

  2. Enselic

    Berny Cantos Guest

    I use to write code like the following when facing this kind of tests:

    typedef std::vector<int> MyVector;
    typedef MyVector::const_iterator MyVectorCIt;

    MyVector vect;
    vect.push_back(10);
    vect.push_back(100);
    vect.push_back(1000);

    MyVectorCIt it = vect.begin();
    MyVectorCIt const end = vect.end();

    while(it != end) {
    int const& data = *it;
    ++it;
    if(it == end) std::cout << "This is the last item: ";
    std::cout << data << '\n';
    }

    Enselic ha escrit:
    > I have problems finding out how to test weather an iterator are at the
    > end of its container.
    >
    > I'd like to do something like the following:
    >
    > std::list<int>::iterator it = intList.begin();
    > while (it != intList.end()) {
    > if (!it.has_next())
    > std::cout << "This is at the last item in the container\n";
    >
    > std::cout << "Number: " << *it << std::endl;
    >
    > it++;
    > }
    >
    > How would I do this the Best way? Am I looking past something obvious
    > here?
    Berny Cantos, Dec 27, 2006
    #2
    1. Advertising

  3. Enselic

    Guest

    Enselic ÃÑÓáÊ:
    > I have problems finding out how to test weather an iterator are at the
    > end of its container.
    >
    > I'd like to do something like the following:
    >
    > std::list<int>::iterator it = intList.begin();
    > while (it != intList.end()) {
    > if (!it.has_next())
    > std::cout << "This is at the last item in the container\n";
    >
    > std::cout << "Number: " << *it << std::endl;
    >
    > it++;
    > }
    >
    > How would I do this the Best way? Am I looking past something obvious
    > here?


    if(it == intList.end()-1)
    , Dec 27, 2006
    #3
  4. Enselic

    Earl Purple Guest

    Enselic wrote:
    > I have problems finding out how to test weather an iterator are at the
    > end of its container.
    >
    > I'd like to do something like the following:
    >
    > std::list<int>::iterator it = intList.begin();
    > while (it != intList.end()) {
    > if (!it.has_next())
    > std::cout << "This is at the last item in the container\n";
    >
    > std::cout << "Number: " << *it << std::endl;
    >
    > it++;
    > }
    >
    > How would I do this the Best way? Am I looking past something obvious
    > here?


    There is no such thing as has_next() but you can just check if the
    "next" item is end().

    while ( it != intList.end() )
    {
    int x = *it++;

    std::cout << x;
    if ( it != intList.end() )
    {
    std::cout << ',';
    }
    }

    If your collection is of a class you can use a reference or const
    reference for a const_iterator. Thus:

    while ( it != fooList.end() )
    {
    const foo & f = *it++;
    //etc
    }

    Use a non-const reference if you have a non-const iterator and you are
    going to modify the objects or call their non-const methods.
    Earl Purple, Dec 27, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,416
  2. greg
    Replies:
    6
    Views:
    453
    Dietmar Kuehl
    Jul 17, 2003
  3. Vivi Orunitia
    Replies:
    11
    Views:
    4,466
    Martijn Lievaart
    Feb 4, 2004
  4. Replies:
    6
    Views:
    640
    Jim Langston
    Oct 30, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    690
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page