目录
一、STL概述
STL(Standard Template Library标准模版库),是惠普实验室开发的一系列软件的统称。
为了建立数据结构和算法的一套标准,降低它们之间的耦合关系,以提升各自的独立性、弹性、交互操作性(相互合作性,interoprrability),诞生了ST
其实就是提高 代码复用性,制造出可重复运用的代码,而不需要自己再去写已经存在的代码。
二、STL基本概念
STL从广义上分为:容器(container)、算法(alhorithm)、迭代器(iterator)
【容器和算法之间通过迭代器进行无缝连接,算法 其实是 通过迭代器 操作 容器数据】
- STL主要出现在c++中,但在c++引入前该技术已经存在很长时间了。
- STL几乎所有代码都采用了 模板类 或者 模板函数 。这相比传统的 由函数和类组成的库 来说,提供了更好的 代码重用机会。
- 在c++标准程序库中,隶属于STL的占到了80%以上。
三、STL六大组件
容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
STL的一个重要特性是 将数据和操作分离,数据由 容器类别 加以管理,操作则由 特定算法 完成。
1.容器:存放数据
2.算法:操作数据
算法分为 质变算法 和 非质变算法。
质变算法:运算过程中,会更改区间内的元素内容。例如拷贝、替换、删除等等。
非质变算法:运算过程中,不会更改区间内元素内容。例如查找、计数、遍历、寻找极值等。
3.迭代器:算法 只能借助迭代器 操作容器数据(容器和迭代器一一对应)。
4.仿函数:为算法提供更多策略
eg:排序函数默认是从小到大排序,用仿函数让它可以从大到小排序,提供更多的策略。
5.适配器(配接器):为算法提供更多参数的接口
eg:函数默认是传1个参数,用适配器让它可以传2个参数,这样的
6.空间配置器:为算法和容器 动态分配、管理空间
最常用的容器是 vector 和 lis t容器。
四、string(类)容器
1.string容器基本概念
c字符串是 以空字符结尾的字符数组,不适合大程序开发,所以c++标准库定义了一种string类。
- char*是一个指针,string是一个类;
- string封装了char*,管理这个字符串,是一个char型的容器;
- string封装了很多实用的成员方法:
【查找find,拷贝copy,删除delete,替换replace,插入insert】
- 不用考虑内存释放和越界
【string管理char*所分配的内存,每一次string的复制、取值都由string类负责维护,不用担心复制越界和取值越界等 】
2.string容器常用操作
(以下都是函数定义。string&是返回值,调用的时候用的是函数名,也就是operator、assign这样的,懂吧)
【不管什么容器,看到assign就是赋值,operator是运算符重载】
【3.[ ]越界不会抛出异常,at方法会】
【5.find返回第一次出现位置,rfind返回最后一次出现位置,没找到返回-1;没传pos默认从头开始找】
#include<iostream>
#include<string>//string.h是c的头文件,string是类的
using namespace std;
#include<iomanip>//input output manipulator:输入输出格式控制
void test1()
{
string str;//1.1
cout << str << endl;
string str1("hello");//1.3
cout << str1 << endl;
string str2(5, 'A');//1.4
cout << str2 << endl;
string str3=str2;//1.2 拷贝构造
cout << str3 << endl;
string str4;
str4 = "hello";//2.1
cout << str4 << endl;
str4.assign("world");//2.4
cout << str4 << endl;
str4 = "W";//2.3
cout << str4 << endl;
str4.assign("hello",3);//2.5
cout << str4 << endl;
string str5 = "hello";
str4.assign(str5, 2, 2);//2.8
cout << str4 << endl;
}
void test2()
{
//3.1+3.2
string str1 = "hello";
cout << str1[1]<<" "<<str1.at(1) << endl;
str1[1] = 'E';
str1.at(6) = 'H';
cout << str1[1] << " " << str1.at(1) << endl;
//[]越界不会抛出异常,at方法会
try
{
//str1[100] = 'A';
str1.at(100) = 'A';
}
catch (exception& e)
{
cout << "捕获到异常:" << e.what() << endl;
}
}
void test3()
{
string str1 = "hello";
str1 += " world";//4.2
cout << str1 << endl;
string str2 = "hehe";
str1 += str2;//4.1
cout << str1 << endl;
string str3 = "hello";
string str4 = "world";
cout << str3 + str4 << endl;//重载+运算符
string str5 = "hello";
string str6 = "world";
str5.append(str6, 2, 3);//4.7
cout << str5 << endl;
string str7 = "hello";
str7.append("world",3);//4.5
cout << str7 << endl;
}
void test4()
{
string str1 = "http://www.sex.777.sex.com.sex";
cout << str1 << endl;
while (1)
{
int ret=str1.find("sex");//5.2
if (ret == -1)
break;
str1.replace(ret, 3, "***");//5.10
}
cout << str1 << endl;
}
void test5()
{
string str1 = "hehe";
string str2 = "haha";
//6
if (str1.compare(str2) == 0)
cout << "相等" << endl;
else if(str1.compare(str2)>0)
cout << "大于" << endl;
else if (str1.compare(str2)< 0)
cout << "小于" << endl;
//>、=、<重载
if (str1==str2)
cout << "相等" << endl;
else if (str1> str2)