欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

STL入门

最编程 2024-01-12 14:46:53
...

目的:复用性提升,为了建立数据结构和算法的一套标准。

STL简介:

STL:标准模板库

STL广义:容器,算法,迭代器

容器和算法之间通过迭代器进行无缝衔接

STL几乎所有代码都采用了模板类或者模板函数。。

STL六大件:容器、算法、迭代器、仿函数、适配器(配接器)、空间适配器

1.容器:各种数据结构,如:vector、list、deque、set、map等,用来存放数据

主要分关联型容器(二叉树,元素之间没有严格意义上的物理顺序)和序列型容器(数组,链表等每个元素有固定的位置)

2.算法:如:sort、fine、copy、for_eachdeng等

主要分为质变算法(运算时改变区间内的元素内容,拷贝、删除等)和非质变算法(运算时不改变区间内的元素内容,查找、遍历,查极值等)

3.迭代器:容器与算法之间的胶合剂,每个容器都有自己专属的迭代器(很类似指针),算法要用迭代器才能访问容器

迭代器主要有五种:

1.输入迭代器:只读访问数据,支持++、==、!=运算

2.输出迭代器:只写访问数据,支持++运算

3.前向迭代器:读写均可,并且能向前推进迭代器,支持++、==、!=运算

4.双向迭代器(常用):读写均可,并且能向前和向后操作,支持++、--运算

5.随机访问迭代器(常用):读写均可,可以访问任意数据,支持++、--、[n]、-n、<、>、<=、>=运算

4.仿函数:行为类似函数,可作为算法的某种策略

5.适配器:用来修饰容器或者仿函数或者迭代器接口

6.空间配置器:空间的配置与管理


vector存放内置数据类型(int、double等)

容器:vector(可暂时理解为数组)

算法:for_each

迭代器:vector<int>::iterator

例子:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5 
 6 void myPrint(int ret){
 7         cout << ret << endl;
 8 }
 9 
10 void test1(){
11     //vector:容器,arr:容器名,<int>:指明容器类存放的数据类型
12     vector<int> arr;
13 
14     //向容器中插入数据(尾插)
15     arr.push_back(11);
16     arr.push_back(12);
17     arr.push_back(13);
18     arr.push_back(14);
19 
20     //通过迭代器访问容器中的数据
21     //可以暂时理解类似vector<int>::iterator itBegin是一个指针
22     vector<int>::iterator itBegin = arr.begin();    //起始迭代器,指向容器中第一个元素
23     vector<int>::iterator itEnd = arr.end();    //结束迭代器,指向容器中最后一个元素的下一个位置
24 
25     //遍历
26     //老法子
27     //
28     while(itBegin != itEnd){
29         cout << *itBegin << endl;
30         itBegin++;
31     }
32     cout << endl;
33     //34     //此法可以注销上面vector<int>::iterator的两行
35     for(vector<int>::iterator it = arr.begin() ; it!=arr.end() ; it++){
36         cout << *it << endl;
37     }
38     cout << endl;
39     //新法子
40     //采用遍历算法
41     //需要定义一个外部全局函数myPrint
42     for_each(arr.begin(),arr.end(),myPrint);
43     //本质是把arr.begin()到arr.end()之间的数逐个读写出来,通过return返回,采用myPrint函数来接收返回的值
44 }
45 
46 int main(){
47     test1();
48     system("pause");
49     return 0;
50 }

Vector存放自定义数据类型(类、指针等这些自定义数据类型)

区别于上面的vector存放内置数据类型(int、double等)

例子:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 
 6 class Person{
 7 public:
 8     Person(string name, int age){
 9         this->mName = name;
10         this->mage = age;
11     }
12 
13     string mName;
14     int mage;
15 };
16 
17 //存放自定义数据类型:类
18 void test1(){
19     vector<Person> v;   //定义一个存放数据类型为Person的vector容器,名字叫v
20     Person p1("aa",122);
21     Person p2("bb",133);
22     Person p3("cc",144);
23     Person p4("dd",155);
24     Person p5("ee",166);
25 
26     //添加数据
27     v.push_back(p1);
28     v.push_back(p2);
29     v.push_back(p3);
30     v.push_back(p4);
31     v.push_back(p5);
32 
33     //遍历
34     for(vector<Person>::iterator s = v.begin(); s!= v.end() ;s++){
35         //两种方法都可以,自选一个
36         cout << "name:" << s->mName << " age:" << s->mage << endl;
37         //cout << "name:" << (*s).mName << " age:" <<(*s).age << endl;
38         //(*s)本质上指的是Person类的数据类型,(*s)是什么类型,看<>内即可
39     }
40     
41 }
42 
43 //存放自定义数据类型:指针
44 void test2(){
45     vector<Person*> v;   //定义一个存放数据类型为Person*(就是指针)的vector容器,名字叫v
46     Person p1("aa",12);
47     Person p2("bb",13);
48     Person p3("cc",14);
49     Person p4("dd",15);
50     Person p5("ee",16);
51 
52     //添加数据
53     v.push_back(&p1);
54     v.push_back(&p2);
55     v.push_back(&p3);
56     v.push_back(&p4);
57     v.push_back(&p5);
58 
59     for(vector<Person*>::iterator s = v.begin(); s!= v.end() ;s++){
60         cout << "name:" << (*s)->mName << " age:" << (*s)->mage << endl;
61         //s指向地址,*s指向地址指向的内容,由于是类,采用->调用
62     }
63 }
64 
65 int main(){
66     test1();
67     test2();
68     system("pause");
69     return 0;
70 }

vector容器嵌套容器(类似多维数组)

例子:

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 void test1(){
 6     vector<vector<int>> v;  //创建一个名为v的容器,用于存放->放置int类型的vector容器(暂称为小容器,外部的叫大容器)
 7 
 8     //创建小容器
 9     vector<int> v1;
10     vector<int> v2;
11     vector<int> v3;
12     vector<int> v4;
13     vector<int> v5;
14 
15     //往小容器内填充数据
16     for(int i = 0; i < 4; i++){
17         v1.push_back(i+10);
18         v2.push_back(i+20);
19         v3.push_back(i+30);
20         v4.push_back(i+40);
21         v5.push_back(i+50);
22     }
23     
24     //把小容器放到大容器中
25     v.push_back(v1);
26     v.push_back(v2);
27     v.push_back(v3);
28     v.push_back(v4);
29     v.push_back(v5);
30 
31     //在大容器把所有数据都遍历一次
32     for(vector<vector<int>>::iterator p = v.begin(); p != v.end(); p++){
33         //此时的(*p)指的是<vector<int>>这个容器
34         //小容器遍历输出
35         for(vector<int>::iterator vp = (*p).begin(); vp != (*p).end(); vp++){
36             cout << *vp << " ";
37         }
38         cout << endl;
39     }
40 }
41 
42 int main(){
43     test1();
44     system("pause");
45     return 0;
46 }

原文地址:https://www.cnblogs.com/MorningMaple/p/16987963.html