练习9.43有错误
cegd748 opened this issue · comments
试试把"thou, like justice, although the tho not in, i tho"中的tho替换成though。
结果是"thoughu, like justice, althoughugh the though not in, i tho"。
下面是我的replace。
结果是“thou, like justice, although the though not in, i though”。
void replace(std::string& s, const std::string& oldVal, const std::string& newVal)
{
std::string s1{ " " };
auto iter = s.begin();
auto end = s.begin();
auto prev = s1.begin();
auto size = oldVal.size();
while (iter <= s.end() - size)
{
end = iter + size;
if (*prev == ' ' && (end == s.end() || *end == ' ' ||
ispunct(*end)) && std::string(iter, end) == oldVal)
{
iter = s.erase(iter, end);
iter = s.insert(iter, newVal.begin(), newVal.end());
iter += size;
prev = iter - 1;
}
else
{
prev = iter;
++iter;
}
}
}