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?
我打电话给擦除有什么问题?
2 个解决方案
#1
4
vec
is an array of vector<string>
s. I believe you meant vec[0].begin()
like so:
vec是vector
vec[0].erase(vec[0].begin());
#2
0
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.
擦除第一个元素后,只剩下一个元素。这一个元素位于0位置,这意味着索引1是非界限 - >未定义的行为。
#1
4
vec
is an array of vector<string>
s. I believe you meant vec[0].begin()
like so:
vec是vector
vec[0].erase(vec[0].begin());
#2
0
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.
擦除第一个元素后,只剩下一个元素。这一个元素位于0位置,这意味着索引1是非界限 - >未定义的行为。