Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1027393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:14:05+00:00 2026-05-16T12:14:05+00:00

I have an stl iterator resulting from a std::find() and wish to test whether

  • 0

I have an stl iterator resulting from a std::find() and wish to test whether it is the last element. One way to write this is as follows:

mine *match = someValue;
vector<mine *> Mine(someContent);
vector<mine *>::iterator itr = std::find(Mine.begin(), Mine.end(), match);

if (itr == --Mine.end()) {
  doSomething;
}

But it seems to me that decrementing the end() iterator is asking for trouble, such as if the vector has no elements, then it would be undefined. Even if I know it will never be empty, it still seems ugly. I’m thinking that maybe rbegin() is the way to go, but am not certain as to best way to compare the forward iterator with a reverse iterator.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:14:05+00:00Added an answer on May 16, 2026 at 12:14 pm

    Do this:

    // defined in boost/utility.hpp, by the way
    template <typename Iter>
    Iter next(Iter iter)
    {
        return ++iter;
    }
    
    // first check we aren't going to kill ourselves
    // then check if the iterator after itr is the end
    if ((itr != Mine.end()) && (next(itr) == Mine.end()))
    {
        // points at the last element
    }
    

    That is all. Never gives you undefined behavior, works on all iterators, good day.

    Wrap it up for fun:

    template <typename Iter, typename Cont>
    bool is_last(Iter iter, const Cont& cont)
    {
        return (iter != cont.end()) && (next(iter) == cont.end())
    }
    

    Giving:

    if (is_last(itr, Mine))
    

    If you’re allergic to utility functions/nice looking code, do:

    if ((itr != Mine.end()) && (itr + 1 == Mine.end()))
    

    But you can’t do it on non-random-access iterators. This one works with bidirectional iterators:

    if ((itr != Mine.end()) && (itr == --Mine.end()))
    

    And is safe since end() > itr by the first check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with returning an iterator from an STL map using an
In c++ STL, if I have an iterator it into a vector v ,
If i have a stl map from string to int and i want to
Have you used a checked STL implementation? Did it find bugs you were not
I'm trying to convert an iterator class I have to be stl compatible so
Right now, I have this code: bool isAnyTrue() { for(std::list< boost::shared_ptr<Foo> >::iterator i =
I have a function which searches an STL container then returns the iterator when
Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter
In one of my rules a have qi::locals<> with stl container as parameter. When
I am writing code in VS2005 using its STL. I have one UI thread

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.