码迷,mamicode.com
首页 > 编程语言 > 详细

[C++]字符串处理

时间:2018-07-18 23:18:49      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:temp   ring   end   str   ++   code   字符串   line   lock   

字符串按行读入

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)

[C++]字符串处理

标签:temp   ring   end   str   ++   code   字符串   line   lock   

原文地址:https://www.cnblogs.com/wendyy/p/9332629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!