STL容器的范围插入函数在c ++ 11下返回void?

2024-04-05

刚刚学习c++,所以我可能没有正确理解这一点,但我只读到范围插入函数在新标准下返回一个迭代器(C++ Primer 5th Ed,cplusplus.com http://www.cplusplus.com/reference/string/basic_string/insert/, cppreference.com http://en.cppreference.com/w/cpp/string/basic_string/insert,以及建议使用它来维持迭代器有效性的各种答案)。

来自 cppreference.com:

template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );

然而,我尝试过的每个版本的 Cygwin GCC 和 MinGW 都使用 -std=c++11 返回 void。即使查看标题,它似乎就是这样写的,而且我无法修改任何内容来解决这个问题。

我缺少什么?

这是我试图编写的“章节结束练习”函数;在给定字符串中用另一个字符串替换一个字符串:

(我知道它不会按其编写的方式发挥作用)

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;
    }
}

没有完全支持的编译器C++11然而。以后的版本gcc and clang新标准的大部分内容已实施,但仍有部分内容需要完成。确实,看着basic_string.h for gcc 4.7.0表明这个版本的insert尚未更新:

  template<class _InputIterator>
    void
    insert(iterator __p, _InputIterator __beg, _InputIterator __end) { ... }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

STL容器的范围插入函数在c ++ 11下返回void? 的相关文章

随机推荐