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?

Community
  • 1
  • 1
ashim
  • 24,380
  • 29
  • 72
  • 96

6 Answers6

2

You've declared vint inside the loop, so its lifetime is precisely one iteration. On each iteration, a fresh vector is constructed, and the end of each iteration, its destructor is called.

Try moving the declaration of vint to outside the loop.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
2

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).

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

vint is cleared (the whole thing, not just the items) from the stack on each iteration of the outer loop.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Well, you are re-declaring vint each time at the beginning of the inner loop. Hence the weird output. Use this instead :

vector<int> vint;
for ( i = 0 ; i < 3 ; i++ ) {

   vint.push_back(i);
   cout << "vecor size is: " << vint.size() << endl;
   for ( j = 0 ; j < vint.size() ; j++ ) {
       cout << "vint[" << j << "] = " << vint[j] << endl;
   }
}
Vishnu
  • 2,024
  • 3
  • 28
  • 34
0

Every time when outer loop iterates it initializes vint due to which its size remain same.

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

if you write like this then it would contain more values in inner loop

 vector<int> vint;           // vint Initialization
 for ( i = 0 ; i < 3 ; i++ ) {

           vint.push_back(i);
           cout << "vecor size is: " << vint.size() << endl;
           for ( j = 0 ; j < vint.size() ; j++ ) {
               cout << "vint[" << j << "] = " << vint[j] << endl;
           }
        }
Mujassir Nasir
  • 1,640
  • 4
  • 31
  • 52
0
vector<int> vint;

is being executed every time in outer loop so you get only one element on every iteration

Mujassir Nasir
  • 1,640
  • 4
  • 31
  • 52