C++ STL教程之 string 常用成员函数

3 篇文章 0 订阅

C++ STL,全称 C++ Standard Template Library,C++ 标准模板库

本篇内容:C++ STL 之 string 常用成员函数 / 变量

 

1、bool empty() const                 返回字符串是否为空

2、void clear()                              删除字符串所有内容

3、size_type length() const        返回 string 对象长度(不含'\0')

4、size_type size() const            同等于 length(),length() 是较早的版本中采用的

5、size_type max_size() const   返回存储字符的最大值,比 常量 npos 小1

6、static const size_type npos   能为 string 对象分配的最大容量,通常为 (unsigned long long) -1,即 unsigned long long 的最大值

7、const char* c_str() const        将 string 对象转为 const char* 并返回(不能直接给char*赋值)

8、const char* data() const          与 c_str() 相似,但不含末尾的'\0',建议使用c_str()

9、size_type copy(char* c, size_type n, size_type pos = 0)

将原 string 对象中从位置 pos 开始的地方复制 n 个字符到 c 中,返回值为复制的字符数

10、size_type capacity() const    返回已分配的内存

11、static const unsigned long long npos = (unsigned long long)-1

string存放的最大字符数

12、string &erase(pos=0, n=npos) 删除从pos开始的n个字符

12:2、string &erase(const_iterator c) 删除迭代器指定的字符

13、const_iterator begin()            返回第一个字符的迭代地址,与 iterator 类型连用

14、const_iterator end()               返回最后一个字符 + 1的位置,与 iterator 类型连用

15、iterator                                    string 的 STL迭代器类型

16、at(pos)                                     string s = "abc"; 则 s.at(1) = s[1] = 'b'

17、ostream& operator<<(ostream&, string&)

cout << s; 的形式

18、istream& operator>>(istream&, string&)

cin >> s; 的形式

19、find函数                                    查找字符

20、find_first_of

        find_first_not_of

        find_last_of

        find_last_not_of

        查找字符,根据字面意思理解

21、rfind                                          等于 find_last_of

22、string& operator=               赋值运算符重载

码字不易,请关注一下或点个赞吧!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jcShan709

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值