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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:33:44+00:00 2026-05-12T07:33:44+00:00

We have large (100,000+ elements) ordered vectors of structs (operator < overloaded to provide

  • 0

We have large (100,000+ elements) ordered vectors of structs (operator < overloaded to provide ordering):

std::vector < MyType > vectorMyTypes;
std::sort(vectorMyType.begin(), vectorMyType.end());

My problem is that we’re seeing performance problems when adding new elements to these vectors while preserving sort order. At the moment we’re doing something like:

for ( a very large set )
{
    vectorMyTypes.push_back(newType);
    std::sort(vectorMyType.begin(), vectorMyType.end());

    ...

    ValidateStuff(vectorMyType); // this method expects the vector to be ordered
}

This isn’t exactly what our code looks like since I know this example could be optimised in different ways, however it gives you an idea of how performance could be a problem because I’m sorting after every push_back.

I think I essentially have two options to improve performance:

  1. Use a (hand crafted?) insertion sort instead of std::sort to improve the sort performance (insertion sorts on a partially sorted vector are blindingly quick)

  2. Create a heap by using std::make_heap and std::push_heap to maintain the sort order

My questions are:

  • Should I implement an insertion sort? Is there something in Boost that could help me here?

  • Should I consider using a heap? How would I do this?


Edit:

Thanks for all your responses. I understand that the example I gave was far from optimal and it doesn’t fully represent what I have in my code right now. It was simply there to illustrate the performance bottleneck I was experiencing – perhaps that’s why this question isn’t seeing many up-votes 🙂

Many thanks to you Steve, it’s often the simplest answers that are the best, and perhaps it was my over analysis of the problem that blinded me to perhaps the most obvious solution. I do like the neat method you outlined to insert directly into a pre-ordered vector.

As I’ve commented, I’m constrained to using vectors right now, so std::set, std::map, etc aren’t an option.

  • 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-05-12T07:33:44+00:00Added an answer on May 12, 2026 at 7:33 am

    Ordered insertion doesn’t need boost:

    vectorMyTypes.insert(
        std::upper_bound(vectorMyTypes.begin(), vectorMyTypes.end(), newType),
        newType);
    

    upper_bound provides a valid insertion point provided that the vector is sorted to start with, so as long as you only ever insert elements in their correct place, you’re done. I originally said lower_bound, but if the vector contains multiple equal elements, then upper_bound selects the insertion point which requires less work.

    This does have to copy O(n) elements, but you say insertion sort is “blindingly fast”, and this is faster. If it’s not fast enough, you have to find a way to add items in batches and validate at the end, or else give up on contiguous storage and switch to a container which maintains order, such as set or multiset.

    A heap does not maintain order in the underlying container, but is good for a priority queue or similar, because it makes removal of the maximum element fast. You say you want to maintain the vector in order, but if you never actually iterate over the whole collection in order then you might not need it to be fully ordered, and that’s when a heap is useful.

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

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want CFStream http://developer.apple.com/mac/library/documentation/Networking/Conceptual/CFNetwork/Concepts/Concepts.html#//apple_ref/doc/uid/TP30001132-CH4-SW4 May 12, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer &apos; in xml uses the predefined entity reference for '… May 12, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer A bit more googling arround found this post Turns out… May 12, 2026 at 7:17 pm

Related Questions

At my workplace we have one large Subversion repository which holds about 100 projects.
I have a large c# solution file (~100 projects), and I am trying to
We are using Maven for a large build process (> 100 modules). We have
We have a hosted application that manages pages of content. Each page can have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.