It:
vLine[i].erase();
does not erase vLine[i] from the vector. The expression vLine[i] returns a reference to the element in index i . Assuming vLine is of type std::vector<std::string> , calling the erase() function actually calls string::erase() on the element, not vector::erase() on the vector. Everything you do makes this particular element empty.
What you might want is something like this:
vLine.erase(vLine.begin() + i);
This actually removes the element from the vector. Now this invalidates all current iterators for the vector, and the indexes will no longer be right. This is a situation where you really need to use iterators.
std::vector<std::string>::iterator i = vLine.begin(); while(i != vLine.end()) { if(i->find(';', 0) != std::string::npos) { i = vLine.erase(i); } else { ++i; } }
But there is an even simpler way to do this: use the standard std::remove_if() algorithm with a functor, then call vLine.erase() .
struct HasSemicolon { bool operator()(const std::string& s) { return s.find(';', 0) != std::string::npos; } };
If you can use the C ++ 11 compiler, then you can also use lambda expressions even more concise.
In silico
source share