文章目录
一 . STL简介
C++含义:
C语言+类+模板
(STL就是活生生的实
STL 全称Standard Template Library 意思为:标准模板库。
c++优于c语言的重要一点就是STL库,使编程更加有效率。
STL有六部分组成:容器,迭代器,算法,适配器,分配器,仿函数。比较常用的有三个:容器,迭代器,算法。而分配器,仿函数并不会将常用到。
二 . STL库的优点
- 节省开发时间,增加开发效率。数据结构有现成的,算法有现成的,直接用就行了,程序员把主要工作放到业务逻辑中就行了
- 高移植性。STL是C++标准模板库,所有的C++编译器都支持这个,所以在有C++的地方就有STL
- 高性能。每个容器的操作,每个算法的实现,都是经过几代大师的修改、优化。所以,基本属于最好的结构了,我们自己写的东西,基本不会比这个更好用
三 . 容器(container)
系统帮我们封装好的数据结构:
数组 链表(单向链表,双向链表) 栈 队列 树 hash表
。
每种结构都能装任意类型
:char和int 等都可以定义封装好的数据结构的类型。
主要是数据结构的操作: 增、删、改、查。
四 . 算法(algorithm)
系统帮我们写好了算法
排序/交换/替换等等
一个算法可以适用多个容器
比如排序sort,默认的是将元素从小到大排列,可以给数组排序,也可以给链表排序
使用算法的时候需要调用头文件:#include
比如:数组的排列
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int i;
int a[5] = { 5,6,9,8,1 };
sort(a, a + 5);
for (i = 0; i < 5; i++) {
cout << a[i];
}
return 0;
}
输出的结果就是:15689
也可以使用sort进行从大到小的排列不过这个就需要用到仿函数的知识需要运用到头文件:#include< functional >
ps:本人觉得 这个从大到小排序没有太大实用性又复杂了好多步,不如直接从小到大排列之后倒序使用或输出。
五 . 迭代器
用于连接算法和容器:
- 关键字interator代表声明一个迭代器,前面需要指明类型
- 迭代器指向容器的某一位置
- 通过*解引用获取元素的引用(注:*得到的是元素的引用)。也可用->得到该元素的成员
例:
#include<iostream>
#include<string>
using namespace std;
int main() {
string str = "HelloWorld";
string::iterator ite; //一个string类型的迭代器
for (ite = str.begin(); ite != str.end(); ite++)
{
cout << *ite;
}
return 0;
}
输出结果:
HelloWorld
迭代器的运算:
例如有迭代器iter,iter1,iter2
*iter 返回迭代器所指元素的引用
iter->data 代表当前所指位置处元素的的指针,可以访问其函数或成员,等价于(*iter).data
++iter 令迭代器指向容器的下一个元素
–iter 令迭代器指向容器的前一个元素
iter1==iter2 判断两个迭代器的位置是否相同
iter1!=iter2 同上
iter + n 改变迭代器的位置
·iter - n 同上
iter +=n 同上
iter -=n 同上
iter1 - iter2 两个迭代器相减,返回它们之间的距离
六 . < string >
头文件:#include< string >
string是专门的字符串操作的一个类
,将< string>中的内容都封装到一起,使字符串的操作更加方便快捷。
与字符串操作中的头文件#include< string.h>中的函数(strlen,stract,strcmp,…)相比实用性更高。
string中封装好的函数可以对string定义的字符变量直接使调用所需要的函数。
string构造的函数可以直接给字符串初始化可以使用cout直接输出:
如:
#include<iostream>
#include<string>
using namespace std;
int main() {
string str("WoHuiHaoHaoXueDe");
cout << str<< endl;
return 0;
}
输出结果:
WoHuiHaoHaoXueDe
使用string可以直接截取字符串的下标的后几个或中间 一段对另一字符串初始化也可以直接拷贝字符串进行初始化。
如:
#include<iostream>
#include<string>
using namespace std;
int main() {
string str("WoHuiHaoHaoXueDe");
cout << str<< endl;
string str1(str, 3);
cout << str1 << endl;
string str2(str, 2, 6);
cout << str2 << endl;
string str3(str);
cout << str3 << endl;
return 0;
}
输出结果:
WoHuiHaoHaoXueDe
uiHaoHaoXueDe
HuiHao
WoHuiHaoHaoXueDe
七 . capacity()(容量大小)
string定义的字符串数组中
不用考虑
内存分配与释放,也不用担心
越界和崩溃的问题。系统会自动分配容量
大小,空的字符串数组系统默认容量大小为15,当字符串存储大于15时系统会重新分配容量大小在15的基础上加上16,超出新分配的容量大小之后还是重新分配容量大小依照上一个容量大小增加16.(vs2017为例)
代码验证:
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
cout << str.capacity() << endl;//空字符串str的容量大小
string str1(7,'a');
cout << str1.capacity() << endl;//str1的容量大小
string str2(16,'a');
cout << str2.capacity() << endl;//str2的容量大小
string str3(32, 'a');
cout << str3.capacity() << endl;//str3的容量大小
return 0;
}
运行结果为:
15
15
31
47
八 . 字符串的修改
1 .赋值(assign):
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
str.assign("hello world"); //将hello world赋值给str;
cout << str << endl;
string str1;
str1.assign("hello world", 5); //将hello world前五个赋值给str1;
cout << str1 << endl;
string str2;
str2.assign(str1);
cout << str2 << endl; //将str1赋值给str2;
string str3;
str3.assign(5, 'x'); //将5个x赋值给str3;
cout << str3 << endl;
return 0;
}
运行结果:
hello world
hello
hello
xxxxx
2 . 插入(insert):
#include<iostream>
#include<string>
using namespace std;
int main (){
string str("abcdefghijk");
string str2("hello world");
str.insert(6, str2); //往str的第六位插入str2
cout << str << endl;
str.insert(6, str2, 3, 4); //把str2的【3,4】擦汗如str的第六位
cout << str << endl;
str.insert(10, "that is cool", 8); //在str的第十位 插入中间字符串的前八位
cout << str << endl;
str.insert(10, "to be "); //在str的第十位 插入该字符串
cout << str << endl;
str.insert(15, 1, ':'); //在str的第15位 插入1个该字符
cout << str << endl;
str.insert(str.begin() + 5, ','); //在str的第5位 插入“,” (str.begin()字符串的开头)
cout << str << endl;
str.insert(str.end(), 3, '.'); //在str的结束 插入3个“.” (str.end()字符串的结尾)
cout << str << endl;
return 0;
}
输出结果:
abcdefhello worldghijk
abcdeflo whello worldghijk
abcdeflo wthat is hello worldghijk
abcdeflo wto be that is hello worldghijk
abcdeflo wto be: that is hello worldghijk
abcde,flo wto be: that is hello worldghijk
abcde,flo wto be: that is hello worldghijk…
九 . string中的常用函数
1 . 函数(lenght.size.resize.empoty):
lenght()(字符串长度) size()(字符个数)这两个相差不太大数值基本一样,但是库函数中把他们两个单独分出来肯定有独特的用处。
resize()(重新设置字符串个数,截断字符串)empoty()(判断字符串是否为空,空返回true,非空返回false;)
例:
#include<iostream>
#include<string>
using namespace std;
int main() {
string str("WoHuiHaoHaoXueDe");
cout << str.length()<< endl; //字符串str的长度
cout << str.size() << endl; //字符串str的字符个数
str.resize(3);
cout << str << endl;
string str1;
cout << str.empty() <<" " << str1.empty() << endl; //判断字符串str和str1是否为空
return 0;
}
输出结果:
16
16
WoH
0 1
2 . 函数( compare.find.substr.swap):
比较(compare)查找子串(find)返回子串(substr)交换字符串(swap)
例:
#include<iostream>
#include<string>
using namespace std;
int main (){
string str("abcdefgh");
string str2("abdefg");
cout << str2.compare(str) << endl; //比较字符串str和str2的大小返回结果为真或假;
cout << str2.compare("abc") << endl;//比较字符串str2和"abc"的大小
cout << str2.compare(1, 3, str) << endl;//从str2的下标1开始的三个字符与str相比较大小
cout << str2.compare(1, 3, str, 1,4) << endl;//从str2的下标1开始的三个字符与str中下标1开始的4个字符相比较
cout << str2.find('d', 1) << endl; //从str2的下标1开始查找字符a(或字符串)查找到返回字符下标(查找字符串返回值为1)否则为-1
cout << str2.substr(2, 4) << endl; //输出str2中下标2-4的字符
str2.swap(str);
cout <<str2 << endl << str << endl;//交换字符串str和str2的内容
return 0;
}
运行结果:
1
1
1
1
2
defg
abcdefgh
abdefg
感谢浏览,在编程的路上一起加油✌