c++ - 如何在参考上进行 regex_replace
问题描述
我有这个方法:
std::string pluralize(std::string const& word) const {
std::regex_replace(word, m_pattern, m_replacement);
return word;
}
但它没有按预期工作。字符串不会被给定的规则替换。是否可以regex_replace
在引用上做而不是返回这个变量的引用?
解决方案
regex_replace
原地不变,但返回新的string
:
std::string pluralize(std::string const& word) const {
return std::regex_replace(word, m_pattern, m_replacement);;
}
如果要编辑原件string
:
void pluralize(std::string &word) const {
word = std::regex_replace(word, m_pattern, m_replacement);
}
如果你想要修改和返回:
std::string pluralize(std::string &word) const {
return word = std::regex_replace(word, m_pattern, m_replacement);
}
推荐阅读
- javascript - Svelte: is there a way to cache the api result in a way that it won't trigger the api call everytime the component renders?
- azure - 将下一个应用程序部署到 Azure 失败,因为需要重新安装 node_modules
- sql-server - 使用 T-SQL 函数,如何从 count(*) 函数返回整数?
- javascript - ReactJs 路由器更改 url 但不更改页面
- python - 创建一个打印 if 语句结果的函数
- r - 从汇总/聚合数据中绘制密度
- ruby-on-rails - Axios:XSRF Cookie 未在生产中随请求一起发送
- amazon-web-services - 使一列依赖于 Amazon QuickSight 中另一列的顺序
- c++ - 尝试 urldownloadtofile 函数时出错
- javascript - 未找到 nuxtjs 内容