一、STL
STL(standard template libaray-标准模板库): 是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
二、string
1. 定义
string是一个字符串的模板类
int main()
{
string s1("hello");//构造对象
return 0;
}
可以看出string是std标准库里的,且是typedef出来的
库里定义:
template<class T>
class basic_string
{
pr
}
可以看出string原来是一个类模板,但它的模板类型是**char
**
但这里不能直接就是以char为类型写string,要使得模板更加能利用,因为存在其他字符串类型
编码 由值+符号建议映射关系——形成编码表
ascii编码表——表示英文编码表
unicode——表示全世界文字编码表 utf-8(Linux下)
utf-16 utf-32
gbk——中文自己量身定做都编码表,中国汉字用两个字节来表示
2. 常用接口
构造函数:
红色是常用的
这里第三个可以控制自己想要的拷贝构造的长度,
npos指的是string里面的一个静态变量,实际是-1,给size_t,无符号整型,-1是最大的。实际上就是拷贝构造到最末尾
第五个是字符串的前几个字符去拷贝构造,第六个是以n个一个字符去构造
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;//无参构造
string s2("abc");//传参构造
string s3(s2);//拷贝构造
cin >> s1;
cout << s2 << endl;
cout << s3 << endl;
cout << s1 << endl;
string s4(s1);
cout << s4 << endl;
return 0;
}
字符串长度:
不包含‘\0’
最好用size
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
string s2("abc");
string s3(s2);
cout << s2.length() << endl;
cout << s2.size() << endl;
return 0;
}
申请空间大小:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
cout << s1.capacity() << endl;
return 0;
}
清除数据:
数据虽然没了,但开辟的空间还在
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
cout << s1 << endl;
s1.clear();
cout << s1 << endl;
cout << s1.capacity() << endl;
return 0;
}
删除数据
本质是顺序表,尽量减少头部删除,因为需要挪动数据
字符串数组:
二者相似,但检查越界的方式不一样,operator断言,at抛异常
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
cout << s1 << endl;
s1.clear();
cout << s1 << endl;
cout << s1.capacity() << endl;
return 0;
}
添加字符:
int main()
{
string s1;
s1.push_back('a');
s1.append("bcde");
cout << s1 << endl;
s1 += ':';
s1 += "hello world";
cout << s1 << endl;
return 0;
}
支持头插尾插,但因为是顺序表所以效率很低
遍历字符串:
- 普通size遍历,注意返回值是size_t
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
for (size_t i = 0; i < s1.size(); ++i)
{
cout << s1[i];
}
return 0;
}
- 迭代器
访问和修改容器的
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
//iterator在string的类域里面
/
string::iterator it = s1.begin();
//返回第一个数据的地址
while (it != s1.end())//返回最后一个数据地址'\0'
{
cout << *it;
++it;
}
//倒着输出
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *it;
++it;
}
return 0;
}
迭代器遍历的意义是什么呢?
所有的容器都可以使用迭代器这种方式去访问修改
对于string,无论是正着遍历,倒着遍历,下标+ []都足够好用,为什么还要迭代器呢?
对于string,下标和[]就足够好用,确实可以不用迭代器。
但是如果是其他容器(数据结构)呢?
- 范围for,语法糖
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
for (auto input : s1)
{
cout << input;
}
return 0;
}
开空间:
这里开空间不一定是正好,一般是1.5-2倍逐渐开空间
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
s1.reserve(100);
cout << s1.capacity() << endl;
return 0;
}
resize和reserve区别
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
s1.reserve(100);
string s2;
s2.resize(100);
return 0;
}
resize会初始化、
通过对比size就可以发现
打印字符串:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abc");
cout << s1 << endl;
cout << s1.c_str() << endl;
return 0;
}
二者的返回值不同,
const char* //可以通过c_str()获取字符串的地址
//例子
string s1;
st1.c_str();
查找字符串:
find返回的是找到该字符串的第一个字符的位置
查找顺序是从左往右寻找
这里有rfind,是从右往左找
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("test.txt");
cout << s1.find(".txt");
return 0;
}
和substr组合使用
string substr (size_t pos = 0, size_t len = npos) const;//起始位置,长度
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("test.txt");
size_t pos = s1.find('.');
cout << s1.substr(pos, s1.size() - pos);
return 0;
}