c++泛型算法中replace的问题

来源:百度知道 编辑:UC知道 时间:2024/09/22 04:36:47
c++菜鸟 看书编了个小程序 不知道哪里错了
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string str[]={"hello","bye","nono","yes"};
vector<string> sevc(str,str+4);
replace(sevc.begin(),sevc.end(),"hello","haha");
for(int i=0;i<sevc.size();i++)
cout<<sevc[i]<<" ";
return 0;
}
为什么出错了呢?

能学到STL的算法已经恨牛逼了。
应该改成
replace(sevc.begin(),sevc.end(),string("hello"),string("haha"));
因为算法中使用模板做形参,所以需要明确类型信息。直接写"hello"编译器不知道该处理成什么类型,所以出错。

str+3吧