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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:51:36+00:00 2026-05-27T19:51:36+00:00

Question is about variables inside the loop. How does it works? Similar questions: one

  • 0

Question is about variables inside the loop. How does it works?
Similar questions:
one
two

code:

for ( i = 0 ; i < 3 ; i++ ) {
   vector<int> vint;
   vint.push_back(i);
   cout << "vecor size is: " << vint.size() << endl;
   for ( j = 0 ; j < vint.size() ; j++ ) {
       cout << "vint[" << j << "] = " << vint[j] << endl;
   }
}

above code gives next output:

vecor size is: 1   
vint[0] = 0
vecor size is: 1
vint[0] = 1
vecor size is: 1
vint[0] = 2

It looks like that in the end of each iteration method sdt::vector::erase is called or somehow vint becomes again empty. How does it work, and how did you figure out it? thank you!

UPDATE:
If I want different vector in each iteration what is better:
declare vint in the loop
or declare it before the loop and use clear() in the end of the loop?

  • 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-27T19:51:37+00:00Added an answer on May 27, 2026 at 7:51 pm

    Variables in the statement of the for-loop are constructed and destructed in each iteration. In your particular case this would involve memory allocation and deallocation for each iteration. If you need a different vector each iteration you’d still be best off declaring it prior to the loop (or in the loop’s initialization) and clear()ing it at the end of the loop (or in the loop’s advance).

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

Sidebar

Related Questions

I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I have a question about the variables inside the static method. Do the variables
I'm currently developing some things in Python and I have a question about variables
I just came across this question about initializing local variables. Many of the answers
I had a question about Java Class Path variables. If I have multiple jars
I have a question about ruby on rails and the process of assigning variables
Question #1: Is declaring a variable inside a loop a good practice or bad
I just had a quick question about how stack variables versus heap variables work.
I have a few questions about the INTENT of variables within a subroutine in
I know a SESSION can expire but what about $_REQUEST/$_POST/$_GET variables? My question is,

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.