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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:13:32+00:00 2026-06-02T04:13:32+00:00

If I am sure that a certain value is not yet into an unordered_set

  • 0

If I am sure that a certain value is not yet into an unordered_set, and I am going to insert such value, is it correct to pass this set end() iterator as a hint?

EDIT:

Code:

#include <unordered_set>
using namespace std;

unordered_set<int> someset;

int main(){
    auto it=someset.find(0);
    if(it==someset.end()) someset.insert(it, 0);    //correct? possible performance boost if the set is actually populated?
}
  • 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-02T04:13:35+00:00Added an answer on June 2, 2026 at 4:13 am

    I think, you can simply call insert function and the returned value will tell you whether value is inserted, or it is already present in the set.

    auto p = someset.insert(value);
    if (!p.second) 
    {
       std::cout << "value was already present in the set" << std::endl;
    }
    

    Actually p is of type std::pair<iterator,bool>, so p.second tells you whether value is inserted, or it is already present in the set, and p.first is the iterator which tells you the position of the value.

    Remember that this is faster than your approach, because my solution reduces the overall work.

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

Sidebar

Related Questions

I routinely run into this problem, and I'm not sure how to get past
I'm not sure if this can be done at all. I'm trying to set
I'm almost sure that not, but I didn't find a difinitive answer to that:
I'm sure that I'm doing something wrong here, but this is what my application
I am sure that this kind of questions must have been asked before, but
Im not sure that its possible to do what I want to accomplish. I
not sure this was asked before, but I couldn't find an obvious answer. I'm
Not sure if this is the right terminology, let me explain what I want.
I'm sure this is a very obvious question but I'm not getting anywhere with
This might be a doozy for some. I'm not sure if it's even 100%

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.