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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:17:27+00:00 2026-06-03T19:17:27+00:00

Which STL container should i use if: Data is inserted and removed regularly. Data

  • 0

Which STL container should i use if:

  1. Data is inserted and removed regularly.
  2. Data is accessed regularly at random.

E.g : dataset(4,10,15) if i want to find the closest number to 9, then it should return me 10.

  1. I am only storing an integer.
  2. It needs to be sorted
  3. Can go to 100k datasets

I thought of using vector, but vector insertion and removing is expensive.

   vector<int>

If i were to use list, i would have to access O(n) elements before reaching the data.

   list<int>

I was thinking of using set as it will be good if it is sorted, but im not very sure about the efficiencies for using SET

So i hope someone can give a good solution!

  • 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-03T19:17:33+00:00Added an answer on June 3, 2026 at 7:17 pm

    I think you should check this SO post: In which scenario do I use a particular STL container? for small sizes vector will suit most scenarios irrespective of what you intend to do.

    The chart is a guide though, the fact that the container is accessed regularly does not affect container choice, the fact that you are storing int is unimportant unless you care about the size of the container, in which case does the overhead of the pointers in a list container or map matter to you?

    Sorting is done automatically by map but sorting a vector and list can be very fast if the container size is small enough to fit in memory.

    Data insertion is optimised for lists and maps anywhere in the container, for maps you get the benefit that it will sort itself but again if the size is small enough then constructing a new vector with the new entry could be very fast still.

    You may also want to consider hash maps, you would still be best to profile your code, trying to second guess what is optimal depends on your usage and you really need to measure and profile.

    You could also just decide that an STL <map> is a fine enough balance or a <set> and use those containers as they automatically sort on insertion and deletion and look up is fast but there is the overhead of maintaining the pointers in each entry that increases the size of the memory used compared to vector, if you don’t care about this then you could consider these containers.

    Still if it matters then test and profile and compare the performance of each container, you will be surprised by how the code will perform against your assumptions.

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

Sidebar

Related Questions

Which STL container has a thread safe insertion process? I want several threads to
I am unable to decide which STL container to use in the following case:
Possible Duplicate: C++ STL: Which method of iteration over a STL container is better?
I am trying to write a container class which uses STL allocators. What I
I'd like to use an STL set in my iPhone app (which is written
Following an example in Accelerated C++ , I created a custom STL container, which
I'm trying to use RAII concepts with an STL container of ofstream objects. For
Need advice here: which of the STL container's operations are considered read-only? Take vector<int>
I am looking for some STL (but not boost) container, which after the following
Which STL container would fit my needs best? I basically have a 10 elements

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.