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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:48:43+00:00 2026-05-31T12:48:43+00:00

I want to reuse a std::vector within a for loop. However, I need the

  • 0

I want to reuse a std::vector within a for loop. However, I need the vector to be empty for each iteration step of the for loop.

Question: How can I empty a vector rapidly without changing its capacity in the most efficient way?

What I used so far is

std::vector<int> myVec;
for(int i=0; i<A_BIG_NUMBER; ++i) {
    std::vector<T>().swap(myVec);
    myVec.reserve(STANDARD_MAXIMUM);

    /// .. doing business
}

Cheers!

Solution:

Thanks for the answers, here is how I implemented (checked) it:

#include <vector>
#include <iostream>

 int main() {

    int n = 10;
    std::vector< int > myVec;
    myVec.reserve(n);
    for(int j=0; j<3; ++j) {
            myVec.clear();
            for(int i=0; i<n; ++i) {
                    myVec.push_back(i);
            }
            for(int i=0; i<myVec.size(); ++i) {
                    std::cout << i << ": " << myVec[i] << std::endl;
            }
    }

    return 0;
}

EDIT: changed from operator[] to push_back.

  • 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-31T12:48:44+00:00Added an answer on May 31, 2026 at 12:48 pm

    Use vector::clear method. It will clear the content without reducing its capacity.

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

Sidebar

Related Questions

i want to reuse an image multiple times within an item renderer, is it
I want to reuse some Cucumber steps but can't seem to find the right
I want to reuse my tabs like footer for each activities in my application.
I want to reuse some code licensed under a BSD license but I don't
I want to reuse some code I wrote to add some functionality to a
I want to reuse a WebRequest object so that cookies and session would be
I have a whole bunch of projects which I want to reuse between the
I know it's simple to implement, but I want to reuse something that already
I want to use/reuse C++ object with Objective-C. I have a hello.h that has
I want to build a custom control to reuse in my project which consists

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.