Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL containers' range insert functions returning void under c++11?

Just learning c++, so I may not be understanding this correctly, but I've only 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 to use it to maintain iterator validity).

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've tried has returned void using -std=c++11. Even looking at the headers it would seem that that's how it's written, and that there's nothing I can modify to fix that.

What am I missing?

Here's the 'end of chapter exercise' function I was attempting to write; replacing one string with another within a given string:

(I understand it won't function as intended the way it's written)

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;
    }
}
like image 542
Habnab Avatar asked Oct 05 '22 10:10

Habnab


1 Answers

There is no compiler that fully supports C++11 yet. Later versions of gcc and clang have a majority of the new standard implemented, but there are still parts that need to be done. Indeed, looking at basic_string.h for gcc 4.7.0 shows that this version of insert has not yet been updated:

  template<class _InputIterator>
    void
    insert(iterator __p, _InputIterator __beg, _InputIterator __end) { ... }
like image 184
Yuushi Avatar answered Oct 12 '22 22:10

Yuushi