So I have the following code, which is supposed to use an iterator to replace an element in a vector if it matches some given value "old".
using StringIter = std::vector<std::string>;
using StringVec = std::vector<std::string>;
void replace(StringVec ref, std::string old, std::string replacement)
{
for (StringIter::iterator it = ref.begin(); it != ref.end(); it++)
{
if (*it == old)
{
ref.erase(it);
ref.insert(it,replacement);
}
}
}
However, I get the following runtime error: "vector emplace iterator outside range". I've tried changing the order of erase and insert, and using insert(it-1,...) instead, but it doesn't work. Haven't found any solution to this exact problem after an hour or so of googling.