1 string 类的常用构造
#include<iostream>
using namespace std;
#include<string>
// string类对象的常用构造
int main(){
string s1;// 构造空字符串(里面\0)
string s2("hello world");//用c语言方式构造
string s3(10, '$');
string s4(s3);// 拷贝构造
cout << s2 << endl;//默认size为16 显示为15 其中一个保存/0
cout << s3 << endl;
cout << s4 << endl;
//cin >> s1;
getline(cin, s1);// getline 函数从终端获取字符串 输入完 回车
cout << s1 << endl;
system("pause");
return 0;
}
2 string类的容量操作
int main(){
string s("hello");
cout << s << endl;
cout << s.size() << endl;//有效字符个数 后来给定的
cout << s.length() << endl;//有效字符个数 刚开始给定的
cout << s.capacity() << endl;
if (s.empty()){
cout << "empty string" << endl;
}
else{
cout << "not empty string" << endl;
}
s.clear();// 清空字符串
if (s.empty()){
cout << "empty string" << endl;
}
else{
cout << "not empty string" << endl;
}
cout << s.capacity() << endl;//容量仍为15
system("pause");
return 0;
}
3 string 类的相关操作函数
1 resize函数
// resize函数 : 将有效字符的个数改成n个 (n 为我们想要的数字)
// 我们的预期的元素个数大于容量时需要扩容 n>capacity 时 扩容
// 我们给定的元素个数小于原本容量时,如果给定的字符个数比原本字符个数还要少,此时需要尾删,将超出我们给定字符个数范围的字符尾删掉
// 即 :当 n<capacity && n<size 时 采取尾删,将字符个数改成我们想要的
#if 0
int main(){
string s("hello");
cout << s.size() << endl; // 此时有效字符为5
cout << s.capacity() << endl;// 容量为16 显示为15 留出一个来存放\0
// n小于capacity
s.resize(10, '$'); //如果重新给定的size大于原来的,则多出来的剩余空间用我们给定的字符来填充 ,如果我们没有给定一般默认0来填充
cout << s.size() << endl;// 10
cout << s.capacity() << endl; // 容量为16 显示为15 留出一个来存放\0
// n 大于capacity: 此时需要扩容: 另开辟新空间 拷贝本来的元素同时填充我们给定字符ch 并记着释放旧空间
s.resize(30, '8');// 扩容
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(24);// 容量不变(以空间换时间) 有效元素个数减少 类似于尾删
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(2);
cout << s.size() << endl;
cout << s.capacity() << endl;
system("pause");
return 0;
}
2 reserve函数
// reserve函数
// 该函数不改变元素个数的大小,只改变空间的大小(只会将原来空间变大)
// 当我们给定的空间(newcapacity) 大于原本空间(oldcapacity)时,此时需要扩容
// 当我们给定的空间(newcapacity)小于原本空间(oldcapacity)时 此时直接忽略reserve 函数,不需要扩容
int main(){
string s("hello");// 以字符串形式构造string类对象
cout << s.size() << endl;// 5
cout << s.capacity() << endl;// 容量为16 显示为15 留出一个来存放\0
// newcapacity<oidcapacity时 reserve不改变底层容量大小(直接忽略reserve)
s.reserve(10);// 将string 类的对象的容量改为10
cout << s.size() << endl;
cout << s.capacity() << endl;
//newcapacity>oldcapacity
s.reserve(20);
cout << s.size() << endl;// 元素个数仍为 5
cout << s.capacity() << endl;// 此时需要扩容 按照特定算法扩容 扩容后容量为32 显示为31 留一个存储\0
// 此时 newcapacity<oidcapacity reserve不改变底层容量大小(直接忽略reserve)
s.reserve(16);
cout << s.size() << endl;// 元素个数 为 5
cout << s.capacity() << endl;// 显示为31 留一个存储\0
// 此时 newcapacity<oidcapacity reserve不改变底层容量大小(直接忽略reserve)
s.reserve(3);
cout << s.size() << endl; // 元素个数仍为 5
cout << s.capacity() << endl;
system("pause");
return 0;
}
3 string类的对象的访问及其遍历
/访问任意位置字符
int main(){
string s("hello");// 构造string类对象
//cout << s[3] << endl;//类似于数组访问
reverse(s.begin(), s.end());// 逆置 字符串
cout << s << endl;
//正向遍历string类(迭代器)(遍历方式1)
//string::iterator it = s.begin();
auto it = s.begin();
while (it != s.end()){
cout << *it;
++it;
}
cout << endl;// 打印\n 换行
// 反向迭代器 :反向遍历
auto rit = s.rbegin();
while (rit != s.rend())
{
cout << *rit;
++rit;
}
// 范围for 遍历(遍历方式2)
for (auto e : s){
cout << e<<" ";
cout << endl;
}
// 常规 for 遍历(遍历方式3)
for (size_t i = 0; i < s.size(); ++i){
cout << s[i];
}
cout << endl;
system("pause");
return 0;
}
4 string类对象的修改操作
int main(){
string s;
s += 'I';// 在字符串后追加 字符 'I'
s += " Love "; //在字符串后追加 字符串" Love "
string str("myself ");// 定义并且构造另一个类的对象
s += str;// 将对象(str) 追加到 对象 d的后面 相当于字符串的拼接函数 :strcat
cout << s << endl;
s.push_back('!');// 在对象 s 后面追加 字符 '!'
size_t pos=s.find('!');//找字符 '!' 并将该字符所在位置返回
if (pos != string::npos){
s.insert(pos,"^_^");
}
/*
// 将s中有效字符清空
s.erase();
s.erase(s.begin(), s.end());
s.clear();
*/
// 删掉笑脸
s.erase(s.rfind(' ') + 1, 3);// 从后往前开始找到第一个空字符,然后从距离该空字符1个位置的位置考试删除3个字符
// 替换字符
s.replace(s.find("myself"), 6, "you");// 从前往后找,找到myself然后用you 将其替换
// 将对象 s 和对象 str 交换
//swap(s,str);
s.swap(str);//建议用这个
system("pause");
return 0;
}
#if 0
int main()
{
// 在string类中 字符串可以直接比较
string s1("hello");
string s2("world");
if (s1 > s2)
{
cout << "s1 > s2" << endl;
}
else
{
cout << "s1 <= s2" << endl;
}
string s3;
s3 = s1 + s2;// 直接将两个字符串拼接
cout << s3 << endl;
system("pause");
return 0;
}
#endif