5

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;
    }
}
6
  • 1
    Which version of MinGW you're using? Please don't say "every version". That doesn't help. Be specific. Apr 11, 2013 at 4:11
  • Ah, thank you. The latest build using mingw-get-inst-20120426.exe, 4.6.2; i686-w64-mingw32-gcc-dw2-4.8.0-win32_rubenvb; x86_64-w64-mingw32-gcc-4.6.3-2-release-win64_rubenvb; and MinGW-TDM 4.7.1-dw2
    – Habnab
    Apr 11, 2013 at 4:20
  • 3
    Seems like the standard library implementations haven't caught up to the new standard in some places. Apr 11, 2013 at 4:22
  • 3
    @Nawaz: Even 4.8 doesn't implement it: liveworkspace.org/code/3dnNFJ$0 -- VC11 does though. Apr 11, 2013 at 4:26
  • 1
    Note: this is less of a compiler issue and more of a Standard Library implementation issue. Whilst every compiler comes bundled with a Standard Library implementation, you may be able to swap an implementation for another: for example Clang compiles both libstdc++ and libc++. Apr 11, 2013 at 7:26

1 Answer 1

4

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) { ... }
5
  • If it's not just me botching things up, that eases some of the frustration, though I do wish that I could find a supported version. Thank you for the help!
    – Habnab
    Apr 11, 2013 at 4:24
  • 2
    @Habnab: Visual Studio 11 has it properly implemented. Apr 11, 2013 at 4:29
  • @BenjaminLindley: Thank you. Though, I have been trying to avoid VS for some reason that is beyond me. Trying to stick to a minimal set of tools as I learn the language (vim and make/tup so far). I may just give in at some point. Much appreciate the help.
    – Habnab
    Apr 11, 2013 at 4:35
  • 2
    @Habnab: You can run the Visual Studio toolset from a command line, and even use makefiles with it, if you want. Apr 11, 2013 at 4:44
  • I will most definitely try that, then! Thanks!
    – Habnab
    Apr 11, 2013 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.