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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:36:50+00:00 2026-06-15T07:36:50+00:00

A snippet if (a<=lim){ if(std::find(prims.begin(), prims.end(), a)==prims.end()){ prims.push_back(a); count+=lim/a; } } So basically I

  • 0

A snippet

if (a<=lim){
    if(std::find(prims.begin(), prims.end(), a)==prims.end()){
        prims.push_back(a);
        count+=lim/a;
    }         
}

So basically I have this part in my code where I add the variable a into this vector if it isn’t already present, and then I update a counter on the fly.

But I am wondering if this is suboptimal in terms of runtime. Anything faster I can do?

  • 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-15T07:36:53+00:00Added an answer on June 15, 2026 at 7:36 am

    std::set Is a container that is used commonly if you want to only keep unique values.
    Values are internally stored as red-black trees so most operations are of logarithmic complexity.

    This would be faster than your implementation, and about as fast as the suggested binary search implementation.

    You can improve it further by using the new (c++11) std::unordered_set, which stores it as a hash table, which would make it even faster at average constant time operations.

    Use set as follows

    std::set<TYPE> prims;
    ....
    if (a<=lim){
        if(prims.insert(a).second){
            count+=lim/a;
        }         
    }
    

    where TYPE is the type of the values stored in your vector

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

Sidebar

Related Questions

This snippet of code generate huge memory leaks. Could you help me to find
This snippet of code always parses the date into the current timezone, and not
jQuery snippet: $(.sliders dt a).click(function(){ $(this).parent().parent().parent().find(.triggers a).removeClass(active); $(this).toggleClass(active); $(this).parent().next(dd).slideToggle(fast); return false }); HTML: <div
this snippet of code is given me headache. Personally, I would like to use
I have the following snippet of code: $(#QnAList).append(<div id=qdiv + i + class=divBase> <span
I currently have this XML schema : <PSC5> <POI_ORI> <CIT>LIM</CIT> </POI_ORI> </PSC5> if user
Code snippet: for (int i = 0; i < vals.Length -1; i++) { series1.Points.Add(Convert.ToDouble(vals[i]));
This snippet basically reads a file line by line, which looks something like: Album=In
This snippet of code creates method as a private method, why? a = %q{def
This snippet is from my admin. I have a description field next to an

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.