STL container range insertion functions returning void under C ++ 11?

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);
                // Here we go. The problem spot!!!
                cur = str.insert(cur, newStr.begin(), newStr.end());
                break;
            }
        }
        ++cur;
    }
}
+5
source share
1 answer

There is no compiler that fully supports C++11. In later versions gcc, and clangsold most of the new standard, but there are still parts that must be done. Indeed, a scan basic_string.hfor gcc 4.7.0shows that this version inserthas not yet been updated:

  template<class _InputIterator>
    void
    insert(iterator __p, _InputIterator __beg, _InputIterator __end) { ... }
+4
source

All Articles