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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:25:58+00:00 2026-06-12T13:25:58+00:00

I need to add, store and delete some pairs of objects, e.g. Person –

  • 0

I need to add, store and delete some pairs of objects, e.g. Person–Hobby. Any person can have several hobbies and several persons can have the same hobby. So, multimap is a good container, right?

Before adding a pair I need to know, if it’s not added yet. As I can see here there is no standard class-method to know, if the concrete pair e.g. Peter-Football exists in the MM. Thus, I’ve written a method which returns a positive integer (equal to the distance between mm.begin() and pair iterator) if the pair exists and -1 otherwise.

Then I need to delete some pair. I call my find method, which returns, some positive integer. I call myMultiMap.erase(pairIndex); but the pair is not being deleted for some reason. That is my problem. Obviously the erase method needs an iterator, not the int. The question is: how do I convert an integer to an iterator?

Thanks!

UPDATE:
I’ve tried this c.begin() + int_value but got an error error: no match for ‘operator+’ on this line….

  • 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-12T13:26:00+00:00Added an answer on June 12, 2026 at 1:26 pm

    Not that I favour your approach, but if the int is distance between begin() and the iterator in question, you can just use

    c.begin() + int_value
    

    or

    std::advance(c.begin(), int_value)
    

    to get the iterator. The second version is needed for iterators which are not random-access-iterators.

    In the interest of your personal sanity (and the program’s speed), I’d suggest you return the iterator directly in some form.

    There are many possible interfaces that solve this one way or the other. What I would call the “old C way” would be returning by an out parameter:

    bool find_stuff(stuff, container::iterator* out_iter) {
     ...
     if(found && out_iter)
       *out_iter = found_iter;
     return found;
    }
    

    use it:

    container::iterator the_iter;
    if(find_stuff(the_stuff, &the_iter)) ...
    

    or

    if(find_stuff(the_stuff, 0)) // if you don't need the iterator
    

    This is not idiomatic C++, but Linus would be pleased with it.

    The second possible and theoretically sound version is using something like boost::optional to return the value. This way, you return either some value or none.

    boost::optional<container::iterator> find_stuff(stuff) {
     ...
     if(found && out_iter)
       return found_iter;
     return boost::none;
    }
    

    Use:

    boost::optional<container::iterator> found = find_stuff(the_stuff);
    if(found) {
      do something with *found, which is the iterator.
    }
    

    or

    if(find_stuff(the_stuff)) ...
    

    Third possible solution would be going the std::set::insert way, ie. returning a pair consisting of a flag and a value:

    std::pair<bool, container::iterator> find_stuff(stuff) {
     ...
     return std::make_pair(found, found_iter);
    }
    

    Use:

    std::pair<bool, container::iterator> found = find_stuff(the_stuff);
    if(found.first) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a collection in which i can store heap-allocated objects having virtual functions.
I have a web form where I need to add, update, delete and read
I need to add a new functionality into my app. I must store a
I am using Magento for my online store. Now i need to add a
Through the website, I need to allow the user to add attachments and store
I can't access a element with its href. Like this example, i need add
Hї! I have wrote test for my application. I need add item to database
Need to add two same name .csproj class libraries in my solution.Have two project
I ll reframe my question based on some research I have done? I need
I have a Sitecore content structure where any single item can have a number

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.