STL容器的范围插入函数返回void在c ++ 11下? [英] STL containers' range insert functions returning void under c++11?
问题描述
只是学习c ++,所以我可能没有正确理解这一点,但我只读了range插入函数返回一个迭代器在新标准下(C ++ Primer第5版, cplusplus.com , cppreference .com ,以及建议使用它来维护迭代器有效性的各种答案。)
来自cppreference.com:
template< class InputIt>
迭代器插入(const_iterator pos,InputIt first,InputIt last);但是,我尝试过的每个版本的Cygwin GCC和MinGW都返回了void,使用-std = c ++ 11。即使看着标题,它似乎是这样写的,而且没有什么可以修改来解决这个问题。
我缺少什么?
这是我试图写的结束练习函数;在给定字符串中用一个字符串替换另一个字符串:
(我理解它不会像写的那样正常工作)
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);
//我们走了。问题现货!
cur = str.insert(cur,newStr.begin(),newStr.end());
break;
}
}
++ cur;
}
}
解决方案是没有编译器完全支持 C ++ 11
。较早版本的 gcc
和 clang
已实现了大部分新标准,但仍有部分需要完成。确实,查看 basic_string.h
为 gcc 4.7.0
显示,此版本的 insert
尚未更新:
模板< class _InputIterator>
void
insert(iterator __p,_InputIterator __beg,_InputIterator __end){...}
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;
}
}
解决方案 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) { ... }
这篇关于STL容器的范围插入函数返回void在c ++ 11下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!