[C++]字符串处理

字符串按行读入

getline(cin, s1);

字符串删除指定字符

通过algorithm中的remove将值为val的元素全部移动到末尾,并返回newend的迭代器
利用string.erase将newend和end范围内的元素删除

s1.erase(remove(s1.begin(), s1.end(), s2[i]), s1.end());

find函数的区别

在string和algorithm中都提供了find

std::find function template

emplate <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);

输入参数

查找范围的迭代器,以及查找值

返回值

第一个匹配元素的迭代器,没有匹配的结果返回last迭代器

std::string::find

重载

string (1)
size_t find (const string& str, size_t pos = 0) const noexcept;
c-string (2)
size_t find (const char* s, size_t pos = 0) const;
buffer (3)
size_t find (const char* s, size_t pos, size_type n) const;
character (4)
size_t find (char c, size_t pos = 0) const noexcept;

输入参数

str/s/c:要寻找的字符串/字符
pos :寻找的起始位置
n :匹配的长度

返回值

返回找到的第一个匹配元素的位置(size_t)
没找到则返回string::npos

输出指定精度

printf("%.2f",num)

转载于:https://www.cnblogs.com/wendyy/p/9332629.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值