前言
大家在学习STL库的时候一定要学会看英文文档,俗话说熟能生巧,所以还得多练!在使用string类之前,要包含头文件#include <string>和using namespace std;
一、string——构造相关操作
1. string(); (常用)
string s1;
2. string(const char *s); (常用)
string s2("hello world");
3. string(const string& str); (常用)
string s2("hello world");
string s3(s2);
4. string& operator=(const string& str); (常用)
string s2("hello world");
string s4 = s2;
5. string& operator=(const char *s); (常用)
string s5 = "hello world";
6. string& operator=(char c);
这里有一点需要注意,赋值运算符重载和拷贝构造的区别,两个对象都是定义之后的=是赋值运算符重载,如果是在定义的时候的=,属于拷贝构造;
string s6;
s6 = 'x';
7. string (const string& str, size_t pos, size_t len = npos);
string s7 = "hello world";
string s8(s7, 0, 3);
8. string (const char* s, size_t n);
从s上拷贝前n个字符
string s9("hello world", 4);
9. string (size_t n, char c);
拷贝n个一样的字符
string s10(10, 'x');
10. string (InputIterator first, InputIterator last);
用迭代器拷贝,first为头,last为尾,拷贝first<= string <last,也就是拷贝从first位置开始,在last前一个位置停下来,不拷贝last位置;(默认迭代器的end是在位置)
string s11 = "hello world";
string s12(s11.begin(), s11.end()-1);
二、string——打印相关操作
1. 输入字符串cin
string s1;
cin >> s1;
cout << s1 << endl;
2. 输出字符串cout
3. 获取字符串getline
string s1;
getline(cin, s1);
cout << s1 << endl;
三、string——访问和遍历相关操作
1. char& operator[] (size_t pos);
string s1 = "hello world";
cout << s1[0] << endl;
2. char& at (size_t pos);
string s1 = "hello world";
cout << "字符串第5个字符为:" << s1.at(4) << endl;
3. iterator begin();和 iterator end();
string s1 = "hello world";
string::iterator begin = s1.begin();
string::iterator end = s1.end();
string s1 = "hello world";
string::iterator begin = s1.begin();
string::iterator end = s1.end();
while(begin < end)
{
cout << *begin << ' ';
++begin;
}
cout << endl;
string s1 = "hello world";
string::iterator begin = s1.begin();
string::iterator end = s1.end();
cout << "访问第1个元素为:" << *begin << endl;
cout << "访问第5个元素为:" << *(begin + 4) << endl;
cout << "访问倒数第3个元素为:" << *(end - 3) << endl;
4. reverse_iterator rbegin();和 reverse_iterator rend();
string s1 = "hello world";
string::reverse_iterator rb = s1.rbegin();
string::reverse_iterator re = s1.rend();
while(rb < re)
{
cout << *rb << ' ';
++rb;
}
5. 范围for遍历
string s1 = "hello world";
for(auto e : s1)
{
cout << e << ' ';
}
cout << endl;
四、string——容量相关操作
1. size
string s1 = "hello world";
int len = s1.size();
cout << len << endl;
2. capacity
string s1 = "hello world";
cout << s1.capacity() << endl;
3. empty
string s1 = "hello world";
cout << s1.empty() << endl;
string s2;
cout << s2.empty() << endl;
4. clear
string s3 = "hello world";
cout << "清空之前的容量:" << s3.capacity() << endl;
cout << "清空之前的长度:" <<s3.size() << endl;
s3.clear();
cout << "清空之后的容量:" << s3.capacity() << endl;
cout << "清空之后的长度:" <<s3.size() << endl;
5. reserve
为字符串保留空间
规则:
1. 扩容:当保留的空间 > capacity,认为扩容。每个编译器扩容的空间是不同的,总体上看就是:实际扩容下的空间≥要保留的空间;
2. 缩容:当保留的空间 < capacity ,认为缩容。有的编译器不会缩容,有的编译器会缩容,如果保留的空间<size,只缩容到size大小。
string s4 = "hello world";
cout << "扩容之前的容量:" << s4.capacity() << endl;
s4.reserve(100);
cout << "扩容之后的容量:" << s4.capacity() << endl;
string s4 = "hello world";
cout << "缩容之前的容量:" << s4.capacity() << endl;
s4.reserve(2);
cout << "缩容之后的容量:" << s4.capacity() << endl;
6. resize
void resize (size_t n);
void resize (size_t n, char c);
将有效字符的个数该成n个;
string s5 = "hello world";
cout << s5 << endl;
cout << "调整有效字符之前的容量:" << s5.capacity() << endl;
cout << "调整有效字符之前的长度:" <<s5.size() << endl;
s5.resize(30);
cout << s5 << endl;
cout << "调整有效字符之后的容量:" << s5.capacity() << endl;
cout << "调整有效字符之后的长度:" <<s5.size() << endl;
string s5 = "hello world";
cout << s5 << endl;
cout << "调整有效字符之前的容量:" << s5.capacity() << endl;
cout << "调整有效字符之前的长度:" <<s5.size() << endl;
s5.resize(30, 'x');
cout << s5 << endl;
cout << "调整有效字符之后的容量:" << s5.capacity() << endl;
cout << "调整有效字符之后的长度:" <<s5.size() << endl;
size < n < capacity ,size = n,capacity不变;若提供c,则后面全为c字符,若没提供,则为‘’;
五、string——增加操作
1. string& operator+= (char c);
尾插字符
string s1 = "hello world";
cout << s1 << endl;
s1 += 'x';
cout << s1 << endl;
2. string& operator+= (const string& str);
尾插字符串
string s1 = "hello world";
cout << s1 << endl;
s1 += s1;
cout << s1 << endl;
3. string& operator+= (const char* s);
尾插字符串
string s1 = "hello world";
cout << s1 << endl;
s1 += " hello CSDN";
cout << s1 << endl;
4. string& insert (size_t pos, const string& str);
string s1 = "hello world";
string s2 = "CSDN";
cout << "插入之前:" << s1 << endl;
s1.insert(1, s2);
cout << "插入之后:" << s1 << endl;
更多的应用:string::insert – C++ Reference
六、string——删除操作
1. string& erase (size_t pos = 0, size_t len = npos);
npos代表-1,len为size_t类型,为无符号类型,就会被转换为整型的最大值,所以len为整型最大值,在不提供实参,就默认为npos;
string s1 = "hello world";
cout << "删除之前:" << s1 << endl;
s1.erase(1,3);
cout << "删除之后:" << s1 << endl;
2. iterator erase (iterator p);
删除迭代器所处的位置的字符
string s1 = "hello world";
cout << "删除之前:" << s1 << endl;
s1.erase(s1.begin());
cout << "删除之后:" << s1 << endl;
3. iterator erase (iterator first, iterator last);
string s1 = "hello world";
cout << "删除之前:" << s1 << endl;
s1.erase(s1.begin(), s1.end()-1);
cout << "删除之后:" << s1 << endl;
七、string——查找操作
在pos位置向后查找str这个string类的对象,返回找到的字符串的第一个位置的下标,没找到就返回npos |
size_t find (const string& str, size_t pos = 0) const; |
---|---|
在pos位置向后查找s这个字符串,返回找到的字符串的第一个位置的下标,没找到就返回npos |
size_t find (const char* s, size_t pos = 0) const; |
在pos位置向后找,找s的前n个字符,找到返回第一个位置的下标,找不到返回npos |
size_t find (const char* s, size_t pos, size_t n) const; |
在pos位置向后查找字符c,返回找到的字符位置的下标,没找到就返回npos |
size_t find (char c, size_t pos = 0) const; |
具体查看:hstring::find – C++ Reference
八、string——构造子串
string s2 = "hello world";
size_t n = s2.find('w', 0);
string s3 = s2.substr(n,s2.size() - n);
cout << s2 << endl;
cout << s3 << endl;
九、string——比较操作
relational operators (string) – C++ Reference
十、好用的函数
1. 字符串转换整数
2. 整数转换字符串
十一、不同的编码要使用对应的string
UTF-8 | string |
UTF-16 | u16string |
UTF-32 | u32string |
宽字符(双字节字符) | wstring |
原文地址:https://blog.csdn.net/2302_76941579/article/details/134653368
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_40514.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!