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= 赋值运算符重载 码字不易,请关注一下或点个赞吧!