2013-04-11 37 views
5

只是學習C++,所以我可能沒有正確理解這一點,但我只讀了範圍插入函數返回一個迭代器在新標準下(C++ Primer 5th Ed,cplusplus.comcppreference.com,以及各種答案,建議使用它保持迭代器有效性)。STL容器的範圍插入函數在C++ 11下返回void?

從cppreference.com:

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

使用-std = C++ 11然而,Cygwin的GCC和MinGW的每一個版本,我試過返回無效。即使看看頭文件,它看起來就是這樣寫的,而且沒有什麼我可以修改來解決這個問題。

我錯過了什麼?

下面是我試圖編寫的'章節練習結束'功能;對更換一個字符串給定的字符串中:

(我理解爲意它的編寫方式也不起作用)

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

?請不要說*「每個版本」*。這沒有幫助。請明確點。 – Nawaz 2013-04-11 04:11:41

+0

啊,謝謝。最新版本使用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釋放,win64_rubenvb;和MinGW-TDM 4.7.1-dw2 – Habnab 2013-04-11 04:20:52

+0

試試。 GCC'4.8.0'。這是最新的! – Nawaz 2013-04-11 04:22:15

回答

4

沒有編譯器,它完全支持C++11呢。後續版本的gccclang已實施了大部分新標準,但仍有部分需要完成。的確,看着basic_string.hgcc 4.7.0表明,該版本的insert尚未更新:其中* MinGW的版本*您在使用

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

如果這不僅僅是我搗亂的事情,這減輕了一些挫折,但我希望我能找到一個支持的版本。感謝您的幫助! – Habnab 2013-04-11 04:24:37

+2

@Habnab:Visual Studio 11已正確實施。 – 2013-04-11 04:29:15

+0

@BenjaminLindley:謝謝。儘管如此,我一直在試圖避免VS出於某種原因,超越了我。當我學習語言(到目前爲止,vim和make/tup)時,試圖堅持使用最少量的工具。我可能會在某個時候放棄。非常感謝幫助。 – Habnab 2013-04-11 04:35:13