一、首先我们先来介绍下容器:
容器的概念:在数据存储上有一种对象类型,它可以持有其它对象或指向其它对象的指针
这种对象类型叫做容器
STL对定义的通用容器分为3类:顺序性容器、关联式容器和容器适配器
使用STL的好处:
1、STL是C++的一部分,不用额外安装什么,被内建在编译器之内
2、STL一个重要的特点就是数据结构和算法的分离
3、程序员不用思考STL的具体实现过程,只要能熟练使用就行了
4、STL具有高可重用性、高性能、高移植性、跨平台的优点
顺序性容器:vector、deque、list
每个元素都有固定位置,取决于插入时机和地点,和元素值无关
关联式容器:set、multiset、map、multimap
元素位置取决于特定的排序准则和插入顺序无关
二、迭代器
软件设计有一个基本原则,所有的问题都可以通过引进一个 间接层 来简化,
这种简化在STL中就是用 迭代器 来完成的。概括来说,迭代器在STL中用来将 算法
和 容器 联系起来,起着一种黏和剂的作用。几乎STL提供的所有算法都是通 过迭代器
存取元素序列进行工作的,每一个容器都定义了其本身所专有的迭代器,用以存取容器中的元素。
三、下面主要来介绍下相关容器
一、string的相关介绍
string 是STL的字符串类型,通常用来表示字符串
string 与 char *的区别
1、string 是一个类、char * 是一个指向字符的指针
2、string封装了char* ,管理这个字符串、是char*型的容器
3、string不用考虑内存释放和越界(string管理char*所分配的内存,每一次string的复制取值都是由string类维护不用担心)
4、string提供了一些字符串操作函数
查找 find 拷贝 copy 删除 erase 替换 replace 插入 insert
1、string 的构造函数
std::string str1 = "hello world";
string str2("str2");
string str3 = str2;
string str4(10, 'a'); //10个字符a初始化
2、string 的遍历
有3种方式 通过数组下标 迭代器 通过 at(数组下标)函数
string str = "hello world";
str[4] = 'A';
// 1、通过数组下标
for (unsigned int i = 0; i < str.length(); i++)
{
cout << str[i];
}
cout << endl;
// 2、string 也有迭代器
for (string::iterator it = str.begin(); it != str.end(); it++)
{
cout << *it;
}
cout << endl;
// 3、通过 at(数组下标) 函数来访问
for (unsigned int i = 0; i < str.length(); i++)
{
cout << str.at(i);
}
cout << endl;
这里有个问题 [ ] 和 at 有什么区别?
at 越界会抛出异常,而 [ ] 不会
try
{
// [] 数组越界直接终止程序运行
// cout << str[str.length()+4] << endl;
// at 抛出一个 out_of_range 异常
cout << str.at(str.length()+4) << endl;
}
catch (exception &e)
{
printf ("捕获一个异常: %s\n", e.what());
}
catch (...)
{
printf ("捕获一个其他异常\n");
}
}
3、string 和 char*的转换
void func3()
{
string str = "hello world";
// cin >> str;
cout << str << endl;
// 很多场合需要char *类型的变量
// string -----> char *
printf ("str = %s\n", str.c_str());
}
4、字符串的连接
void func4()
{
string str1 = "hello";
string str2 = str1 + " world";
cout << str2 << endl;
str2 = "123 " + str1 + " world";
cout << str2 << endl;
}
5、字符串的查找和替换
void func5()
{
string str = "111111 hello 22222222222 hello 333333 hello 44444444 hello 555555555 hello";
// string 内部有一个find函数, 用来在内部查找,返回的查找到的元素的下标
// 找到的时候 返回的是找到的第一个元素的小标
// 没找到的时候 返回 -1 std::npos
int index = 0;
index = str.find("hello", index);
while (index != string::npos)
{
cout << index++ << endl;
index = str.find("hello", index);
}
//str.replace(index, count, newStr);
// 1、删除下标 index 开始的 count 个字符
// 2、在下标 index 位置插入 新字符串 newStr
str.replace(1,2,"abcdefg");
cout << str << endl;
// 将所有hello 替换成 HELLO
index = 0;
index = str.find("hello", index); //从index处开始查找
while (index != string::npos)
{
str.replace(index, 5, "HELLO");
index = str.find("hello", index);
}
cout << str << endl;
}
void func6()
{
string str = "hello world";
// 通过迭代器删除
str.erase(str.begin());
cout << str << endl;
// 区间删除
str.erase(str.begin(), str.begin()+7);
cout << str << endl;
// 全部删除:删除是 左闭右开的 [str.begin(), str.end())
str.erase(str.begin(), str.end());
cout << str << endl;
str = "hello world";
// 从 下标 2 开始删除 4 个字符
str.erase(2, 4);
cout << str << endl;
str.insert(1, " ");
str.insert(0, "111111111");
str.insert(str.length(), "22222222");
cout << str << endl;
}
7、string 的算法 转换函数
void func7()
{
string str = "111111 hello acadw hello avvbb hello cc hello dd hello";
// 转换函数
transform(str.begin(), str.end(), str.begin(), toupper);
cout << str << endl;
transform(str.begin(), str.end(), str.begin(), tolower);
cout << str << endl;
}