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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:56:55+00:00 2026-06-05T05:56:55+00:00

I have a sorted std::vector of relative small size ( from 5 to 20

  • 0

I have a sorted std::vector of relative small size ( from 5 to 20 elements ).
I used std::vector since the data is continuous so I have speed because of cache.
On a specific point I need to remove an element from this vector.

I have now a doubt: which is the fastest way to remove this value between the 2 options below?

  1. setting that element to 0 and call sort to reorder: this has complexity but
    elements are on the same cache line.
  2. call erase that will copy ( or memcpy who knows?? ) all elements after it
    of 1 place ( I need to investigate the behind scense of erase ).

Do you know which one is faster?

I think that the same approach could be thought about inserting a new element without hitting the max capacity of the vector.

Regards

AFG

  • 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-05T05:56:56+00:00Added an answer on June 5, 2026 at 5:56 am

    If you don’t care about the order of elements, you may be able to swap the element with the last one.

    void Remove( std::vector<Object> &vec, iterator i ) {
        iterator last = vec.end()-1;
        if (i != last)
            std::swap( *i, *last );
        vec.erase( last );
    }
    

    You mention setting the element to 0. If that means you have pointers, then you may not need the swap:

    void Remove( std::vector<Object *> &vec, iterator i ) {
        vec[i] = vec.back();
        vec.erase( vec.end()-1 );
    }
    

    If you do care about the order, then your second option of using using erase() will preserve it and do the minimum amount of work. It will almost certainly be faster than resorting.

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

Sidebar

Related Questions

I have a collection of elements in a std::vector that are sorted in a
I have some data that is stored in a sorted vector. This vector is
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a sorted std::vector<unsigned long long int> myvector (and all values are different).
I have a sorted set (std::set to be precise) that contains elements with an
I have table of data that is sorted as follows: Item | Sample |
How large does a collection have to be for std::map to outpace a sorted
I have a std::vector<std::string> which would contain numbers and characters (single char). I would
I have this two vector<double> 's mass and velocity both of the same size
i have a vector of vertices from mygraph, and i topologically sort the vertices.

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.