acmilan作者
11年6个月前IP:四川
773479
3楼
今天我把上边的代码移植回Visual Studio 2010,发现regex_replace有一个地方出错了:
regextesstdlg.cpp(267): error C2784: “std::basic_string<_elem> std::tr1::regex_replace(const std::basic_string<_elem> &,const std::tr1::basic_regex<_elem,_rxtraits> &,const std::basic_string<_elem> &,std::tr1::regex_constants::match_flag_type)”: 未能从“const wchar_t [11]”为“const std::basic_string<_elem> &”推导 模板 参数
regextesstdlg.cpp(267): error C2780: “_OutIt std::tr1::regex_replace(_OutIt,_BidIt,_BidIt,const std::tr1::basic_regex<_elem,_rxtraits> &,const std::basic_string<_elem> &,std::tr1::regex_constants::match_flag_type)”: 应输入 6 个参数,却提供了 3 个
regextesstdlg.cpp(267): error C2228: “.c_str”的左边必须有类/结构/联合
regextesstdlg.cpp(277): error C2780: “std::basic_string<_elem> std::tr1::regex_replace(const std::basic_string<_elem> &,const std::tr1::basic_regex<_elem,_rxtraits> &,const std::basic_string<_elem> &,std::tr1::regex_constants::match_flag_type)”: 应输入 4 个参数,却提供了 6 个
regextesstdlg.cpp(277): error C2784: “_OutTy *std::tr1::regex_replace(_OutTy (&)[_OutSize],_BidIt,_BidIt,const std::tr1::basic_regex<_elem,_rxtraits> &,const std::basic_string<_elem> &,std::tr1::regex_constants::match_flag_type)”: 未能从“std::_String_iterator<_elem,_traits,_alloc>”为“_OutTy (&)[_OutSize]”推导 模板 参数
regextesstdlg.cpp(277): error C2784: “_OutIt std::tr1::regex_replace(_OutIt,_BidIt,_BidIt,const std::tr1::basic_regex<_elem,_rxtraits> &,const std::basic_string<_elem> &,std::tr1::regex_constants::match_flag_type)”: 未能从“const wchar_t [11]”为“const std::basic_string<_elem> &”推导 模板 参数</_elem></_elem></_elem,_rxtraits></_elem,_traits,_alloc></_elem></_elem,_rxtraits></_elem></_elem,_rxtraits></_elem></_elem></_elem></_elem,_rxtraits></_elem></_elem></_elem,_rxtraits></_elem></_elem>
最后通过intellisense发现Visual Studio 2010对C++ TR1的支持并不完善,regex_replace第三个参数fmt只能使用wstring类型,不能使用wchar_t *类型。于是把
m_rslt +=
    regex_replace(srcstr, reg1, L"$1.$2.x.$4") // wstring regex_replace(字符串, 正则, 表达式)
    .c_str();
// 以及。。。
regex_replace(resultstr.begin(), srcstr.begin(), srcstr.end(), // 使用输入和输出迭代器
              reg1, L"$1.$2.x.$4",
              regex_constants::format_first_only); // 只替换第一个
改为
m_rslt +=
    regex_replace(srcstr, reg1, (wstring)L"$1.$2.x.$4") // wstring regex_replace(字符串, 正则, 表达式)
    .c_str();
// 以及。。。
regex_replace(resultstr.begin(), srcstr.begin(), srcstr.end(), // 使用输入和输出迭代器
              reg1, (wstring)L"$1.$2.x.$4",
              regex_constants::format_first_only); // 只替换第一个
问题解决。现上传VS2010版本的示例程序。
attachment icon RegExTesst.rar 1.37MB RAR 60次下载
评论
文号 / 773479

千古风流
名片发私信
学术分 4
总主题 466 帖总回复 2942 楼拥有证书:进士 学者 笔友
注册于 2009/05/30 21:22:42最后登录 2019/01/31 17:16:38
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:0

个人简介

暂未填写
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的