我的测试程序的目标是擦除一个简单的字符串向量中的单元格,如下所示。程序失败(分段错误)。
static void display(std::vector<std::string> const &vec)
{
std::vector<std::string>::const_iterator It = vec.begin();
for (; It != vec.end(); ++It)
std::cout << *It << " ";
std::cout << std::endl;
}
int main(void)
{
std::vector<std::string> vec;
size_t index = 0;
vec.push_back("Toto");
vec.push_back("Titi");
vec.push_back("Tata");
vec.push_back("Tutu");
display(vec);
std::vector<std::string>::iterator It = vec.begin();
for (size_t idx = 0; It != vec.end(); ++It, idx++)
if (!(*It).compare("Tutu"))
index = idx;
vec.erase(std::remove(vec.begin(), vec.end(), index), vec.end()); //Segmentation fault
display(vec);
getchar();
return (0);
}
有人可以帮助我吗?在此先感谢您的帮助。