STL简介
STL(The Stadard Template Librara)是C++学习中必须掌握的库。它提供了高效率的算来解决对一堆数据的管理,它让程序员能够直接获得数据结构和算法领域改进带来的好处,而不需要程序员去直接了解这些算法到底是怎么实现的。从程序员的角度来说,它提供了一系列应对不同需求的数据集合。使用STL你就不需要再写动态数组、链表、二叉树、哈希表和各种搜索算法。 STL是基于多种结构良好的部件的合作,其中最关键的是容器(Containers)、迭代器(Iterator)、算法(Algorithms)。
容器
容器是用来管理某种数据类型的集合。每种数据类型都有其优点和缺点,所以对不同的需求采用不同的容器。容器主要分为三类:顺序容器、关联容器、非顺序容器。顺序容器的元素顺序取决于插入的时间和位置关联容器则取决于Key值,非顺序容器中元素的位置并不重要关键的是元素是否存在于容器中(元素插入的时间和值对位置没有影响而且在容器中元素的位置可能发生改变)。顺序容器主要有:String、Array、Vector、Deque、List、Forward List(C++11提供)。关联容器主要有:Set、Multiset、Map、Multimap。非顺序容器:Unordered Set、Unordered Multiset、Unordered Map、Unordered Multimap。
迭代器
迭代器能让我们访问容器里面的元素,它主要的优势是对所以的抽象容器提供了一个小而通用的接口。它独立于容器内元素的类型,不管是数组、树还是哈虚表。你可以把它当作一个顺序指针,可以通过“++”读取下一个元素指针,”*“来获取该迭代器所指元素。
算法
算法是用来处理容器里的元素。例如,对容器里的元素进行排序、搜索、修改。算法也是使用迭代器这个接口。为了让算法更加灵活,你可以在调用算法的时候,采用一些其他辅助的函数。
STL的思想是建立在数据和操作之间的分离上的,数据是由容器来管理,操作则是由算法定义的,而迭代器就是两者之间的桥梁(让算法和容器实现交互)。
总的来说,STL的思想和传统的面向对象的思想是相反的,它将数据和操作分离开来而不是将两者结合在一起,之所以这么做是因为这样你可以组合不同的容器和算法来实现自己的目的,这样以小的框架提供了非常好的灵活性。当然STL还包括泛型组件,例如:配接器、仿函数。
STL迭代器(iterator)简要
迭代器(iterator)类似于指针,能够通过它访问到它所指的元素。每个容器都有一些基本的函数让你能够获得迭代器并访问容器里的元素。begin() 返回容器中第一个元素的迭代器;end() 返回容器中最后一个元素所在位置的下一个位置而不是最后一个元素的位置。另外关于迭代器 ++pos、pos++两者的效率问题中第一个操作的效率更高因为后者需要创建一个临时对象来返回结果。说明代码:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">Operator Operator::<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>++() { ++<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//内部成员变量</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>; } Operator Operator::<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>++(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> ) { Operator temp; temp.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span>=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span>++; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> temp; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
顺序容器操作实例
iterator操作基本实例(g++ 4.8调试通过):
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(){ <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">list</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span>></span> temp; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> c=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span>; c<=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'z'</span>;++c){ temp.push_back(c); } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//iterator task</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">list</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span>></span>::iterator pos=temp.begin();pos!=temp.end();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//const_iterator task</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">list</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span>></span>::const_iterator pos=temp.begin();pos!=temp.end();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//auto iterator since c++11</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> pos=temp.begin();pos!=temp.end();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//auto const_iterator since c++11</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> pos=temp.cbegin();pos!=temp.cend();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li></ul>
如上所示iterator的基本类型有两种iterator与const_iterator前者对于容器的元素可读可写,而后者只能读而不能修改元素的值。而auto操作符是c++11标准加入的,它的优势可以很明显的看出来,首先声明简单明了,而且如果更改容器的类型,声明语句不需要做修改,而在原先的标准下的声明则要修改。
关联容器操作实例:
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<iostream></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">set</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> IntSet; IntSet temp; temp.insert(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); temp.insert(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); temp.insert(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>); temp.insert(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>); temp.insert(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>); temp.insert(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//print all elem</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> pos=temp.cbegin();pos!=temp.cend();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//task2</span> temp.clear(); temp.insert({<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>});<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//since c++11</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> pos=temp.cbegin();pos!=temp.cend();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>
输出的结果都是:
1 2 3 4 5 6
上面例子表明关联容器的迭代器与插入的时间无关只与插入的值有关而且set容器不被允许有相同值(相同值只显示一个,要是需要相同值都可以使用 multiset )。该例中插入的数字可以用二叉排序树的形式来理解,顺序输出的形式是二叉树的中序输出。
STL算法基础简要
- 简单的算法实例
- 算法中的范围(Ranges)
简单的算法实例
STL提供多种标准算法来处理容器中的数据,这些算法包括搜索、排序、拷贝、修改,重排等基本常用的操作。STL算法不是某个容器类的成员函数而是,通过迭代器操作的全局函数。这样的一个好处就是算法只需要实现一次,而不用为每个特定类型的容器都重新实现算法。它还支持用户自定义的容器类型。
实例代码
常用版本 :
<code class="language-C++ hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <alogrithm></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <vector></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(){ <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> coll; coll.push_back(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); coll.push_back(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>); coll.push_back(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>); coll.push_back(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); coll.push_back(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>); coll.push_back(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>); <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span>::const_iterator minpos=min_element(coll.begin(),coll.end()); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"min: "</span><<*minpos<<endl; <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span>::const_iterator maxpos=min_element(coll.begin(),coll.end()); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"max: "</span><<*maxpos<<endl; sort(coll.begin(),coll.end()); <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span>::iterator pos3; pos3=find(coll.begin(),coll.end(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>); reverse(pos3,coll.end()); <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span>::const_iterator pos; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(pos=coll.begin();pos!=coll.end();++pos){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<*pos<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul>
C++11 版本:
<code class="language-C++11 hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <alogrithm></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <vector></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(){ <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> coll={<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>}; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> minpos=min_element(coll.cbegin(),coll.cend()); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"min: "</span><<*minpos<<endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> maxpos=min_element(coll.cbegin(),coll.cend()); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"max: "</span><<*maxpos<<endl; sort(coll.begin(),coll.end()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> pos3=find(coll.begin(),coll.end(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>); reverse(pos3,coll.end()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">auto</span> elem:coll){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<elem<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>
程序的输出均为:
min: 1
max: 6
1 2 6 5 4 3
实例中min_element() 和 max_element() 算法通过两个参数来确定算法的操作范围,算法返回的是一个iterator对象,分别指向最小值和最大值。find() 算法查找所给区域内的某个值,当找到是返回第一个该值的iterator 若没有则返回的是该区域内最后一个对象的下一个对象的 iterator 该例中是 coll.end()。sort() 算法对所给区域内的对象进行排序。 reverse()对所给区域内对象进行倒置。
算法中的范围(Ranges)
每个算法处理的都是一个或者多个范围的数据元素。你通过两个语句将范围的开始和结尾传递给算法,而不是通过一个语句。这个接口很灵活但是同时也带来了一些问题,例如某个范围: list_1.begin() ,list_2.end() 那么当迭代器前进的时候,因为永远都无法到达范围的结尾程序将出现不可预知的问题。从这个角度来说迭代器并没有传统的指针那么安全可靠。所以在使用算法的时候,一定要确保迭代器属于同一个容器并且确保范围正确,不要将开始和结尾倒置了。当然这种错误导致的未定义行为,STL可以自行捕捉和并进行相应处理。
每个算法中的范围都是个半开区间,用数学中的定义就是左闭右开区间。所以该范围包含开始,但是不包含用于标识末尾的位置。这样的一个好处就是有效的防止了我们多余空容器的处理。
实例代码:
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<iostream></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<list></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<algorithm></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> namespaced <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(){ <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">list</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> coll; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>;i<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>;++i){ coll.push_back(i); } pos25=find(coll.begin(),coll.end(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>); pos35=find(coll.begin(),coll.end(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"max: "</span><<*max_element(pos25,pos35)<<endl; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"max: "</span><<*max_element(pos25,++pos35)<<endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li></ul>
输出结果:
max: 34
max: 35
需要注意的有:
- 针对本例中的不支持随机访问的容器迭代器前进采用 ++ 而像vector 这种支持随机访问的可以采用 +1
- 本例中我们事先就已经知道了pos25与pos35的前后关系当我们不知道两者的前后关系时该如何应对
支持随机访问容器的应对方法:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pos25<pos35){ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//[pos25,pos35)为有效范围</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//[pos35,pos25)为有效范围</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
不支持随机访问容器的常规应对方法:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">pos25=find(cloo.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">begin</span>(),coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>); pos35=find(coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">begin</span>(),pos25,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pos25!=coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>() && pos35!=pos25){ <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//pos</span>35在前面有效区间[pos35,pos25) } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ pos35=find(pos25,coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pos!=coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>(){ <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//pos</span>25在前面有效区间[pos25,pos35) } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//pos</span>25与pos35至少有一个不存在 } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>
不支持随机访问容器的C++11中更加有效的方法:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">pos=find_if(coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">begin</span>(),coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>(), [] (int i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> i==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span> || i==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>; }); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pos==coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>()){ <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>不存在值为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>和<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>的迭代器 .... } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(*pos==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>){ <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>在前面 pos25=pos; pos35=find(++pos,coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>); .... } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>在前面 pos35=pos; pos25=find(++pos,coll.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>(),<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>); .... }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
在这段代码中使用了lambda表达式,这是c++11标准中才开始采用的。该表达式用来确定查找时返回第一个出现的值(25 or 35),如果存在的话。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[] (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> i==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span> || i==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
多个范围的处理
在某些算法中可能涉及到多个范围例如equal、copy函数,在使用这些算法的时候需要注意第二个范围的大小最起码等于第一范围的大小。当第二个范围小的时候我们需要重新设定第二个范围的大小。
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<iostream></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<list></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<vector></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<deque></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(){ <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">list</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> coll={<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>}; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//c++11</span> <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> coll2; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//重置coll2大小调用equal算法</span> coll2.resize(coll.size()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(equal(coll1.begin(),coll1.end(),coll2.begin()){ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"coll1 is equal coll2"</span><<endl; } <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">deque</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> coll3(coll1.size());<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//让coll3与coll1大小一样</span> copy(coll1.begin(),coll1.end(),coll3.begin()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
需要注意的是这里不管是重置大小还是初始化时就给定大小,其实都为容易创建了新的元素(元素的值由默认构造器初始化)。还有就是这里copy算法之所以需要第二个范围大是因为该算法采用重写的方式而不是插入。