c++ - Erasing from vector -


i'm tried erase elements vector. in fact, wrote that:

#include<iostream> #include<vector>  std::vector<int> foo(std::vector<int> v) {     std::vector<int> result;     std::cout << "initial size = " << v.size() << std::endl;     for(int = 0; < v.size(); i++)     {         std::cout << "size = " << v.size() << std::endl;         v.erase(v.begin() + i);     }     return result; }  int main() {     int a[] = {1 ,2, 5, 8, 213, 2};     std::vector<int> v;     v.assign(a, a+6);     foo(v); } 

demo

why program prints

initial size = 6 size = 6 size = 5 size = 4 

where

size = 3 size = 2 size = 1 

?

after third erasure have == 3 , v.size() == 3 , exits


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -