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 9223777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:09:15+00:00 2026-06-18T04:09:15+00:00

Based on the implementation of Iter , I have difficulties to understand operator!= and

  • 0

Based on the implementation of Iter, I have difficulties to understand operator!= and don’t understand why it doesn’t check _p_vec?

This is the suggested implementation of operator!= that ONLY compares the _pos.

class Iter
{
    public:
    Iter (const IntVector* p_vec, int pos)
        : _pos( pos )
        , _p_vec( p_vec )
    { }

    // these three methods form the basis of an iterator for use with
    // a range-based for loop
    bool operator!= (const Iter& other) const
    {
        return _pos != other._pos;
    }
    ...
    ...
    private:
        int _pos;
        const IntVector *_p_vec;
};

However, I think the correct way to do this as follows. In other words, we have to compare both _pos and _p_vec.

bool Iter::operator!= (const Iter& other) const
{
    return _p_vec != other._p_vec || _pos != other._pos;
}

Question> Whose code is correct?

===Update on how std::vector works on comparison of iterator====

std::vector<int> vecOne { 1, 2, 3};
std::vector<int> vecTwo { 4, 5, 6};

auto iterOne = vecOne.begin();
std::advance(iterOne, 1);

auto iterTwo = vecTwo.begin();
std::advance(iterTwo, 1);

if ( iterOne == iterTwo)
    std::cout << "iterOne == iterTwo" << std::endl;
else
    std::cout << "iterOne != iterTwo" << std::endl;

Output is : iterOne != iterTwo

However,

std::vector<int> foo;
std::vector<int> bar;

if ( foo.begin() == bar.begin() )
    std::cout << "foo.begin() == bar.begin()" << std::endl;
else
    std::cout << "foo.begin() != bar.begin()" << std::endl;

Output is : foo.begin() == bar.begin()

GCC (version 4.7.1)

  • 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-06-18T04:09:17+00:00Added an answer on June 18, 2026 at 4:09 am

    Also comparing the underlying container reference is an improvement to make sure that, for example, the begin() iterators of two different containers compare equal.

    However, iterators of different containers are rarely compared (when using STL algorithms, they never will). So this might be considered an optimization. Think of a loop in which you step from begin() to end(), so the only iterators you are comparing are the “current” one and end(), which are of the same container.

    Comparing iterators of standard containers (vector, etc.) is considered undefined behavior. Indeed, since they never are compared in (good) code (for example the standard algorithms), it doesn’t have to define a behavior for such cases.

    So the author of this Iter class might also want to come back to this point and say: “Comparing iterators of different vectors is undefined behavior.”

    This being said, if you want to ensure that you never compare iterators of different containers, leave out this check and maybe assert it for debug builds (but don’t check it in release builds).

    A good example where you might want to compare iterators of different containers is for linked lists, where you define the end of the list being a null pointer (or a pointer to a static null item instance). Consider the iterator being a pointer to a linked list item; in this case, the end() iterator is just a null pointer (or points to this null item). It is the same one for all containers, so you can’t implement the check in your comparison method in such cases. Maybe this is one of the reasons why such a comparison isn’t defined — it should return “unequal” when comparing end iterators of different lists, but it can’t.

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

Sidebar

Related Questions

I followed this tutorial for drag and drop listview implementation based on component that
It's clear that Apple has an OpenCL implementation based on Clang and LLVM. There's
My understanding is that Java's implementation of regular expressions is based on Perl's. However,
I have developed an array based implementation of a hashTable with several stock names,
Is there any file system based B+ Tree implementation in c#(open source). I have
What is the best way to have your own home-grown Java-based implementation of Aspect-Oriented-Programming?
This is my first question on this site, and I only have a basic
If I understand correctly, every WebView based implementation needs WebViewClient . But looking at
Is there any speed/space/general performance gains in using array based implementation of a binary
Is anyone aware of a pure, Javascript based implementation of the directional flow diagrams

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.