c++快速替换字符串指定字符

   项目中偶尔要用到替换指定字符的功能,因为不常用每次一用到的就去官方文档看,总得花点儿时间感觉不是那么好用,自己想了个办法,可能性能不是最好的但我感觉应该是最直观明了的;以下是实现:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	std::string str("123 232 434 65r"); //替换所有空格
	std::vector<char> vec_str = std::vector<char>(std::make_move_iterator(str.begin()), std::make_move_iterator(str.end()));//move 如果不用 make_move_iterator 则是copy
	std::for_each(vec_str.begin(), vec_str.end(), [](char& c){
		if (c == ' ')c = '_';
	});
	str = std::string(std::make_move_iterator(vec_str.begin()), std::make_move_iterator(vec_str.end()));
	std::cout << str << std::endl;
	return 0; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值