'替换'来自< algorithm>运作不正常 [英] 'Replace' from <algorithm> not functioning correctly
本文介绍了'替换'来自< algorithm>运作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于某种原因,< algorithm>
中的替换
模板不希望对我有效。请参阅下面的示例代码:
For some reason, the replace
template from <algorithm>
does not want to work for me. See example code below:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s = "Hello, world!";
replace(s.begin(), s.end(), "l", "i");
cout << s << endl;
return 0;
}
代码没有语法错误,但编译器仍然给我带来以下错误:
There are no syntax errors with the code, but the compiler is still throwing me the following errors:
In file included from /usr/include/c++/4.9/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/4.9/bits/stl_algo.h: In instantiation of 'void std::replace(_FIter, _FIter, const _Tp&, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _Tp = char [2]]':
prog.cpp:8:38: required from here
/usr/include/c++/4.9/bits/stl_algo.h:4234:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (*__first == __old_value)
^
/usr/include/c++/4.9/bits/stl_algo.h:4235:13: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
*__first = __new_value;
^
谢谢,全部!
我尝试了什么:
我尝试了多个版本,但似乎没有任何效果。谷歌只给了我模板,我已经知道了。
Thanks, all!
What I have tried:
I have tried multiple versions of this, but nothing seems to work. Google just gives me the template, which I already know.
推荐答案
你的调用替换(迭代器,迭代器,const char *,const char * )
与std :: replace
模板不匹配:
Your callreplace(iterator, iterator, const char*, const char *)
does not match thestd::replace
template:
template<typename _FIter, typename _Tp>
void
replace(_FIter, _FIter, const _Tp&, const _Tp&);
_Tp
是 char
这里因为 std :: string
迭代器指向 char
。
所以你必须使用
_Tp
is char
here because the std::string
iterators are pointing to char
.
So you must use
replace(s.begin(), s.end(), 'l', 'i');
这篇关于'替换'来自< algorithm>运作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文