The goal of my test program is to erase a cell in a simple vector of strings like below. The program fail (segmentation fault).
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);
}
Does anyone can help me? Thanks in advance for your help.