Just learning C ++, so I may not get it right, but I just read that the range insert function returns an iterator under the new standard (C ++ Primer 5th Ed, cplusplus.com , cppreference.com and various answers suggesting using it to preserve the validity of the iterator).
From cppreference.com:
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
However, every version of Cygwin GCC and MinGW that I tried returned void with -std = C ++ 11. Even looking at the headers, it would seem that it was written and that I could not change anything to fix it.
What am I missing?
Here is the function "end of chapter exercise" that I tried to write; replacing one line with another in the given line:
(I understand that he will not function as intended, as he wrote)
void myfun(std::string& str, const std::string& oldStr, const std::string& newStr)
{
auto cur = str.begin();
while (cur != str.end())
{
auto temp = cur;
auto oldCur = oldStr.begin();
while (temp != str.end() && *oldCur == *temp)
{
++oldCur;
++temp;
if (oldCur == oldStr.end())
{
cur = str.erase(cur, temp);
cur = str.insert(cur, newStr.begin(), newStr.end());
break;
}
}
++cur;
}
}
source
share