|
本期内容开始分享一些stl标准库的学习笔记,如果要做cpp岗位,stl和boost必须要掌握;好了以下是本次文章内容,跟随我的脚步往一起往下看! 一、stl的诞生: (1)长久以来,软件界一直希望建立一种可重复利用的东西。 (2)C++的面向对象和泛型编程思想,目的就是复用性的提升。 (3)大多情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作 (4)为了建立数据结构和算法的一套标准,于是乎就诞生了stl。 二、stl的基本概念: (1)stl(standard template library),标准模板库。 (2)stl从广义上分为:容器(container)、算法(algorithm)、迭代器(iterator)。 (3)容器和算法之间通过迭代器进行无缝连接;换句直白的话来说,容器和算法之间要实现一些操作,还需迭代器来牵桥搭线。 (4)stl几乎所有的代码都采用了模板类或者模板函数 三、stl六大组件: stl大体上可以分为六大组件,他们分别为:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器 (1)容器:各种数据结构,比如:vector、list、deque、set、map等用来存放数据 (2)算法:各种常用的算法,比如:sort、find、copy、for_each等 (3)迭代器:扮演了容器和算法之间的胶合剂。 (4)仿函数:行为类似函数,可作为算法的莫衷策略。 (5)适配器:一种用来修饰容器或者仿函数、迭代器接口的东西。 (6)空间配置器:负责空间的配置与管理。 四、stl中的容器、算法、迭代器: 1、容器:字面理解就是放东西的空间。 stl容器就是将运用最广泛的一些数据结构实现出来;数据结构就是我们常见的一些数据结构:数组、链表、树、栈、队列、集合,隐射表等; 容器又分为序列式容器和关联式容器两种: (1)序列容器:强调值的排序,序列式容器中的每个元素均有固定的位置。 (2)关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系。 2、算法:问题的解决方法(解法) 有限的步骤,解决逻辑或者数学上的问题,这一门学科我们叫做算法,这个学数据结构里面首先就会给你介绍啥是算法,它的特性啥的,这里就不再造轮子了。 算法分为:质变算法和非质变算法 (1)质变算法:是指运算过程中会更改区间内的元素的内容,例如拷贝、替换、删除等等。 (2)非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等。 3、迭代器 每个容器都有自己专属的迭代器;迭代器使用非常类似于指针,刚开始学我们可以理解迭代器为指针。 迭代器的种类: 种类 功能 支持运算 输入迭代器对数据的只读访问只读,支持++、==、!、= 输出迭代器对数据的只读访问只写,支持++ 前向迭代器读写操作,并能向前推进迭代器读写支持++、== 、!、 = 双向迭代器读写操作,并能向前和向后操作读写,支持++、-- 随机访问迭代器读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器读写,支持++、--、-n、<、<=、>、>= 常用的容器中迭代器种类为双向迭代器、随机访问迭代器。 五、容器算法迭代器初认识: 1、stl中最常用的容器为vector,可以简单理解为数组,下面我们来举个例子如何往这个容器中插入数据,并遍历这个容器: --容器:vector,所用头文件vector --算法:for_each,所用头文件,algorithm --迭代器:vector::iterator 下面是三种遍历方法都有: //vector容器中存放内置数据类型#include <iostream> #include <vector> #include <algorithm> using namespace std; void print(int val) { cout<<val<<endl; } void test() { //创建了一个vector容器,数组 vector<int> v; //向容器中插入数据 v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); //通过迭代器访问容器中的数据 vector<int>::iterator itBegin = v.begin();/起始迭代器,指向容器中的第一个元素 vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的下一个位置,不是指向最后一个元素哦 //第一种遍历方法 while(itBegin != itEnd) { cout<<*itBegin <<endl; itBegin++; } } int main() { test(); } 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out10 20 30 40 50 第二种方式: #include <iostream>#include <vector> #include <algorithm> using namespace std; void print(int val) { cout<<val<<endl; } void test() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); vector<int>::iterator itBegin = v.begin(); vector<int>::iterator itEnd = v.end(); /* while(itBegin != itEnd) { cout<<*itBegin <<endl; itBegin++; }*/ for(vector<int>::iterator it = v.begin();it != v.end();it++) { cout<<*it<<endl; } } int main() { test(); } 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out10 20 30 40 50 第三种方式: #include <iostream>#include <vector> #include <algorithm> using namespace std; void print(int val) { cout<<val<<endl; } void test() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); vector<int>::iterator itBegin = v.begin(); vector<int>::iterator itEnd = v.end(); /* while(itBegin != itEnd) { cout<<*itBegin <<endl; itBegin++; }*/ /* for(vector<int>::iterator it = v.begin();it != v.end();it++) { cout<<*it<<endl; }*/ //利用了stl提供的遍历算法 for_each(v.begin(),v.end(),print); } int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test# ./a.out10 20 30 40 50 2、vector存放自定义数据类型示例: 代码示例: #include <iostream>#include <vector> #include <algorithm> #include <string> using namespace std; class Person { public: Person(string name ,int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; void test() { vector<Person> v; Person p1("aa",10); Person p2("bb",20); Person p3("cc",30); Person p4("ee",40); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); for(vector<Person>::iterator it = v.begin();it != v.end();it++) { cout<<"name: "<<(*it).m_Name<<" age "<<(*it).m_Age<<endl; //cout<<"name: "<<it->m_Name<<" age "<<it->m_Age<<endl; } } int main() { test(); } 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.outname: aa age 10 name: bb age 20 name: cc age 30 name: ee age 40 3、容器嵌套(可以把它看成二维数组) #include <iostream>#include <vector> #include <algorithm> #include <string> using namespace std; void test() { //容易嵌套容器 vector< vector<int> >v; //创建小容器 vector<int> v1; vector<int> v2; vector<int> v3; vector<int> v4; //向小容器里面添加数据 for( int i =0; i<4; i++) { v1.push_back(i+1); v2.push_back(i+2); v3.push_back(i+3); v4.push_back(i+4); } //将小容器插入到大容器中去 v.push_back(v1); v.push_back(v2); v.push_back(v3); v.push_back(v4); //通过打容器,把所有数据遍历一遍 for(vector< vector<int> >::iterator it = v.begin();it !=v.end();it++) { for(vector<int>::iterator vit =(*it).begin(); vit !=(*it).end();vit++) { cout<<*vit<<" "; } cout<<endl; } } int main() { test(); } 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 |
微信公众号
手机版