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

  • Home
  • SEARCH
  • 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 9217103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:34:01+00:00 2026-06-18T02:34:01+00:00

I have a multiset mymult i where I sort according to a class member

  • 0

I have a multiset mymulti where I sort according to a class member m_a.

I want then to check for all sorted elements, if the difference in m_a for neighbour fields of mymulti is less than my given threshold, say 0.001. If so, then I want to prefer that which has a smaller another class member, m_b.

Here I am stuck, I have no experience with multiset or iterators. I don’t know how to compare iterators from two iterations. If you can provide me with a right code for this what I want to do, will be very grateful!
My try, not too much, just my concept:

    //all before I got stuck
    for(it = mymulti.begin(); it!= mymulti.end(); ++it) //or it++?
        if( (it+1)->mymulti.m_a - (it)->mymulti.m_a < 0.001)
           if ((it+1)->mymulti.m_b < (it)->mymulti.m_b)
              //swap them. but how to swap two fields in a multiset, not two multisets?
           // otherwise do nothing
  • 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-18T02:34:03+00:00Added an answer on June 18, 2026 at 2:34 am

    You cannot (or if you can, depending on your STL implementation, should not) modify items once they have been inserted into a multiset, as it could violate the provided ordering of the items in the multiset. So swapping would be a bad idea, even if you could do it.

    See https://stackoverflow.com/a/2038534/713961 and http://www.cplusplus.com/reference/set/multiset/

    If you would like to remove items, use multiset::erase, which takes an iterator. I believe the standard practice for “modifying” an item in a multiset is to remove it, then insert the modified version.

    As a side note, I noticed you’re checking if two floating point numbers are close enough in value by using a fixed epsilon (0.001). As explained in this article, this only works if all the floats you are comparing are sufficiently small. See the article for a comparison that works equally well for large and small floating-point values.

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

Sidebar

Related Questions

I have a std::multiset which stores elements of class A . I have provided
So I need to have a some sort of multiset of characters, where adding
Have written all the code in a silverlight class library (dll) and linked this
I have a std::multiset of sorted custom objects. Two equal objects (based on the
I have a multiset and I'm getting a range from it. I want to
How to count values in a multiset? I have a dataset and i want
have written this little class, which generates a UUID every time an object of
Have a painfully simple blog Post creator, and I'm trying to check if the
I have: 1) a starting set, a multiset, e.x. { x, y, z, z
I have a private field std::map<std::string, std::multiset<GraphObject*>>* the_Map; How can I allocate memory for

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.