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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:53:48+00:00 2026-06-18T15:53:48+00:00

All I need is to know if something exists and how many times it

  • 0

All I need is to know if something exists and how many times it exist. I will iterate over the existent things and query how much of that exists.

My implementation so far uses multiset, I do as follow:

std::multiset<thing> a;
auto previous = a.end();
for( auto each = a.begin(); each != a.end(); ++each ) {
    if( previous == a.end() || *previous != *each ) {
        a.count(*each);
    }
    previous = each;
}

Clarifications

I have a vector of things. But they repeat the value sometimes, I want to iterate over unique things and for each unique do something. This “something” needs to know the amount of time this thing appears on the vector.

The code I posted above is how I am resolving my problem right now, it does not seems to be the most elegant way to do what I want.

I am just following the Stackoverflow guidelines: I tell what is my problem, and I tell my (tried) solution.

If a sentence with a question mark is really needed, there you go: Is there a way to iterate over unique elements over a multiset?

  • 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-18T15:53:49+00:00Added an answer on June 18, 2026 at 3:53 pm

    Three possible approaches:

    • Use std::unique to create a temporary collection of unique values. This might make the code a little more readable, but less efficient.
    • Advance your iterator by using std::multiset::upper_bound rather than increments: for( auto each = a.begin(); each != a.end(); each=a.upper_bound(*each)) – that way you don’t need the if check insider your loop, plus it is guaranteed to be logarithmic in size. Pretty cool (didn’t know that before I looked it up). For the following suggestion, all credit goes to @MarkRansom: Using std::upper_bound from <algorithm>, you can specify a range in which to look for the upper bound. In your case, you already have a good candidate for the start of that range, so this method is likely to be more efficient, depending on the implementation in your standard library.
    • If this is a real performance problem for you and the previous solution still isn’t good enough, consider switching to map<thing, unsigned> or even unordered_map<thing,unsigned> where the unsigned just keeps track of the number of equivalent things you have. That implies rewriting your insertion/deletion code though.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a query that will return results if all the tables
I need to know if all the client-side validator checks have succeeded, or if
i need all permutation from N lists, i dont know N until the program
I've got a problem where I need to know about all the bindings that
Afternoon All, I have a web site where i need to know who is
Hallo all. if i have week number 42 and i need to know what
As my title, i need to know whether a file contains all the crash
I am doing a project, for which I need to know all the wikipedia
Does anyone know why my site is all messed up on safari? I need
In my jnlp app I need to know if a user property file exists,

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.