My compiler complains.
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<string> vec[2];
vec[0].push_back("test1");
vec[0].push_back("test2");
cout << vec[0][0] << endl;
vec[0].erase(vec.begin());
cout << vec[0][1] << endl;
}
What is wrong when I call erase?
vec
is an array of vector<string>
s. I believe you meant vec[0].begin()
like so:
vec[0].erase(vec[0].begin());
vec[0].erase(vec[0].begin());
cout << vec[0][1] << endl;
After you erased the first element, there's only one left. This one element is at position 0, which means index 1 is off bounds -> undefined behaviour.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.