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); }
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
Post a Comment