目录
标准库中的string(std::string)
我们通过阅读文档 猛戳此处来进行详细学习
通过文档发现,string的底层其实也是模板
string类所用的头文件 #include<string>
1. string 的构造
发现C++98标准中有七个构造函数
当主要还要参考
例:
string s0; //会去调用默认构造,空字符串
string s1("hello string!"); //c_string
string s2(s1); //拷贝构造
string s3(s1,6,3); //下标是从0开始
//构造n个字符
string s6(6,'*');
//operator+ 和 operator=
string str1, str2,str3;
str1 = "hello "; //c_string
str2 = "string!";
str3 = str1 + str2;
cout << str3 << endl;
2. string 类的遍历
2.1 下标+[ ]
//使用下标+[]进行遍历
string str1 = "hello string!";
//遍历 字符串
for (size_t i = 0; i < str1.size(); i++) //s1.size()可以计算有多少个字符
{
cout << str1[i] << " "; //采用下标+[]进行遍历的方式
}
cout << endl; //换行
//当然string类中也有operator<<,operator<<的重载
cout<<str1;
也可以通过下标+[ ] 的形式修改值
//修改pos位置的字符
for (size_t i = 0; i < str1.size(); i++)
{
str1[i]++;
cout << str1[i] << " ";
}
注意这里的str1[i]++;相当于对下方代码返回的进行++
class string
{
public:
char& operator[](size_t pos)
{
return _str[pos];
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
这里的operator[ ] 有两种形式,读写 和 只读
const string s2("11222");
//s2[0]++; //不能修改
2.2 string 中的迭代器(Iterators)
(1) 迭代器遍历
//迭代器 iterator
//迭代器行为像指针一样的类型对象,定义区间左闭右开【)
string s1 = "my string`s iterator";
string::iterator it = s1.begin(); //s1.begin()记录第一个有效数据的位置
while (it != s1.end()) //s1.end() 记录最后一个有效数据的下一个位置
{
//遍历
cout << *it << " ";
++it;
}
(2) 使用迭代器修改内容
while (it1 != s1.end())
{
//修改内容
*it1 -= 3;
++it1;
}
//再次遍历
it1 = s1.begin(); //记得从begin开始
while (it1 != s1.end()) //s1.end() 记录最后一个有效数据的下一个位置
{
//遍历
cout << *it1 << " ";
++it1;
}
cout << endl;
(3) 反向迭代器遍历
当想要去逆序遍历,就可以去用反向迭代器
//反向迭代器
string str1("hello world");
string::reverse_iterator rit = str1.rbegin();
while (rit != str1.rend())
{
cout << *rit << " ";
rit++;
}
cout << endl;
注意下方的 const迭代器
小结一下:
一般分为四种迭代器:
- 正向迭代器 iterator
- 反向迭代器 reverse_iterator
- const 正向迭代器 const_iterator
- const 反向迭代器 const_reverse_iterator
2.3 范围for遍历
//范围for遍历
string s1 = "hello world!";
for (auto e : s1)
cout << e << " ";
3. string 中的Capacity
3.1. size() 和 length()
计算字符串的大小一般情况下使用size(),当然length()也可以,都是计算字符串的实际长度。
//string::size
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("this string");
cout<<str.size()<<endl; //结果输出 11
cout<<str.length()<<endl; //也是可以计算字符串的实际长度
return 0;
}
上述都可以计算字符串的长度,更建议使用size。
3.2 capacity()
string的capacity是返回 当前为字符串分配的存储空间的大小,用字节表示。即,当前最多可以容纳多少个字符。
//capacity
string str1("hello world");
//注意不包含'\0'占有的空间
cout << str1.capacity()<< endl;
str1.clear();//清空数据,不清空间
3.3 max_size()
string中的max_size()是计算字符串可以到达的最大长度。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("hello");
cout << str.size() << endl;//5
cout << str.length() << endl;//5
cout << str.capacity() << endl;
cout << str.max_size() << endl; //返回字符串可以达到的最大长度
return 0;
}
在vs2022下的x86下的运行结果:
3.4 reserve()
用于主动管理内存分配的成员函数,它允许你预先为字符串分配足够的存储空间(即调整 capacity),避免后续操作中频繁的内存重新分配。
语法:void reserve (size_t n = 0);
- 当n <= capacity()的时候,没有效果(不会去缩小当前的capacity)
- 当n > capacity()的时候,会重新分配内存,让capacity() >= n
string str("hello");
cout << str.size() << endl;
cout << str.capacity() << endl;
str.reserve(16);
cout << str.capacity() << endl;
//运行结果:
5
15
31
//注意不同编译环境下capacity()的大小会有所不同
3.5 clear()
string的clear 只是消除字符串的内容,将字符串变成空字符串(字符串长度是0),但是改变字符串的空间。
string str("hello");
cout << str.size() << endl;//5
cout << str.capacity() << endl;//15
str.clear();
cout << str.size() << endl; // 0
cout << str.capacity() << endl; // 15 ,不改变其空间的大小
//运行结果:
5
15
0
15
3.6 empty()
std::string::empty 是用来返回字符串是否为空。
string str("hello");
str.clear();
cout << str.size() << endl; // 0
if (str.empty())
{
cout << "this str is empty" << endl; //当前字符串长度为0
}
else
{
cout << "this str is non-empty" << endl;
}
//运行结果:
0
this str is empty
3.7 关于缩容shrink_to_fit() [C++11]
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello 111111111111111111111111111111111111111111");
cout << s1 << endl;
cout << s1.capacity() << endl;
cout << s1.size() << endl;
s1.clear(); //先清空数据
//缩容
s1.shrink_to_fit(); //shrink_to_fit()是C++11标准中的
cout << s1.capacity() << endl;
cout << s1.size() << endl;
return 0;
}
3.8 关于扩容 resize()
resize一般用于调整字符串的大小的。
语法:
void resize (size_t n);
void resize (size_t n, char c);
注意:
- 当 n 小于当前字符串的长度时,则当前字符串将缩短为其前 n 个字符,并删除第 n 个字符以外的字符。
- 当 n 大于当前字符串的长度时,则通过在末尾插入所需数量的字符来扩展当前内容,以达到 n 的大小。如果有指定的字符c,就使用字符c来填充,否则用’\0’来填充。
//查看扩容机制
string s;
size_t sz = s.capacity(); //先拿到首次的容量
cout << "init capacity: " << sz << endl;
cout << "making s grow: \n";
for (int i = 0; i < 100;++i)
{
s.push_back('a');
if (sz != s.capacity())
{
sz = s.capacity();//自动扩容
cout << "now capacity is: " << sz << endl;
}
}
cout << "capacity is changed end \n";
基于vs2022版的编译器
string s;
//reserve 保留,相当与进行扩容
s.reserve(100);
size_t sz = s.capacity(); //先拿到首次的容量
cout << "init capacity: " << sz << endl;
注意 当 reserve 设置的值比 capacity 大的时候会起作用
resize()的三种常见的情况:
情况1:当 n < size,会删除字符
string s2("hello world!");
cout << s2 << endl;
cout << "size is :" << s2.size() << endl;
cout << "capacity is :" << s2.capacity() << endl;
cout << "----------------------------\n";
//resize(n) 可以改变字符串的长度
//当 n < size,会删除字符
cout << "resize : \n";
s2.resize(5); //只会保留5个字符
cout << s2 << endl;
cout << "size is :" << s2.size() << endl;
cout << "capacity is :" << s2.capacity() << endl;
情况2:当 size<n<capacity,会插入’\0’
string s2("hello world!");
cout << s2 << endl;
cout << "size is :" << s2.size() << endl;
cout << "capacity is :" << s2.capacity() << endl;
cout << "----------------------------\n";
//当 size<n<capacity,会插入'\0'
cout << "resize : \n";
s2.resize(14); ////改变长度为14
cout << s2 << endl;
cout << "size is :" << s2.size() << endl;
cout << "capacity is :" << s2.capacity() << endl;
情况3:当 n>capacity,会进行扩容+插入
string s2("hello world!");
cout << s2 << endl;
cout << "size is :" << s2.size() << endl;
cout << "capacity is :" << s2.capacity() << endl;
cout << "----------------------------\n";
//当 n>capacity,会进行扩容+插入
cout << "resize : \n";
s2.resize(20); //改变长度为20
cout << s2 << endl;
cout << "size is :" << s2.size() << endl;
cout << "capacity is :" << s2.capacity() << endl;
4. sting中单个字符访问
4.1 operator[ ] 和 at 的区别
关于at
string s1("hello string!");
cout << s1[4] << endl; // o
cout << s1.at(4) << endl; // o
//区别:在于越界检查不同
try
{
s1[15];
}
catch (const exception& e)
{
cout << e.what() << endl;
}
[ ] 越界是断言报错
string s1("hello string!");
cout << s1[4] << endl;
cout << s1.at(4) << endl;
////区别:在于越界检查不同
//try
//{
// s1[15];
//}
//catch (const exception& e)
//{
// cout << e.what() << endl;
//}
//at 越界会抛异常
try
{
s1.at(15); //会抛异常
}
catch (const exception& e)
{
cout << e.what() << endl;
}
at越界会抛异常
4.2 back()和front()的其他用法
back和front的其他用法不仅仅可以获取单个字符,还可以修改字符。
string s1("-ello String-");
s1.back() = '!';
s1.front() = 'H';
cout << s1 << endl;
//运行结果:
Hello String!
string中的back()和front()是C++11中的。
5. string中的 Modifiers
5.1 operator+= 、append() 和 push_back()
这三个函数共同作用:都可以在字符串的末尾上追加内容
5.1.1 operator+=
//std::string::operator+=使用
string family("zhang");
string name("feng");
family += " san "; // c-string
family += name; // string
family += '!'; // character 单个字符串
cout << family; // zhang san feng!
5.1.2 append()
//std::string::append()使用
string str;
string str1("hello");
string str2 = "---world***";
str.append(str1); //string
str.append(str2,3,5); //substring,如果 str2 太短或 sublen 为 string::npos,会直接到str2的末尾
str.append("!!"); // c-string
str.append("%%33%%",2); //buffer
str.append(3,'6'); //fill
str.append(str2.begin()+8,str2.end()); //iterator
cout << str << endl;
//打印结果:helloworld!!%%666***
5.1.3 push_back()
std::string::push_back() 只能追加单个字符。
//std::string::push_back()
string str = "hello";
str.push_back('!'); //仅仅可以追加单个字符
cout << str << endl;
5.2 assign()
为字符串分配一个新值,替换其当前内容。
发现assign()的用法与append()用法类似
//std::string::assign()使用
string str("hello");
string str1("world");
str.assign(str1); //string
cout << str << endl;
str.assign(str1,0,-1); //substring
cout << str << endl;
str.assign("HE"); //c-string
cout << str << endl;
str.assign("HELLO",4); //buffer
cout << str << endl;
str.assign(6,'6'); //fill
cout << str << endl;
str.assign(str1.begin(),str1.end()); //iterator
cout << str << endl;
//打印结果:
world
world
HE
HELL
666666
world
5.3 insert()
std::string::insert() 成员函数用于在字符串的任意位置插入字符或子串。
//std::string::insert()的使用
string str("123");
string str1("abc");
//1 在pos位置插入str1
str.insert(3,str1);
cout << str << endl; //123abc
//2 在pos位置插入部分字符串
str.insert(6,str1,1,2);
cout << str << endl; //123abcbc
//3 在pos位置插入c-string
str1.insert(3,"---");
cout << str1 << endl; //abc---
//4 在pos位置插入字符串s的前n个
str1.insert(0, "111456",3);
cout << str1 << endl; //111abc---
//5 在pos位置插入n个字符c
str1.insert(1,2,'2');
cout << str1 << endl; //12211abc---
//6 使用iterator控制插入位置 插入当个字符c
string::iterator its;
its = str1.insert(str1.end(),'!');
cout << str1 << endl; //12211abc---!
//7 在 str3下标处插入str4的前三个字符
string str3 = "hello";
string str4 = "12345";
str3.insert(str3.begin()+2,str4.begin(),str4.begin()+3);//区间左闭右开
cout << str3 << endl; //he123llo
//像insert erase replace 谨慎使用
//因为频繁使用效率比较低
5.4 erase()
erase()只是清出字符串数据,并不会清理空间。
//std::string::erase
string str("hello-- world!");
cout << str.size() << endl; //字符长度14
cout << str.capacity() << endl; //当前空间15
cout << str << endl; //hello-- world!
// 1 string& erase (size_t pos = 0, size_t len = npos);
//删除指定位置的多个字符
str.erase(5,2); //清空pos位置开始的两个字符
cout << str << endl; //hello world!
// 2 iterator erase (iterator p);
//可以删除指定位置的单个字符
str.erase(str.begin()+5);
cout << str << endl; //helloworld!
// 3 iterator erase (iterator first, iterator last);
//删除指定范围的多个字符
str.erase(str.begin()+1,str.end()-3);
cout << str << endl; //hld!
// 4 当没有给参数的时候,默认清空整个字符串
str.erase();
cout << str << endl;
cout << str.capacity() << endl; //15
cout << str.size() << endl; //字符长度为0
5.5 replace() 和 swap()
replace()是替换字符串的一部分。
swap()是交互字符串。
replace()的一些简单用法:
//std::string::replace()
string str = "hello------world!";
string str1 = "this";
//1 替换部分字符串
//将str的下标6开始的4个字符替换为"this"
str.replace(6,4,str1);
cout << str << endl; //hello-this-world!
//2 替换整个字符串
str.replace(0,str.size(),"666");
cout << str << endl; //666
//3 借助迭代器
string st = "Hello, World!";
string replacement = "C++";
st.replace(st.begin() + 7, st.begin() + 12, replacement.begin(), replacement.end()); // 将 "World" 替换为 "C++"
cout << st << endl; //Hello, C++!
swap()的简单用法:
//swap()
string str("test swap!");
string str1("TEST SWAP!");
str.swap(str1);
cout << str << endl; //TEST SWAP!
5.6 pop_back()[C++11]
删除字符串的最后一个字符,从而有效地将其长度减少 1。
//std::string::pop_back()
string str("hello C++!!");
str.pop_back(); //pop_back()是C++11标准中的
cout << str << endl;//hello C++!
string中常用的还有 find、rfind、getline等函数。
string最常用的就是遍历和修改,上述介绍了部分的string的使用。