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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:07:57+00:00 2026-06-06T14:07:57+00:00

I would like some container that I can very efficiently append a variable amount

  • 0

I would like some container that I can very efficiently append a variable amount of elements to, but be able to trigger something so I can start overwriting from the beginning. With a std::list it would look something like this:

while(whatever)
{
    for(int i = 0; i < randNumber; ++i)
        list.push_back( foo() );
    //now want to reset
    list.clear();
}

The problem is list.clear() is linear time whereas I would really just like to go back to the beginning and start overwriting from there…I tried with vector using vector[index++] = foo() and replacing the clear with index = 0 but you cant predict randNumber so this does not work…what can I use instead to achieve this?

BTW vector clear does not seem to be constant time even if I have a trivial destructor:

struct rat
{
    rat(int* a, int* b) : a_(a), b_(b) {}

    int *a_;
    int *b_;
};

int main(int argc, char **argv)
{
    uint64_t start, end;

    int k = 0;
    vector<rat> v;
    for (int i = 0; i < 9000; ++i)
        v.push_back(rat(&k, &k));

    start = timer();
    v.clear();
    end = timer();

    cout << end - start << endl;
}
  • 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-06T14:07:59+00:00Added an answer on June 6, 2026 at 2:07 pm

    Just replace std::list for std::vector in your code. push_back will increment the size as needed, and clear will remove all elements from the container. Note: std::vector<>::clear() takes linear time on the size of the container, but the operations are destruction of the stored elements (if they have a non-trivial destructor) which you need to do anyway. For types with trivial destructors, std::vector<>::clear() behaves as a constant time operation.

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

Sidebar

Related Questions

I would like to write up some css/html for container boxes that I can
I would like to filter NSMutableArray elements that contains some string. ListArchives is filled
i would like to have a container (with some text) on the left of
When you run a Java Servlet Container that you would like to serve both
I've implemented a set of draggable elements that can be dropped into some containers
I would like some advice on the best approach to use in the following
I would like some of my preferences to have icons, like the Settings app.
I'm new to JavaScript and would like some constructive criticism regarding a code snippet.
I am developing a site and i would like some simple markup. I would
I am learning C++ exceptions and I would like some clarification of the scenario:

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.