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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:52:55+00:00 2026-05-27T10:52:55+00:00

Suppose I have a long sequence of unordered elements S s1, s2, s3,…. of

  • 0

Suppose I have a long sequence of unordered elements S s1, s2, s3,.... of a arbitrary but fixed data type through which I wish to iterate and delete certain elements according to some boolean criterion.

Now if after iterating through the sequence if I am not interested in the final ordering of the sequence then I can store my sequence in 2 ways

  1. Use a plain ol’ std::list to represent the sequence. Perform removal with the std::list methods.
  2. Use a std::vector to represent the sequence. If a certain element fails the criterion and has to be deleted swap it with the last vector element and perform a pop_back.

My questions are

1.Which would be a better/efficient way timewise and/or memorywise to store my sequence?

2.If I had to venture a guess, then I would say list, because if si ‘s data-type memory size is large, swapping would be expensive. Would this reasoning be correct?

  • 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-27T10:52:56+00:00Added an answer on May 27, 2026 at 10:52 am

    In practice, std::vector has a great performance advantage over other containers due to its tight memory locality. If your elements are moreover movable (i.e. inexpensive to swap), then your second option should be your first try. Implement it with the standard remove/erase idiom:

    v.erase(std::remove_if(v.begin(), v.end(), predicate), v.end());
    

    You should also set up a second version with a std::list and compare the performance:

    l.remove_if(predicate);
    

    The list avoids moving any elements around, so in theory it could be efficient, however the practical effects of memory locality cannot be captured by the language standard and you cannot get around measuring and comparing the actual performance.

    (Supposedly, if your element type is huge, like sizeof(T) > 10000, the list will probably start being faster than the vector. Test and compare, and keep your code modular such that changing this later is easy.)

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

Sidebar

Related Questions

I have a python app which is supposed to be very long-lived, but sometimes
Suppose I have a relatively long module, but need an external module or method
Suppose I have several javascript object {type:gotopage,target:undefined} {type:press,target:a} {type:rotate,target:long} How can I add this
Suppose I have a very long file, utilities.php, which contains most if not all
Suppose I have: typedef unsigned long long uint64; unsigned char data[BUF_SIZE]; uint64 MyPacket::GetCRC() {
suppose I have a long algorithm which I would like to be optionally verbose
Suppose I have an R data frame with columns that specify location (lat/long), height,
Suppose I have a class like this: class Owner { private: long m_Id; QString
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input

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.