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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:23+00:00 2026-06-12T12:35:23+00:00

I have a Visual Studio 2008 C++03 application where I would like to create

  • 0

I have a Visual Studio 2008 C++03 application where I would like to create a std::map that uses the iterator from another std::map as its key-type. But, I run in to a problem when I try to erase an element from that map using its key-type.

In this example, when an element in MyList gets to be more than 5 minutes old, a timer should fire and removes it from the map and destroy its age-timer.

typedef std::map< Foo, FooBar > MyList;
typedef std::map< MyList::iterator, boost::shared_ptr< Timer > > MyListAgeTimers;

class A
{
public:

    void AddItem( Foo& f, FooBar& fb )
    {
        CriticalSection::Guard g( lock_ );
        std::pair< MyList::iterator, bool > new_foo = 
            my_list_.insert( std::make_pair( f, fb ) );
        if( new_foo.second )
        {
            timers_.insert( std::make_pair( 
                new_foo.first, 
                boost::make_shared< Timer >( FiveMinutes, boost::bind( &A::OnAgeTimer, this, new_foo.first ) ) ) );
        }
    };

    void OnAgeTimer( MyList::iterator item )
    {
        CriticalSection::Guard g( lock_ );

        // erase by key-type generates an error:
        // functional(143) : error C2676: binary '<' : 'const std::_Tree<_Traits>::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator
        timers_.erase( item );

        // erase by iterator. works okay.
        my_list_.erase( item );
    };

private:
    MyList my_list_;
    MyListAgeTimers timers_;
    CriticalSection lock_;
};

Can you not use the iterator from one map as the key-type to another? Or, do I need to define a specialized operator< for this?

EDIT:

The obvious thing to do (to me) is just this:

namespace std {
inline bool operator <( const MyList::iterator& a, const MyList::iterator& b )
{
    return a->first < b->first;
}
};

But, if that’s correct, why is that not the default behavior for the std::operator< when comparing two iterators?

  • 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-12T12:35:24+00:00Added an answer on June 12, 2026 at 12:35 pm

    std::map<key, value> requires that key have an operator<; that’s what the map object uses to find a matching key. std::map<x, y>::iterator is a bidirectional iterator; it does not have an operator<, so you can’t use it as the key type in another map unless you provide your own operator< or a function object to compare two iterators and decide which one comes before the other.

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

Sidebar

Related Questions

I have a Visual Studio 2008 C++03 application that uses Lua 5.2.1. I would
I have a Visual Studio 2008 C++ application that does something like this: template<
I have a Visual Studio 2008 C++ application where I would like to copy
I have a Visual Studio 2008 C++03 application where I would like to initialize
I have a Visual Studio 2008 C++03 application where I would like to have
I have a Visual Studio 2008 C++ application where I would like to replace
I have a Visual Studio 2008 C++ application where I would like to insert
I have a Visual Studio 2008 C++ application where I would like to convert
I have a Visual Studio 2008 C++03 application where I would like to use
I have a GUI C++ application (Visual Studio 2008) that needs to be converted

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.