c – std :: replace难度大
发布时间:2020-12-16 10:24:25 所属栏目:百科 来源:网络整理
导读:我还是STL的新手,想用k替换字符串中所有出现的ch. 我尝试了以下方法: std::replace (str.begin(),str.end(),"ch","k"); 但它抛出了这个错误: no matching function for call to ‘replace(__gnu_cxx::__normal_iteratorchar*,std::basic_stringchar,std::c
我还是STL的新手,想用k替换字符串中所有出现的ch.
我尝试了以下方法: std::replace (str.begin(),str.end(),"ch","k"); 但它抛出了这个错误: no matching function for call to ‘replace(__gnu_cxx::__normal_iterator<char*,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,__gnu_cxx::__normal_iterator<char*,const char [2],const char [1]) 在这种情况下如何让替换工作? 注意:我看到了一个类似的问题但在这种情况下OP使用“blah”和’b’作为要替换的参数,但这里我的两个参数都是字符串. 解决方法
由于
std::replace 的定义是
template< class It,class T > void replace( It first,It last,const T& old_value,const T& new_value ); 参考http://en.cppreference.com/w/cpp/algorithm/replace 你必须传递一个char,因为std :: string是std :: basic_string< char>和T是一个char. 例如: std::replace (str.begin(),'c','k'); 要解决您的问题,请阅读:How do I replace all instances of a string with another string? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby-on-rails – find_or_create竞争条件
- ios – XCTestCase禁用快照辅助功能层次结构
- 单元测试 – 编写源代码之前或之后的TDD和测试?
- ruby-on-rails – Rails没有路由匹配
- Ajax 跨域请求 Access-Control-Allow-Origin 问题
- Flash务实主义(七)——构建易维护的程序:高效修改
- objective-c – 在NSCache中使用NSPurgeabledata有什么意义
- VB & SQLServer 存储过程
- 新手学习FPGA----如何将软硬件程序固化进串行Flash芯片EPCS
- reactjs – react-native:无法从”… / node_modules / wi