c++ - why vector does not updates in loop? -
I want to update the vector 'v' so that I can repeat count from 0-100.
I know that this is not allowed, but what if I only want to do this? Is there any way?
int main () {// your code here vector and lieutenant; Int & gt; V; V.push_back (1); Int count = 0; (INM: v) {if (count> 100) v.push_back (count); Count ++; } (ATM: v) COAT & LT; & Lt; Elem & lt; & Lt; Endl; Return 0; }
Output is:
1
As you can see from border-based for loops, end_expr is not updated between iterations so you only have one running push_back
Invalid v.end ()
(which is described in the end_expr as linked page), so you actually have undefined behavior.
The easiest way to fill the vector with 0.01 will be:
vector & lt; Int & gt; V (101); Std :: iota (v.begin (), v.end (), 0);
Comments
Post a Comment