|
本期内容开始分享一些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、迭代器 每个容器都有自己专属的迭代器;迭代器使用非常类似于指针,刚开始学我们可以理解迭代器为指针。 迭代器的种类: 种类 功能 支持运算 输入迭代器对数据的只读访问只读,支持++、==、!、=8 X8 h- M v; I: @ 输出迭代器对数据的只读访问只写,支持++1 s4 t9 r4 E* T! N' i( | 前向迭代器读写操作,并能向前推进迭代器读写支持++、== 、!、 = 双向迭代器读写操作,并能向前和向后操作读写,支持++、--0 E" H4 g I) D- l! v( X% a: i 随机访问迭代器读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器读写,支持++、--、-n、<、<=、>、>= 常用的容器中迭代器种类为双向迭代器、随机访问迭代器。 五、容器算法迭代器初认识: 1、stl中最常用的容器为vector,可以简单理解为数组,下面我们来举个例子如何往这个容器中插入数据,并遍历这个容器: --容器:vector,所用头文件vector --算法:for_each,所用头文件,algorithm --迭代器:vector::iterator 下面是三种遍历方法都有: //vector容器中存放内置数据类型/ G, H2 X% P+ l9 s) p& k: C#include <iostream> #include <vector> #include <algorithm>; m1 B5 ^; P$ ~/ [( Y using namespace std; # v; ?4 t! V; C$ m) l( E void print(int val); M& o! x+ l6 v/ Y! G, F* ] { cout<<val<<endl; }4 Q- Y5 u f) i* H. { void test()# E) [$ n$ m& c; y {5 I- ~% M: u2 a% A( e //创建了一个vector容器,数组4 c4 w P& a) W8 z7 N vector<int> v;% B& E9 S3 W9 s( T7 ~5 G% r //向容器中插入数据 v.push_back(10);5 @3 P y% W; e+ s+ n( ` v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); //通过迭代器访问容器中的数据 vector<int>::iterator itBegin = v.begin();/起始迭代器,指向容器中的第一个元素" f, W, D, E$ u e vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的下一个位置,不是指向最后一个元素哦 //第一种遍历方法 e0 ]2 O/ d1 ]! y1 W' d while(itBegin != itEnd)5 x5 o5 V% U- e- R {' @, h+ y2 @6 M! t cout<<*itBegin <<endl;+ L [; n. q. [# @* ?4 d itBegin++; }2 ?/ r% C- f7 q) n+ } } int main(), U! K" d9 s2 y& F! s8 v) A6 v {: Z1 e# G( n( P( y; T test();3 [$ U9 A' n) R! s) p) c/ p1 C' l 4 g, ?3 O. j' E* \( R# y2 ` }3 J& G! [% E- l! `" D * U( Q% Z$ S8 _ 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out10 Y2 i; S& ~) e& ^2 X 204 z7 d. K1 j3 j; ?7 b! ]+ U: X4 C 30 40 50, E9 W4 I1 j/ G+ @/ M5 `6 f3 f9 J |( H' {% \& M1 q4 H* E% t 第二种方式: #include <iostream>$ x' C p2 Y: y/ S1 u# Y" A! K5 i$ }#include <vector> #include <algorithm> using namespace std;: z! \( g0 H- |! F) T void print(int val) { cout<<val<<endl; } void test() { 4 o9 a2 ^: B5 B vector<int> v;/ C% U4 y" Z0 d! c1 l & d* j: ~+ T+ M! Y9 m- P v.push_back(10); v.push_back(20); v.push_back(30);/ P( w; I: `' ^2 V2 ]4 B- O v.push_back(40);# c8 C3 L/ _- v/ V v.push_back(50); 8 m( t/ G9 H. n' S vector<int>::iterator itBegin = v.begin(); vector<int>::iterator itEnd = v.end();4 ~- a1 \- H" a( [5 U - g- u y% T* \6 @' e6 V4 ` /* while(itBegin != itEnd) { cout<<*itBegin <<endl;+ a1 R4 I3 J( \ itBegin++;- E9 M" G& e6 [. k- j }*/! W2 ^7 U$ h4 y6 J( i for(vector<int>::iterator it = v.begin();it != v.end();it++) {+ m. V* z2 f8 g: C/ L cout<<*it<<endl;. n6 P, _* N, s; o m1 w' @7 }; i } l* s1 [6 b) Y5 K/ f ; K+ r" {4 S2 j& [! Y+ L6 d1 ~5 N }2 o, U9 c9 C- S/ t' z. a int main()6 V2 ]/ |0 _; }+ K. a4 J4 c { test(); }! _# }( G1 m, Z g$ G 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out10( c) y/ H; n5 E/ u3 P% z3 C! @ 20 300 `& [) {6 z; P3 B* ~8 K 405 W j: N6 R- y% \! t7 W& { 50 ' X3 g: `, N0 t, E; O 第三种方式: #include <iostream>#include <vector> #include <algorithm> ; _+ }4 A' ^* I/ _, A7 ^" M( v using namespace std;, n+ ?/ Q- |" \* O, c, Y void print(int val)+ ]/ Q' Q/ g7 ~6 ]; ?: ^7 F {+ b( Z3 g( [0 J7 x+ P x3 D cout<<val<<endl;' \$ f1 K1 {& Z }" K8 ]$ B% G' Q$ i- h6 x ]5 w void test() { " B* a& v8 v& I vector<int> v;* |1 Y3 n. d% u& j# A v.push_back(10);+ ]* R% e' T8 d& B2 Q9 h v.push_back(20);) A) S; }! [5 |5 W7 B i5 k+ H v.push_back(30);5 N' [& E2 n# t" T5 \ T v.push_back(40);3 s( {! g. Q$ l5 H# q( q v.push_back(50);% |. D# h4 ?7 I ! c. }9 S9 R. d7 L vector<int>::iterator itBegin = v.begin(); vector<int>::iterator itEnd = v.end(); * J9 d- `7 c, N6 r /* while(itBegin != itEnd) {( B- h" ]4 J) ^6 L cout<<*itBegin <<endl; itBegin++;' u7 `# v( l/ Y ]$ ` }*/ /* for(vector<int>::iterator it = v.begin();it != v.end();it++)8 v' `( R& G- E H7 [" N! }) O- V { cout<<*it<<endl; }*/3 } G$ ]9 E' J# h3 S7 @. g9 Z( J //利用了stl提供的遍历算法1 ~+ v, ^ l" L. g: P for_each(v.begin(),v.end(),print); 3 h$ ?2 M: j" g1 @( w' W } int main(), H( E2 l9 j& q( q2 N {) U7 _7 u/ f. H# L/ ~ test();9 `/ d) S {7 [7 l } 结果输出: root@txp-virtual-machine:/home/txp/test# ./a.out10* M( |; p3 t5 c. i& R8 h 20: u9 f( p1 U) n& V9 O% t2 C) O 30 40 50 ) x. m& v# K. \ 2、vector存放自定义数据类型示例: 代码示例: #include <iostream>' c3 Q; V9 c# x1 E% _. r#include <vector> #include <algorithm>; R( I. d3 M& d0 ]; x% ] #include <string> using namespace std; class Person { public: Person(string name ,int age) {7 o# Q8 r5 p( I this->m_Name = name;- C1 [9 l$ A, ?7 G$ i; u this->m_Age = age;! @: |$ A- s v; `' J/ o } f& {5 ~# g9 k* u) ^ string m_Name; int m_Age; };" f6 l( q# S$ l* E4 H void test() { vector<Person> v; Person p1("aa",10);8 D0 x. v. o1 ^! R6 v9 I0 } Person p2("bb",20);# I$ j) F0 T! c% ^5 ~9 \7 v; C Person p3("cc",30);# L+ o0 T2 Q/ M, i& m Person p4("ee",40); v.push_back(p1); v.push_back(p2); v.push_back(p3);' z, d0 y7 l8 M M% `( g7 o v.push_back(p4); % p# B8 X2 Y E, U" ^+ Q8 r4 Q 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; }7 O* `9 H; @* `. o }, h# \1 B1 b# ]4 T* L; | int main()+ N/ n o' ~, n/ M: e5 W1 U {9 L$ T; p# r4 ?- {' e [: } test(); }7 H* Y( I( V$ i) y0 P, ~# E 6 k8 Z4 R8 M7 i' ~9 F 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out3 |/ C0 l6 @% v! hname: aa age 10 name: bb age 20 name: cc age 30 name: ee age 40" R: ~8 J9 J* G8 J1 f T- a 3、容器嵌套(可以把它看成二维数组) #include <iostream>* ~+ A- P, _/ h9 E3 o+ p0 q#include <vector>1 |. W; w) h, T* k( ` #include <algorithm>% u2 y7 G ~2 ]1 M- W6 J #include <string> using namespace std;, H5 Q9 v3 `2 v: M# a& f7 b1 U 8 |2 w, \8 h3 E* q void test()3 F4 ~" U }- \$ d* I {# a8 ]3 s. c0 b: v( e$ } //容易嵌套容器% H- J3 l0 @8 k- ?& Z$ _ vector< vector<int> >v;, \' x, S' g% k5 u: ~2 D //创建小容器 vector<int> v1; vector<int> v2; vector<int> v3;' |, k7 n+ x% Y. m6 U, e vector<int> v4;9 H1 B% T8 F* l //向小容器里面添加数据) h9 X6 ^, z% s9 K( s ^0 k" G for( int i =0; i<4; i++) {# t- l2 u$ Q7 d; t# B. b- F- @ v1.push_back(i+1); v2.push_back(i+2);1 A0 L g' H% ]8 j v3.push_back(i+3);/ D% n( B0 |6 I2 W1 c v4.push_back(i+4); }' F3 A5 B+ U0 O3 i //将小容器插入到大容器中去 v.push_back(v1); v.push_back(v2); v.push_back(v3);' j, n$ o4 V6 h, Z( Q/ Y v.push_back(v4); //通过打容器,把所有数据遍历一遍 for(vector< vector<int> >::iterator it = v.begin();it !=v.end();it++) {, e, t/ m% f; @8 C' D8 H& j$ L- ^/ W for(vector<int>::iterator vit =(*it).begin(); vit !=(*it).end();vit++) {" n5 J+ @4 b9 {0 r+ c3 S cout<<*vit<<" "; } cout<<endl; } } int main()6 X, h9 @, e' b8 ? {; e7 n$ I; |1 R2 [ test(); }# h# V! \# O3 D0 Y4 L6 p& ? 输出结果: root@txp-virtual-machine:/home/txp/test# ./a.out5 S5 [8 N( H: n8 O2 G7 }: _1 2 3 4 2 3 4 5 6 A5 B1 {4 D# B( i) H% P }$ h 3 4 5 6 2 w+ }" [1 e- q* M6 V 4 5 6 7 |
微信公众号
手机版