-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-search.xml
98 lines (45 loc) · 627 KB
/
local-search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>C++提高编程</title>
<link href="/2022/01/07/C++%E6%8F%90%E9%AB%98%E7%BC%96%E7%A8%8B/"/>
<url>/2022/01/07/C++%E6%8F%90%E9%AB%98%E7%BC%96%E7%A8%8B/</url>
<content type="html"><![CDATA[<h1 id="C-提高编程"><a href="#C-提高编程" class="headerlink" title="C++提高编程"></a>C++提高编程</h1><ul><li>本阶段主要针对C++==泛型编程==和==STL==技术做详细讲解,探讨C++更深层的使用</li></ul><h2 id="1-模板"><a href="#1-模板" class="headerlink" title="1 模板"></a>1 模板</h2><h3 id="1-1-模板的概念"><a href="#1-1-模板的概念" class="headerlink" title="1.1 模板的概念"></a>1.1 模板的概念</h3><p>模板就是建立<strong>通用的模具</strong>,大大<strong>提高复用性</strong></p><p>例如生活中的模板</p><p>一寸照片模板:</p><p><img src="assets/1547105026929.png" alt="1547105026929"></p><p>PPT模板:</p><p><img src="assets/1547103297864.png" alt="1547103297864"></p><p><img src="assets/1547103359158.png" alt="1547103359158"></p><p>模板的特点:</p><ul><li>模板不可以直接使用,它只是一个框架</li><li>模板的通用并不是万能的</li></ul><h3 id="1-2-函数模板"><a href="#1-2-函数模板" class="headerlink" title="1.2 函数模板"></a>1.2 函数模板</h3><ul><li>C++另一种编程思想称为 ==泛型编程== ,主要利用的技术就是模板</li></ul><ul><li>C++提供两种模板机制:<strong>函数模板</strong>和<strong>类模板</strong> </li></ul><h4 id="1-2-1-函数模板语法"><a href="#1-2-1-函数模板语法" class="headerlink" title="1.2.1 函数模板语法"></a>1.2.1 函数模板语法</h4><p>函数模板作用:</p><p>建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个<strong>虚拟的类型</strong>来代表。</p><p><strong>语法:</strong> </p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T><br>函数声明或定义<br></code></pre></td></tr></table></figure><p><strong>解释:</strong></p><p>template — 声明创建模板</p><p>typename — 表面其后面的符号是一种数据类型,可以用class代替</p><p>T — 通用的数据类型,名称可以替换,通常为大写字母</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><code class="hljs C++"><br><span class="hljs-comment">//交换整型函数</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">swapInt</span><span class="hljs-params">(<span class="hljs-keyword">int</span>& a, <span class="hljs-keyword">int</span>& b)</span> </span>{<br><span class="hljs-keyword">int</span> temp = a;<br>a = b;<br>b = temp;<br>}<br><br><span class="hljs-comment">//交换浮点型函数</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">swapDouble</span><span class="hljs-params">(<span class="hljs-keyword">double</span>& a, <span class="hljs-keyword">double</span>& b)</span> </span>{<br><span class="hljs-keyword">double</span> temp = a;<br>a = b;<br>b = temp;<br>}<br><br><span class="hljs-comment">//利用模板提供通用的交换函数</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mySwap</span><span class="hljs-params">(T& a, T& b)</span></span><br><span class="hljs-function"></span>{<br>T temp = a;<br>a = b;<br>b = temp;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>;<br><br><span class="hljs-comment">//swapInt(a, b);</span><br><br><span class="hljs-comment">//利用模板实现交换</span><br><span class="hljs-comment">//1、自动类型推导</span><br><span class="hljs-built_in">mySwap</span>(a, b);<br><br><span class="hljs-comment">//2、显示指定类型</span><br>mySwap<<span class="hljs-keyword">int</span>>(a, b);<br><br>cout << <span class="hljs-string">"a = "</span> << a << endl;<br>cout << <span class="hljs-string">"b = "</span> << b << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>函数模板利用关键字 template</li><li>使用函数模板有两种方式:自动类型推导、显示指定类型</li><li>模板的目的是为了提高复用性,将类型参数化</li></ul><h4 id="1-2-2-函数模板注意事项"><a href="#1-2-2-函数模板注意事项" class="headerlink" title="1.2.2 函数模板注意事项"></a>1.2.2 函数模板注意事项</h4><p>注意事项:</p><ul><li>自动类型推导,必须推导出一致的数据类型T,才可以使用</li></ul><ul><li>模板必须要确定出T的数据类型,才可以使用</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//利用模板提供通用的交换函数</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mySwap</span><span class="hljs-params">(T& a, T& b)</span></span><br><span class="hljs-function"></span>{<br>T temp = a;<br>a = b;<br>b = temp;<br>}<br><br><br><span class="hljs-comment">// 1、自动类型推导,必须推导出一致的数据类型T,才可以使用</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>;<br><span class="hljs-keyword">char</span> c = <span class="hljs-string">'c'</span>;<br><br><span class="hljs-built_in">mySwap</span>(a, b); <span class="hljs-comment">// 正确,可以推导出一致的T</span><br><span class="hljs-comment">//mySwap(a, c); // 错误,推导不出一致的T类型</span><br>}<br><br><br><span class="hljs-comment">// 2、模板必须要确定出T的数据类型,才可以使用</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">func</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"func 调用"</span> << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//func(); //错误,模板不能独立使用,必须确定出T的类型</span><br>func<<span class="hljs-keyword">int</span>>(); <span class="hljs-comment">//利用显示指定类型的方式,给T一个类型,才可以使用该模板</span><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>使用模板时必须确定出通用数据类型T,并且能够推导出一致的类型</li></ul><h4 id="1-2-3-函数模板案例"><a href="#1-2-3-函数模板案例" class="headerlink" title="1.2.3 函数模板案例"></a>1.2.3 函数模板案例</h4><p>案例描述:</p><ul><li>利用函数模板封装一个排序的函数,可以对<strong>不同数据类型数组</strong>进行排序</li><li>排序规则从大到小,排序算法为<strong>选择排序</strong></li><li>分别利用<strong>char数组</strong>和<strong>int数组</strong>进行测试</li></ul><p>示例:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//交换的函数模板</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mySwap</span><span class="hljs-params">(T &a, T&b)</span></span><br><span class="hljs-function"></span>{<br>T temp = a;<br>a = b;<br>b = temp;<br>}<br><br><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T</span>></span> <span class="hljs-comment">// 也可以替换成typename</span><br><span class="hljs-comment">//利用选择排序,进行对数组从大到小的排序</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mySort</span><span class="hljs-params">(T arr[], <span class="hljs-keyword">int</span> len)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < len; i++)<br>{<br><span class="hljs-keyword">int</span> max = i; <span class="hljs-comment">//最大数的下标</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j < len; j++)<br>{<br><span class="hljs-keyword">if</span> (arr[max] < arr[j])<br>{<br>max = j;<br>}<br>}<br><span class="hljs-keyword">if</span> (max != i) <span class="hljs-comment">//如果最大数的下标不是i,交换两者</span><br>{<br><span class="hljs-built_in">mySwap</span>(arr[max], arr[i]);<br>}<br>}<br>}<br><span class="hljs-function"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printArray</span><span class="hljs-params">(T arr[], <span class="hljs-keyword">int</span> len)</span> </span>{<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < len; i++) {<br>cout << arr[i] << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//测试char数组</span><br><span class="hljs-keyword">char</span> charArr[] = <span class="hljs-string">"bdcfeagh"</span>;<br><span class="hljs-keyword">int</span> num = <span class="hljs-built_in"><span class="hljs-keyword">sizeof</span></span>(charArr) / <span class="hljs-built_in"><span class="hljs-keyword">sizeof</span></span>(<span class="hljs-keyword">char</span>);<br><span class="hljs-built_in">mySort</span>(charArr, num);<br><span class="hljs-built_in">printArray</span>(charArr, num);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//测试int数组</span><br><span class="hljs-keyword">int</span> intArr[] = { <span class="hljs-number">7</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">9</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span> };<br><span class="hljs-keyword">int</span> num = <span class="hljs-built_in"><span class="hljs-keyword">sizeof</span></span>(intArr) / <span class="hljs-built_in"><span class="hljs-keyword">sizeof</span></span>(<span class="hljs-keyword">int</span>);<br><span class="hljs-built_in">mySort</span>(intArr, num);<br><span class="hljs-built_in">printArray</span>(intArr, num);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:模板可以提高代码复用,需要熟练掌握</p><h4 id="1-2-4-普通函数与函数模板的区别"><a href="#1-2-4-普通函数与函数模板的区别" class="headerlink" title="1.2.4 普通函数与函数模板的区别"></a>1.2.4 普通函数与函数模板的区别</h4><p><strong>普通函数与函数模板区别:</strong></p><ul><li>普通函数调用时可以发生自动类型转换(隐式类型转换)</li><li>函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换</li><li>如果利用显示指定类型的方式,可以发生隐式类型转换</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//普通函数</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">myAdd01</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> a + b;<br>}<br><br><span class="hljs-comment">//函数模板</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function">T <span class="hljs-title">myAdd02</span><span class="hljs-params">(T a, T b)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> a + b;<br>}<br><br><span class="hljs-comment">//使用函数模板时,如果用自动类型推导,不会发生自动类型转换,即隐式类型转换</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>;<br><span class="hljs-keyword">char</span> c = <span class="hljs-string">'c'</span>;<br><br>cout << <span class="hljs-built_in">myAdd01</span>(a, c) << endl; <span class="hljs-comment">//正确,将char类型的'c'隐式转换为int类型 'c' 对应 ASCII码 99</span><br><br><span class="hljs-comment">//myAdd02(a, c); // 报错,使用自动类型推导时,不会发生隐式类型转换</span><br><br>myAdd02<<span class="hljs-keyword">int</span>>(a, c); <span class="hljs-comment">//正确,如果用显示指定类型,可以发生隐式类型转换</span><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型T</p><h4 id="1-2-5-普通函数与函数模板的调用规则"><a href="#1-2-5-普通函数与函数模板的调用规则" class="headerlink" title="1.2.5 普通函数与函数模板的调用规则"></a>1.2.5 普通函数与函数模板的调用规则</h4><p>调用规则如下:</p><ol><li>如果函数模板和普通函数都可以实现,优先调用普通函数</li><li>可以通过空模板参数列表来强制调用函数模板</li><li>函数模板也可以发生重载</li><li>如果函数模板可以产生更好的匹配,优先调用函数模板</li></ol><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//普通函数与函数模板调用规则</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">myPrint</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"调用的普通函数"</span> << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">myPrint</span><span class="hljs-params">(T a, T b)</span> </span><br><span class="hljs-function"></span>{ <br>cout << <span class="hljs-string">"调用的模板"</span> << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">myPrint</span><span class="hljs-params">(T a, T b, T c)</span> </span><br><span class="hljs-function"></span>{ <br>cout << <span class="hljs-string">"调用重载的模板"</span> << endl; <br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//1、如果函数模板和普通函数都可以实现,优先调用普通函数</span><br><span class="hljs-comment">// 注意 如果告诉编译器 普通函数是有的,但只是声明没有实现,或者不在当前文件内实现,就会报错找不到</span><br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>;<br><span class="hljs-built_in">myPrint</span>(a, b); <span class="hljs-comment">//调用普通函数</span><br><br><span class="hljs-comment">//2、可以通过空模板参数列表来强制调用函数模板</span><br>myPrint<>(a, b); <span class="hljs-comment">//调用函数模板</span><br><br><span class="hljs-comment">//3、函数模板也可以发生重载</span><br><span class="hljs-keyword">int</span> c = <span class="hljs-number">30</span>;<br><span class="hljs-built_in">myPrint</span>(a, b, c); <span class="hljs-comment">//调用重载的函数模板</span><br><br><span class="hljs-comment">//4、 如果函数模板可以产生更好的匹配,优先调用函数模板</span><br><span class="hljs-keyword">char</span> c1 = <span class="hljs-string">'a'</span>;<br><span class="hljs-keyword">char</span> c2 = <span class="hljs-string">'b'</span>;<br><span class="hljs-built_in">myPrint</span>(c1, c2); <span class="hljs-comment">//调用函数模板</span><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性</p><h4 id="1-2-6-模板的局限性"><a href="#1-2-6-模板的局限性" class="headerlink" title="1.2.6 模板的局限性"></a>1.2.6 模板的局限性</h4><p><strong>局限性:</strong></p><ul><li>模板的通用性并不是万能的</li></ul><p><strong>例如:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">f</span><span class="hljs-params">(T a, T b)</span></span><br><span class="hljs-function"></span>{ <br> a = b;<br> }<br></code></pre></td></tr></table></figure><p>在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了</p><p>再例如:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">f</span><span class="hljs-params">(T a, T b)</span></span><br><span class="hljs-function"></span>{ <br> <span class="hljs-keyword">if</span>(a > b) { ... }<br> }<br></code></pre></td></tr></table></figure><p>在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行</p><p>因此C++为了解决这种问题,提供模板的重载,可以为这些<strong>特定的类型</strong>提供<strong>具体化的模板</strong></p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-comment">//普通函数模板</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">myCompare</span><span class="hljs-params">(T& a, T& b)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">if</span> (a == b)<br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>}<br><span class="hljs-keyword">else</span><br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>}<br>}<br><br><br><span class="hljs-comment">//具体化,显示具体化的原型和定意思以template<>开头,并通过名称来指出类型</span><br><span class="hljs-comment">//具体化优先于常规模板</span><br><span class="hljs-keyword">template</span><> <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">myCompare</span><span class="hljs-params">(Person &p1, Person &p2)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">if</span> ( p1.m_Name == p2.m_Name && p1.m_Age == p2.m_Age)<br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>}<br><span class="hljs-keyword">else</span><br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>;<br><span class="hljs-comment">//内置数据类型可以直接使用通用的函数模板</span><br><span class="hljs-keyword">bool</span> ret = <span class="hljs-built_in">myCompare</span>(a, b);<br><span class="hljs-keyword">if</span> (ret)<br>{<br>cout << <span class="hljs-string">"a == b "</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"a != b "</span> << endl;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"Tom"</span>, <span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"Tom"</span>, <span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-comment">//自定义数据类型,不会调用普通的函数模板</span><br><span class="hljs-comment">//可以创建具体化的Person数据类型的模板,用于特殊处理这个类型</span><br><span class="hljs-keyword">bool</span> ret = <span class="hljs-built_in">myCompare</span>(p1, p2);<br><span class="hljs-keyword">if</span> (ret)<br>{<br>cout << <span class="hljs-string">"p1 == p2 "</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"p1 != p2 "</span> << endl;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>利用具体化的模板,可以解决自定义类型的通用化</li><li>学习模板并不是为了写模板,而是在STL能够运用系统提供的模板</li></ul><h3 id="1-3-类模板"><a href="#1-3-类模板" class="headerlink" title="1.3 类模板"></a>1.3 类模板</h3><h4 id="1-3-1-类模板语法"><a href="#1-3-1-类模板语法" class="headerlink" title="1.3.1 类模板语法"></a>1.3.1 类模板语法</h4><p>类模板作用:</p><ul><li>建立一个通用类,类中的成员 数据类型可以不具体制定,用一个<strong>虚拟的类型</strong>来代表。</li></ul><p><strong>语法:</strong> </p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-keyword">template</span><<span class="hljs-keyword">typename</span> T><br>类<br></code></pre></td></tr></table></figure><p><strong>解释:</strong></p><p>template — 声明创建模板</p><p>typename — 表面其后面的符号是一种数据类型,可以用class代替</p><p>T — 通用的数据类型,名称可以替换,通常为大写字母</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-comment">//类模板</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NameType</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">AgeType</span>></span> <br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(NameType name, AgeType age)<br>{<br><span class="hljs-keyword">this</span>->mName = name;<br><span class="hljs-keyword">this</span>->mAge = age;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"name: "</span> << <span class="hljs-keyword">this</span>->mName << <span class="hljs-string">" age: "</span> << <span class="hljs-keyword">this</span>->mAge << endl;<br>}<br><span class="hljs-keyword">public</span>:<br>NameType mName;<br>AgeType mAge;<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">// 指定NameType 为string类型,AgeType 为 int类型</span><br>Person<string, <span class="hljs-keyword">int</span>><span class="hljs-built_in">P1</span>(<span class="hljs-string">"孙悟空"</span>, <span class="hljs-number">999</span>);<br>P1.<span class="hljs-built_in">showPerson</span>();<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板</p><h4 id="1-3-2-类模板与函数模板区别"><a href="#1-3-2-类模板与函数模板区别" class="headerlink" title="1.3.2 类模板与函数模板区别"></a>1.3.2 类模板与函数模板区别</h4><p>类模板与函数模板区别主要有两点:</p><ol><li>类模板没有自动类型推导的使用方式</li><li>类模板在模板参数列表中可以有默认参数</li></ol><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-comment">//类模板</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NameType</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">AgeType</span> =</span> <span class="hljs-keyword">int</span>> <br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(NameType name, AgeType age)<br>{<br><span class="hljs-keyword">this</span>->mName = name;<br><span class="hljs-keyword">this</span>->mAge = age;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"name: "</span> << <span class="hljs-keyword">this</span>->mName << <span class="hljs-string">" age: "</span> << <span class="hljs-keyword">this</span>->mAge << endl;<br>}<br><span class="hljs-keyword">public</span>:<br>NameType mName;<br>AgeType mAge;<br>};<br><br><span class="hljs-comment">//1、类模板没有自动类型推导的使用方式</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">// Person p("孙悟空", 1000); // 错误 类模板使用时候,不可以用自动类型推导</span><br>Person <string ,<span class="hljs-keyword">int</span>><span class="hljs-built_in">p</span>(<span class="hljs-string">"孙悟空"</span>, <span class="hljs-number">1000</span>); <span class="hljs-comment">//必须使用显示指定类型的方式,使用类模板</span><br>p.<span class="hljs-built_in">showPerson</span>();<br>}<br><br><span class="hljs-comment">//2、类模板在模板参数列表中可以有默认参数</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Person <string> <span class="hljs-built_in">p</span>(<span class="hljs-string">"猪八戒"</span>, <span class="hljs-number">999</span>); <span class="hljs-comment">//类模板中的模板参数列表 可以指定默认参数</span><br>p.<span class="hljs-built_in">showPerson</span>();<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>类模板使用只能用显示指定类型方式</li><li>类模板中的模板参数列表可以有默认参数</li></ul><h4 id="1-3-3-类模板中成员函数创建时机"><a href="#1-3-3-类模板中成员函数创建时机" class="headerlink" title="1.3.3 类模板中成员函数创建时机"></a>1.3.3 类模板中成员函数创建时机</h4><p>类模板中成员函数和普通类中成员函数创建时机是有区别的:</p><ul><li>普通类中的成员函数一开始就可以创建</li><li>类模板中的成员函数在调用时才创建</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person1</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson1</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"Person1 show"</span> << endl;<br>}<br>};<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person2</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson2</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"Person2 show"</span> << endl;<br>}<br>};<br><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br>T obj;<br><br><span class="hljs-comment">//类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成</span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fun1</span><span class="hljs-params">()</span> </span>{ obj.<span class="hljs-built_in">showPerson1</span>(); }<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fun2</span><span class="hljs-params">()</span> </span>{ obj.<span class="hljs-built_in">showPerson2</span>(); }<br><br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>MyClass<Person1> m;<br><br>m.<span class="hljs-built_in">fun1</span>();<br><br><span class="hljs-comment">//m.fun2();//编译会出错,说明函数调用才会去创建成员函数</span><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:类模板中的成员函数并不是一开始就创建的,在调用时才去创建</p><h4 id="1-3-4-类模板对象做函数参数"><a href="#1-3-4-类模板对象做函数参数" class="headerlink" title="1.3.4 类模板对象做函数参数"></a>1.3.4 类模板对象做函数参数</h4><p>学习目标:</p><ul><li>类模板实例化出的对象,向函数传参的方式</li></ul><p>一共有三种传入方式:</p><ol><li>指定传入的类型 — 直接显示对象的数据类型</li><li>参数模板化 — 将对象中的参数变为模板进行传递</li><li>整个类模板化 — 将这个对象类型 模板化进行传递</li></ol><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-comment">//类模板</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NameType</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">AgeType</span> =</span> <span class="hljs-keyword">int</span>> <br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(NameType name, AgeType age)<br>{<br><span class="hljs-keyword">this</span>->mName = name;<br><span class="hljs-keyword">this</span>->mAge = age;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"name: "</span> << <span class="hljs-keyword">this</span>->mName << <span class="hljs-string">" age: "</span> << <span class="hljs-keyword">this</span>->mAge << endl;<br>}<br><span class="hljs-keyword">public</span>:<br>NameType mName;<br>AgeType mAge;<br>};<br><br><span class="hljs-comment">//1、指定传入的类型</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printPerson1</span><span class="hljs-params">(Person<string, <span class="hljs-keyword">int</span>> &p)</span> </span><br><span class="hljs-function"></span>{<br>p.<span class="hljs-built_in">showPerson</span>();<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Person <string, <span class="hljs-keyword">int</span> ><span class="hljs-built_in">p</span>(<span class="hljs-string">"孙悟空"</span>, <span class="hljs-number">100</span>);<br><span class="hljs-built_in">printPerson1</span>(p);<br>}<br><br><span class="hljs-comment">//2、参数模板化</span><br><span class="hljs-keyword">template</span> <<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printPerson2</span><span class="hljs-params">(Person<T1, T2>&p)</span></span><br><span class="hljs-function"></span>{<br>p.<span class="hljs-built_in">showPerson</span>();<br>cout << <span class="hljs-string">"T1的类型为: "</span> << <span class="hljs-built_in"><span class="hljs-keyword">typeid</span></span>(T1).<span class="hljs-built_in">name</span>() << endl;<br>cout << <span class="hljs-string">"T2的类型为: "</span> << <span class="hljs-built_in"><span class="hljs-keyword">typeid</span></span>(T2).<span class="hljs-built_in">name</span>() << endl;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Person <string, <span class="hljs-keyword">int</span> ><span class="hljs-built_in">p</span>(<span class="hljs-string">"猪八戒"</span>, <span class="hljs-number">90</span>);<br><span class="hljs-built_in">printPerson2</span>(p);<br>}<br><br><span class="hljs-comment">//3、整个类模板化</span><br><span class="hljs-function"><span class="hljs-keyword">template</span><class T></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printPerson3</span><span class="hljs-params">(T & p)</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"T的类型为: "</span> << <span class="hljs-built_in"><span class="hljs-keyword">typeid</span></span>(T).<span class="hljs-built_in">name</span>() << endl;<br>p.<span class="hljs-built_in">showPerson</span>();<br><br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test03</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Person <string, <span class="hljs-keyword">int</span> ><span class="hljs-built_in">p</span>(<span class="hljs-string">"唐僧"</span>, <span class="hljs-number">30</span>);<br><span class="hljs-built_in">printPerson3</span>(p);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><span class="hljs-built_in">test02</span>();<br><span class="hljs-built_in">test03</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>通过类模板创建的对象,可以有三种方式向函数中进行传参</li><li>使用比较广泛是第一种:指定传入的类型</li></ul><h4 id="1-3-5-类模板与继承"><a href="#1-3-5-类模板与继承" class="headerlink" title="1.3.5 类模板与继承"></a>1.3.5 类模板与继承</h4><p>当类模板碰到继承时,需要注意一下几点:</p><ul><li>当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型</li><li>如果不指定,编译器无法给子类分配内存</li><li>如果想灵活指定出父类中T的类型,子类也需变为类模板</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Base</span></span><br><span class="hljs-class">{</span><br>T m;<br>};<br><br><span class="hljs-comment">//class Son:public Base //错误,c++编译需要给子类分配内存,必须知道父类中T的类型才可以向下继承</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> :</span><span class="hljs-keyword">public</span> Base<<span class="hljs-keyword">int</span>> <span class="hljs-comment">//必须指定一个类型</span><br>{<br>};<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Son c;<br>}<br><br><span class="hljs-comment">//类模板继承类模板 ,可以用T2指定父类中的T类型</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son2</span> :</span><span class="hljs-keyword">public</span> Base<T2><br>{<br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Son2</span>()<br>{<br>cout << <span class="hljs-built_in"><span class="hljs-keyword">typeid</span></span>(T1).<span class="hljs-built_in">name</span>() << endl;<br>cout << <span class="hljs-built_in"><span class="hljs-keyword">typeid</span></span>(T2).<span class="hljs-built_in">name</span>() << endl;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Son2<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">char</span>> child1;<br>}<br><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:如果父类是类模板,子类需要指定出父类中T的数据类型</p><h4 id="1-3-6-类模板成员函数类外实现"><a href="#1-3-6-类模板成员函数类外实现" class="headerlink" title="1.3.6 类模板成员函数类外实现"></a>1.3.6 类模板成员函数类外实现</h4><p>学习目标:能够掌握类模板中的成员函数类外实现</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-comment">//类模板中成员函数类外实现</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-comment">//成员函数类内声明</span><br><span class="hljs-built_in">Person</span>(T1 name, T2 age);<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson</span><span class="hljs-params">()</span></span>;<br><br><span class="hljs-keyword">public</span>:<br>T1 m_Name;<br>T2 m_Age;<br>};<br><br><span class="hljs-comment">//构造函数 类外实现</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br>Person<T1, T2>::<span class="hljs-built_in">Person</span>(T1 name, T2 age) {<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><br><span class="hljs-comment">//成员函数 类外实现</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-keyword">void</span> Person<T1, T2>::<span class="hljs-built_in">showPerson</span>() {<br>cout << <span class="hljs-string">"姓名: "</span> << <span class="hljs-keyword">this</span>->m_Name << <span class="hljs-string">" 年龄:"</span> << <span class="hljs-keyword">this</span>->m_Age << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-function">Person<string, <span class="hljs-keyword">int</span>> <span class="hljs-title">p</span><span class="hljs-params">(<span class="hljs-string">"Tom"</span>, <span class="hljs-number">20</span>)</span></span>;<br>p.<span class="hljs-built_in">showPerson</span>();<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:类模板中成员函数类外实现时,需要加上模板参数列表</p><h4 id="1-3-7-类模板分文件编写"><a href="#1-3-7-类模板分文件编写" class="headerlink" title="1.3.7 类模板分文件编写"></a>1.3.7 类模板分文件编写</h4><p>学习目标:</p><ul><li>掌握类模板成员函数分文件编写产生的问题以及解决方式</li></ul><p>问题:</p><ul><li>类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到</li></ul><p>解决:</p><ul><li>解决方式1:直接包含.cpp源文件</li><li>解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制</li></ul><p><strong>示例:</strong></p><p>person.hpp中代码:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> once</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(T1 name, T2 age);<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showPerson</span><span class="hljs-params">()</span></span>;<br><span class="hljs-keyword">public</span>:<br>T1 m_Name;<br>T2 m_Age;<br>};<br><br><span class="hljs-comment">//构造函数 类外实现</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br>Person<T1, T2>::<span class="hljs-built_in">Person</span>(T1 name, T2 age) {<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><br><span class="hljs-comment">//成员函数 类外实现</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-keyword">void</span> Person<T1, T2>::<span class="hljs-built_in">showPerson</span>() {<br>cout << <span class="hljs-string">"姓名: "</span> << <span class="hljs-keyword">this</span>->m_Name << <span class="hljs-string">" 年龄:"</span> << <span class="hljs-keyword">this</span>->m_Age << endl;<br>}<br></code></pre></td></tr></table></figure><p>类模板分文件编写.cpp中代码</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><span class="hljs-comment">//#include "person.h"</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"person.cpp"</span> <span class="hljs-comment">//解决方式1,包含cpp源文件</span></span><br><br><span class="hljs-comment">//解决方式2,将声明和实现写到一起,文件后缀名改为.hpp</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"person.hpp"</span></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-function">Person<string, <span class="hljs-keyword">int</span>> <span class="hljs-title">p</span><span class="hljs-params">(<span class="hljs-string">"Tom"</span>, <span class="hljs-number">10</span>)</span></span>;<br>p.<span class="hljs-built_in">showPerson</span>();<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp</p><h4 id="1-3-8-类模板与友元"><a href="#1-3-8-类模板与友元" class="headerlink" title="1.3.8 类模板与友元"></a>1.3.8 类模板与友元</h4><p>学习目标:</p><ul><li>掌握类模板配合友元函数的类内和类外实现</li></ul><p>全局函数类内实现 - 直接在类内声明友元即可</p><p>全局函数类外实现 - 需要提前让编译器知道全局函数的存在</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-comment">//2、全局函数配合友元 类外实现 - 先做函数模板声明,下方在做函数模板定义,在做友元</span><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>;</span><br><br><span class="hljs-comment">//如果声明了函数模板,可以将实现写到后面,否则需要将实现体写到类的前面让编译器提前看到</span><br><span class="hljs-comment">//template<class T1, class T2> void printPerson2(Person<T1, T2> & p); </span><br><br><span class="hljs-function"><span class="hljs-keyword">template</span><class T1, class T2></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printPerson2</span><span class="hljs-params">(Person<T1, T2> & p)</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"类外实现 ---- 姓名: "</span> << p.m_Name << <span class="hljs-string">" 年龄:"</span> << p.m_Age << endl;<br>}<br><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T1</span>, <span class="hljs-keyword">class</span> <span class="hljs-title">T2</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-comment">//1、全局函数配合友元 类内实现</span><br><span class="hljs-function"><span class="hljs-keyword">friend</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printPerson</span><span class="hljs-params">(Person<T1, T2> & p)</span></span><br><span class="hljs-function"></span>{<br>cout << <span class="hljs-string">"姓名: "</span> << p.m_Name << <span class="hljs-string">" 年龄:"</span> << p.m_Age << endl;<br>}<br><br><br><span class="hljs-comment">//全局函数配合友元 类外实现</span><br><span class="hljs-keyword">friend</span> <span class="hljs-keyword">void</span> printPerson2<>(Person<T1, T2> & p);<br><br><span class="hljs-keyword">public</span>:<br><br><span class="hljs-built_in">Person</span>(T1 name, T2 age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><br><br><span class="hljs-keyword">private</span>:<br>T1 m_Name;<br>T2 m_Age;<br><br>};<br><br><span class="hljs-comment">//1、全局函数在类内实现</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Person <string, <span class="hljs-keyword">int</span> ><span class="hljs-built_in">p</span>(<span class="hljs-string">"Tom"</span>, <span class="hljs-number">20</span>);<br><span class="hljs-built_in">printPerson</span>(p);<br>}<br><br><br><span class="hljs-comment">//2、全局函数在类外实现</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>Person <string, <span class="hljs-keyword">int</span> ><span class="hljs-built_in">p</span>(<span class="hljs-string">"Jerry"</span>, <span class="hljs-number">30</span>);<br><span class="hljs-built_in">printPerson2</span>(p);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别</p><h4 id="1-3-9-类模板案例"><a href="#1-3-9-类模板案例" class="headerlink" title="1.3.9 类模板案例"></a>1.3.9 类模板案例</h4><p>案例描述: 实现一个通用的数组类,要求如下:</p><ul><li>可以对内置数据类型以及自定义数据类型的数据进行存储</li><li>将数组中的数据存储到堆区</li><li>构造函数中可以传入数组的容量</li><li>提供对应的拷贝构造函数以及operator=防止浅拷贝问题</li><li>提供尾插法和尾删法对数组中的数据进行增加和删除</li><li>可以通过下标的方式访问数组中的元素</li><li>可以获取数组中当前元素个数和数组的容量</li></ul><p><strong>示例:</strong></p><p>myArray.hpp中代码</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> once</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><span class="hljs-keyword">template</span><<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T</span>></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyArray</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br> <br><span class="hljs-comment">//构造函数</span><br><span class="hljs-built_in">MyArray</span>(<span class="hljs-keyword">int</span> capacity)<br>{<br><span class="hljs-keyword">this</span>->m_Capacity = capacity;<br><span class="hljs-keyword">this</span>->m_Size = <span class="hljs-number">0</span>;<br>pAddress = <span class="hljs-keyword">new</span> T[<span class="hljs-keyword">this</span>->m_Capacity];<br>}<br><br><span class="hljs-comment">//拷贝构造</span><br><span class="hljs-built_in">MyArray</span>(<span class="hljs-keyword">const</span> MyArray & arr)<br>{<br><span class="hljs-keyword">this</span>->m_Capacity = arr.m_Capacity;<br><span class="hljs-keyword">this</span>->m_Size = arr.m_Size;<br><span class="hljs-keyword">this</span>->pAddress = <span class="hljs-keyword">new</span> T[<span class="hljs-keyword">this</span>->m_Capacity];<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-keyword">this</span>->m_Size; i++)<br>{<br><span class="hljs-comment">//如果T为对象,而且还包含指针,必须需要重载 = 操作符,因为这个等号不是 构造 而是赋值,</span><br><span class="hljs-comment">// 普通类型可以直接= 但是指针类型需要深拷贝</span><br><span class="hljs-keyword">this</span>->pAddress[i] = arr.pAddress[i];<br>}<br>}<br><br><span class="hljs-comment">//重载= 操作符 防止浅拷贝问题</span><br>MyArray& <span class="hljs-keyword">operator</span>=(<span class="hljs-keyword">const</span> MyArray& myarray) {<br><br><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>->pAddress != <span class="hljs-literal">NULL</span>) {<br><span class="hljs-keyword">delete</span>[] <span class="hljs-keyword">this</span>->pAddress;<br><span class="hljs-keyword">this</span>->m_Capacity = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">this</span>->m_Size = <span class="hljs-number">0</span>;<br>}<br><br><span class="hljs-keyword">this</span>->m_Capacity = myarray.m_Capacity;<br><span class="hljs-keyword">this</span>->m_Size = myarray.m_Size;<br><span class="hljs-keyword">this</span>->pAddress = <span class="hljs-keyword">new</span> T[<span class="hljs-keyword">this</span>->m_Capacity];<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-keyword">this</span>->m_Size; i++) {<br><span class="hljs-keyword">this</span>->pAddress[i] = myarray[i];<br>}<br><span class="hljs-keyword">return</span> *<span class="hljs-keyword">this</span>;<br>}<br><br><span class="hljs-comment">//重载[] 操作符 arr[0]</span><br>T& <span class="hljs-keyword">operator</span> [](<span class="hljs-keyword">int</span> index)<br>{<br><span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>->pAddress[index]; <span class="hljs-comment">//不考虑越界,用户自己去处理</span><br>}<br><br><span class="hljs-comment">//尾插法</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Push_back</span><span class="hljs-params">(<span class="hljs-keyword">const</span> T & val)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>->m_Capacity == <span class="hljs-keyword">this</span>->m_Size)<br>{<br><span class="hljs-keyword">return</span>;<br>}<br><span class="hljs-keyword">this</span>->pAddress[<span class="hljs-keyword">this</span>->m_Size] = val;<br><span class="hljs-keyword">this</span>->m_Size++;<br>}<br><br><span class="hljs-comment">//尾删法</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Pop_back</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>->m_Size == <span class="hljs-number">0</span>)<br>{<br><span class="hljs-keyword">return</span>;<br>}<br><span class="hljs-keyword">this</span>->m_Size--;<br>}<br><br><span class="hljs-comment">//获取数组容量</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getCapacity</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>->m_Capacity;<br>}<br><br><span class="hljs-comment">//获取数组大小</span><br><span class="hljs-function"><span class="hljs-keyword">int</span><span class="hljs-title">getSize</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>->m_Size;<br>}<br><br><br><span class="hljs-comment">//析构</span><br>~<span class="hljs-built_in">MyArray</span>()<br>{<br><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>->pAddress != <span class="hljs-literal">NULL</span>)<br>{<br><span class="hljs-keyword">delete</span>[] <span class="hljs-keyword">this</span>->pAddress;<br><span class="hljs-keyword">this</span>->pAddress = <span class="hljs-literal">NULL</span>;<br><span class="hljs-keyword">this</span>->m_Capacity = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">this</span>->m_Size = <span class="hljs-number">0</span>;<br>}<br>}<br><br><span class="hljs-keyword">private</span>:<br>T * pAddress; <span class="hljs-comment">//指向一个堆空间,这个空间存储真正的数据</span><br><span class="hljs-keyword">int</span> m_Capacity; <span class="hljs-comment">//容量</span><br><span class="hljs-keyword">int</span> m_Size; <span class="hljs-comment">// 大小</span><br>};<br></code></pre></td></tr></table></figure><p>类模板案例—数组类封装.cpp中</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"myArray.hpp"</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printIntArray</span><span class="hljs-params">(MyArray<<span class="hljs-keyword">int</span>>& arr)</span> </span>{<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < arr.<span class="hljs-built_in">getSize</span>(); i++) {<br>cout << arr[i] << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//测试内置数据类型</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-function">MyArray<<span class="hljs-keyword">int</span>> <span class="hljs-title">array1</span><span class="hljs-params">(<span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>array1.<span class="hljs-built_in">Push_back</span>(i);<br>}<br>cout << <span class="hljs-string">"array1打印输出:"</span> << endl;<br><span class="hljs-built_in">printIntArray</span>(array1);<br>cout << <span class="hljs-string">"array1的大小:"</span> << array1.<span class="hljs-built_in">getSize</span>() << endl;<br>cout << <span class="hljs-string">"array1的容量:"</span> << array1.<span class="hljs-built_in">getCapacity</span>() << endl;<br><br>cout << <span class="hljs-string">"--------------------------"</span> << endl;<br><br><span class="hljs-function">MyArray<<span class="hljs-keyword">int</span>> <span class="hljs-title">array2</span><span class="hljs-params">(array1)</span></span>;<br>array2.<span class="hljs-built_in">Pop_back</span>();<br>cout << <span class="hljs-string">"array2打印输出:"</span> << endl;<br><span class="hljs-built_in">printIntArray</span>(array2);<br>cout << <span class="hljs-string">"array2的大小:"</span> << array2.<span class="hljs-built_in">getSize</span>() << endl;<br>cout << <span class="hljs-string">"array2的容量:"</span> << array2.<span class="hljs-built_in">getCapacity</span>() << endl;<br>}<br><br><span class="hljs-comment">//测试自定义数据类型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>() {} <br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age) {<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><span class="hljs-keyword">public</span>:<br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printPersonArray</span><span class="hljs-params">(MyArray<Person>& personArr)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < personArr.<span class="hljs-built_in">getSize</span>(); i++) {<br>cout << <span class="hljs-string">"姓名:"</span> << personArr[i].m_Name << <span class="hljs-string">" 年龄: "</span> << personArr[i].m_Age << endl;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//创建数组</span><br><span class="hljs-function">MyArray<Person> <span class="hljs-title">pArray</span><span class="hljs-params">(<span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"孙悟空"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"韩信"</span>, <span class="hljs-number">20</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"妲己"</span>, <span class="hljs-number">18</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"王昭君"</span>, <span class="hljs-number">15</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p5</span><span class="hljs-params">(<span class="hljs-string">"赵云"</span>, <span class="hljs-number">24</span>)</span></span>;<br><br><span class="hljs-comment">//插入数据</span><br>pArray.<span class="hljs-built_in">Push_back</span>(p1);<br>pArray.<span class="hljs-built_in">Push_back</span>(p2);<br>pArray.<span class="hljs-built_in">Push_back</span>(p3);<br>pArray.<span class="hljs-built_in">Push_back</span>(p4);<br>pArray.<span class="hljs-built_in">Push_back</span>(p5);<br><br><span class="hljs-built_in">printPersonArray</span>(pArray);<br><br>cout << <span class="hljs-string">"pArray的大小:"</span> << pArray.<span class="hljs-built_in">getSize</span>() << endl;<br>cout << <span class="hljs-string">"pArray的容量:"</span> << pArray.<span class="hljs-built_in">getCapacity</span>() << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><p>能够利用所学知识点实现通用的数组</p><h2 id="2-STL初识"><a href="#2-STL初识" class="headerlink" title="2 STL初识"></a>2 STL初识</h2><h3 id="2-1-STL的诞生"><a href="#2-1-STL的诞生" class="headerlink" title="2.1 STL的诞生"></a>2.1 STL的诞生</h3><ul><li><p>长久以来,软件界一直希望建立一种可重复利用的东西</p></li><li><p>C++的<strong>面向对象</strong>和<strong>泛型编程</strong>思想,目的就是<strong>复用性的提升</strong></p></li><li><p>大多情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作</p></li><li><p>为了建立数据结构和算法的一套标准,诞生了<strong>STL</strong></p></li></ul><h3 id="2-2-STL基本概念"><a href="#2-2-STL基本概念" class="headerlink" title="2.2 STL基本概念"></a>2.2 STL基本概念</h3><ul><li>STL(Standard Template Library,<strong>标准模板库</strong>)</li><li>STL 从广义上分为: <strong>容器(container) 算法(algorithm) 迭代器(iterator)</strong></li><li><strong>容器</strong>和<strong>算法</strong>之间通过<strong>迭代器</strong>进行无缝连接。</li><li>STL 几乎所有的代码都采用了模板类或者模板函数</li></ul><h3 id="2-3-STL六大组件"><a href="#2-3-STL六大组件" class="headerlink" title="2.3 STL六大组件"></a>2.3 STL六大组件</h3><p>STL大体分为六大组件,分别是:<strong>容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器</strong></p><ol><li>容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。</li><li>算法:各种常用的算法,如sort、find、copy、for_each等</li><li>迭代器:扮演了容器与算法之间的胶合剂。</li><li>仿函数:行为类似函数,可作为算法的某种策略。</li><li>适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。</li><li>空间配置器:负责空间的配置与管理。</li></ol><h3 id="2-4-STL中容器、算法、迭代器"><a href="#2-4-STL中容器、算法、迭代器" class="headerlink" title="2.4 STL中容器、算法、迭代器"></a>2.4 STL中容器、算法、迭代器</h3><p><strong>容器:</strong>置物之所也</p><p>STL<strong>容器</strong>就是将运用<strong>最广泛的一些数据结构</strong>实现出来</p><p>常用的数据结构:数组, 链表,树, 栈, 队列, 集合, 映射表 等</p><p>这些容器分为<strong>序列式容器</strong>和<strong>关联式容器</strong>两种:</p><p> <strong>序列式容器</strong>:强调值的排序,序列式容器中的每个元素均有固定的位置。<br> <strong>关联式容器</strong>:二叉树结构,各元素之间没有严格的物理上的顺序关系</p><p><strong>算法:</strong>问题之解法也</p><p>有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)</p><p>算法分为:<strong>质变算法</strong>和<strong>非质变算法</strong>。</p><p>质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等</p><p>非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等</p><p><strong>迭代器:</strong>容器和算法之间粘合剂</p><p>提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。</p><p>每个容器都有自己专属的迭代器</p><p>迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针</p><p>迭代器种类:</p><table><thead><tr><th>种类</th><th>功能</th><th>支持运算</th></tr></thead><tbody><tr><td>输入迭代器</td><td>对数据的只读访问</td><td>只读,支持++、==、!=</td></tr><tr><td>输出迭代器</td><td>对数据的只写访问</td><td>只写,支持++</td></tr><tr><td>前向迭代器</td><td>读写操作,并能向前推进迭代器</td><td>读写,支持++、==、!=</td></tr><tr><td>双向迭代器</td><td>读写操作,并能向前和向后操作</td><td>读写,支持++、–,</td></tr><tr><td>随机访问迭代器</td><td>读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器</td><td>读写,支持++、–、[n]、-n、<、<=、>、>=</td></tr></tbody></table><p>常用的容器中迭代器种类为双向迭代器,和随机访问迭代器</p><h3 id="2-5-容器算法迭代器初识"><a href="#2-5-容器算法迭代器初识" class="headerlink" title="2.5 容器算法迭代器初识"></a>2.5 容器算法迭代器初识</h3><p>了解STL中容器、算法、迭代器概念之后,我们利用代码感受STL的魅力</p><p>STL中最常用的容器为Vector,可以理解为数组,下面我们将学习如何向这个容器中插入数据、并遍历这个容器</p><h4 id="2-5-1-vector存放内置数据类型"><a href="#2-5-1-vector存放内置数据类型" class="headerlink" title="2.5.1 vector存放内置数据类型"></a>2.5.1 vector存放内置数据类型</h4><p>容器: <code>vector</code></p><p>算法: <code>for_each</code></p><p>迭代器: <code>vector<int>::iterator</code></p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MyPrint</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//创建vector容器对象,并且通过模板参数指定容器中存放的数据的类型</span><br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-comment">//向容器中放数据</span><br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br><span class="hljs-comment">//每一个容器都有自己的迭代器,迭代器是用来遍历容器中的元素</span><br><span class="hljs-comment">//v.begin()返回迭代器,这个迭代器指向容器中第一个数据</span><br><span class="hljs-comment">//v.end()返回迭代器,这个迭代器指向容器元素的最后一个元素的下一个位置</span><br><span class="hljs-comment">//vector<int>::iterator 拿到vector<int>这种容器的迭代器类型</span><br><br>vector<<span class="hljs-keyword">int</span>>::iterator pBegin = v.<span class="hljs-built_in">begin</span>();<br>vector<<span class="hljs-keyword">int</span>>::iterator pEnd = v.<span class="hljs-built_in">end</span>();<br><br><span class="hljs-comment">//第一种遍历方式:</span><br><span class="hljs-keyword">while</span> (pBegin != pEnd) {<br>cout << *pBegin << endl;<br>pBegin++;<br>}<br><br><br><span class="hljs-comment">//第二种遍历方式:</span><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << endl;<br>}<br>cout << endl;<br><br><span class="hljs-comment">//第三种遍历方式:</span><br><span class="hljs-comment">//使用STL提供标准遍历算法 头文件 algorithm</span><br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), MyPrint);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h4 id="2-5-2-Vector存放自定义数据类型"><a href="#2-5-2-Vector存放自定义数据类型" class="headerlink" title="2.5.2 Vector存放自定义数据类型"></a>2.5.2 Vector存放自定义数据类型</h4><p>学习目标:vector中存放自定义数据类型,并打印输出</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-comment">//自定义数据类型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age) {<br>mName = name;<br>mAge = age;<br>}<br><span class="hljs-keyword">public</span>:<br>string mName;<br><span class="hljs-keyword">int</span> mAge;<br>};<br><span class="hljs-comment">//存放对象</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>vector<Person> v;<br><br><span class="hljs-comment">//创建数据</span><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"aaa"</span>, <span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"bbb"</span>, <span class="hljs-number">20</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"ccc"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"ddd"</span>, <span class="hljs-number">40</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p5</span><span class="hljs-params">(<span class="hljs-string">"eee"</span>, <span class="hljs-number">50</span>)</span></span>;<br><br>v.<span class="hljs-built_in">push_back</span>(p1);<br>v.<span class="hljs-built_in">push_back</span>(p2);<br>v.<span class="hljs-built_in">push_back</span>(p3);<br>v.<span class="hljs-built_in">push_back</span>(p4);<br>v.<span class="hljs-built_in">push_back</span>(p5);<br><br><span class="hljs-keyword">for</span> (vector<Person>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << <span class="hljs-string">"Name:"</span> << (*it).mName << <span class="hljs-string">" Age:"</span> << (*it).mAge << endl;<br><br>}<br>}<br><br><br><span class="hljs-comment">//放对象指针</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span> </span>{<br><br>vector<Person*> v;<br><br><span class="hljs-comment">//创建数据</span><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"aaa"</span>, <span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"bbb"</span>, <span class="hljs-number">20</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"ccc"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"ddd"</span>, <span class="hljs-number">40</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p5</span><span class="hljs-params">(<span class="hljs-string">"eee"</span>, <span class="hljs-number">50</span>)</span></span>;<br><br>v.<span class="hljs-built_in">push_back</span>(&p1);<br>v.<span class="hljs-built_in">push_back</span>(&p2);<br>v.<span class="hljs-built_in">push_back</span>(&p3);<br>v.<span class="hljs-built_in">push_back</span>(&p4);<br>v.<span class="hljs-built_in">push_back</span>(&p5);<br><br><span class="hljs-keyword">for</span> (vector<Person*>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>Person * p = (*it);<br>cout << <span class="hljs-string">"Name:"</span> << p->mName << <span class="hljs-string">" Age:"</span> << (*it)->mAge << endl;<br>}<br>}<br><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br> <br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h4 id="2-5-3-Vector容器嵌套容器"><a href="#2-5-3-Vector容器嵌套容器" class="headerlink" title="2.5.3 Vector容器嵌套容器"></a>2.5.3 Vector容器嵌套容器</h4><p>学习目标:容器中嵌套容器,我们将所有数据进行遍历输出</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-comment">//容器嵌套容器</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>vector< vector<<span class="hljs-keyword">int</span>> > v;<br><br>vector<<span class="hljs-keyword">int</span>> v1;<br>vector<<span class="hljs-keyword">int</span>> v2;<br>vector<<span class="hljs-keyword">int</span>> v3;<br>vector<<span class="hljs-keyword">int</span>> v4;<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">4</span>; i++) {<br>v1.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">1</span>);<br>v2.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">2</span>);<br>v3.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">3</span>);<br>v4.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">4</span>);<br>}<br><br><span class="hljs-comment">//将容器元素插入到vector v中</span><br>v.<span class="hljs-built_in">push_back</span>(v1);<br>v.<span class="hljs-built_in">push_back</span>(v2);<br>v.<span class="hljs-built_in">push_back</span>(v3);<br>v.<span class="hljs-built_in">push_back</span>(v4);<br><br><br><span class="hljs-keyword">for</span> (vector<vector<<span class="hljs-keyword">int</span>>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator vit = (*it).<span class="hljs-built_in">begin</span>(); vit != (*it).<span class="hljs-built_in">end</span>(); vit++) {<br>cout << *vit << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h2 id="3-STL-常用容器"><a href="#3-STL-常用容器" class="headerlink" title="3 STL- 常用容器"></a>3 STL- 常用容器</h2><h3 id="3-1-string容器"><a href="#3-1-string容器" class="headerlink" title="3.1 string容器"></a>3.1 string容器</h3><h4 id="3-1-1-string基本概念"><a href="#3-1-1-string基本概念" class="headerlink" title="3.1.1 string基本概念"></a>3.1.1 string基本概念</h4><p><strong>本质:</strong></p><ul><li>string是C++风格的字符串,而string本质上是一个类</li></ul><p><strong>string和char * 区别:</strong></p><ul><li>char * 是一个指针</li><li>string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。</li></ul><p><strong>特点:</strong></p><p>string 类内部封装了很多成员方法</p><p>例如:查找find,拷贝copy,删除delete 替换replace,插入insert</p><p>string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责</p><h4 id="3-1-2-string构造函数"><a href="#3-1-2-string构造函数" class="headerlink" title="3.1.2 string构造函数"></a>3.1.2 string构造函数</h4><p>构造函数原型:</p><ul><li><code>string();</code> //创建一个空的字符串 例如: string str;<br> <code>string(const char* s);</code> //使用字符串s初始化</li><li><code>string(const string& str);</code> //使用一个string对象初始化另一个string对象</li><li><code>string(int n, char c);</code> //使用n个字符c初始化 </li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-comment">//string构造</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>string s1; <span class="hljs-comment">//创建空字符串,调用无参构造函数</span><br>cout << <span class="hljs-string">"str1 = "</span> << s1 << endl;<br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* str = <span class="hljs-string">"hello world"</span>;<br><span class="hljs-function">string <span class="hljs-title">s2</span><span class="hljs-params">(str)</span></span>; <span class="hljs-comment">//把c_string转换成了string</span><br><br>cout << <span class="hljs-string">"str2 = "</span> << s2 << endl;<br><br><span class="hljs-function">string <span class="hljs-title">s3</span><span class="hljs-params">(s2)</span></span>; <span class="hljs-comment">//调用拷贝构造函数</span><br>cout << <span class="hljs-string">"str3 = "</span> << s3 << endl;<br><br><span class="hljs-function">string <span class="hljs-title">s4</span><span class="hljs-params">(<span class="hljs-number">10</span>, <span class="hljs-string">'a'</span>)</span></span>;<br>cout << <span class="hljs-string">"str3 = "</span> << s3 << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:string的多种构造方式没有可比性,灵活使用即可</p><h4 id="3-1-3-string赋值操作"><a href="#3-1-3-string赋值操作" class="headerlink" title="3.1.3 string赋值操作"></a>3.1.3 string赋值操作</h4><p>功能描述:</p><ul><li>给string字符串进行赋值</li></ul><p>赋值的函数原型:</p><ul><li><code>string& operator=(const char* s);</code> //char*类型字符串 赋值给当前的字符串</li><li><code>string& operator=(const string &s);</code> //把字符串s赋给当前的字符串</li><li><code>string& operator=(char c);</code> //字符赋值给当前的字符串</li><li><code>string& assign(const char *s);</code> //把字符串s赋给当前的字符串</li><li><code>string& assign(const char *s, int n);</code> //把字符串s的前n个字符赋给当前的字符串</li><li><code>string& assign(const string &s);</code> //把字符串s赋给当前字符串</li><li><code>string& assign(int n, char c);</code> //用n个字符c赋给当前字符串</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//赋值</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>string str1;<br>str1 = <span class="hljs-string">"hello world"</span>;<br>cout << <span class="hljs-string">"str1 = "</span> << str1 << endl;<br><br>string str2;<br>str2 = str1;<br>cout << <span class="hljs-string">"str2 = "</span> << str2 << endl;<br><br>string str3;<br>str3 = <span class="hljs-string">'a'</span>;<br>cout << <span class="hljs-string">"str3 = "</span> << str3 << endl;<br><br>string str4;<br>str4.<span class="hljs-built_in">assign</span>(<span class="hljs-string">"hello c++"</span>);<br>cout << <span class="hljs-string">"str4 = "</span> << str4 << endl;<br><br>string str5;<br>str5.<span class="hljs-built_in">assign</span>(<span class="hljs-string">"hello c++"</span>,<span class="hljs-number">5</span>);<br>cout << <span class="hljs-string">"str5 = "</span> << str5 << endl;<br><br><br>string str6;<br>str6.<span class="hljs-built_in">assign</span>(str5);<br>cout << <span class="hljs-string">"str6 = "</span> << str6 << endl;<br><br>string str7;<br>str7.<span class="hljs-built_in">assign</span>(<span class="hljs-number">5</span>, <span class="hljs-string">'x'</span>);<br>cout << <span class="hljs-string">"str7 = "</span> << str7 << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><p> string的赋值方式很多,<code>operator=</code> 这种方式是比较实用的</p><h4 id="3-1-4-string字符串拼接"><a href="#3-1-4-string字符串拼接" class="headerlink" title="3.1.4 string字符串拼接"></a>3.1.4 string字符串拼接</h4><p><strong>功能描述:</strong></p><ul><li>实现在字符串末尾拼接字符串</li></ul><p><strong>函数原型:</strong></p><ul><li><code>string& operator+=(const char* str);</code> //重载+=操作符</li><li><code>string& operator+=(const char c);</code> //重载+=操作符</li><li><code>string& operator+=(const string& str);</code> //重载+=操作符</li><li><code>string& append(const char *s); </code> //把字符串s连接到当前字符串结尾</li><li><code>string& append(const char *s, int n);</code> //把字符串s的前n个字符连接到当前字符串结尾</li><li><code>string& append(const string &s);</code> //同operator+=(const string& str)</li><li><code>string& append(const string &s, int pos, int n);</code>//字符串s中从pos开始的n个字符连接到字符串结尾</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//字符串拼接</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>string str1 = <span class="hljs-string">"我"</span>;<br><br>str1 += <span class="hljs-string">"爱玩游戏"</span>;<br><br>cout << <span class="hljs-string">"str1 = "</span> << str1 << endl;<br><br>str1 += <span class="hljs-string">':'</span>;<br><br>cout << <span class="hljs-string">"str1 = "</span> << str1 << endl;<br><br>string str2 = <span class="hljs-string">"LOL DNF"</span>;<br><br>str1 += str2;<br><br>cout << <span class="hljs-string">"str1 = "</span> << str1 << endl;<br><br>string str3 = <span class="hljs-string">"I"</span>;<br>str3.<span class="hljs-built_in">append</span>(<span class="hljs-string">" love "</span>);<br>str3.<span class="hljs-built_in">append</span>(<span class="hljs-string">"game abcde"</span>, <span class="hljs-number">4</span>);<br><span class="hljs-comment">//str3.append(str2);</span><br>str3.<span class="hljs-built_in">append</span>(str2, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>); <span class="hljs-comment">// 从下标4位置开始 ,截取3个字符,拼接到字符串末尾</span><br>cout << <span class="hljs-string">"str3 = "</span> << str3 << endl;<br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:字符串拼接的重载版本很多,初学阶段记住几种即可</p><h4 id="3-1-5-string查找和替换"><a href="#3-1-5-string查找和替换" class="headerlink" title="3.1.5 string查找和替换"></a>3.1.5 string查找和替换</h4><p><strong>功能描述:</strong></p><ul><li>查找:查找指定字符串是否存在</li><li>替换:在指定的位置替换字符串</li></ul><p><strong>函数原型:</strong></p><ul><li><code>int find(const string& str, int pos = 0) const;</code> //查找str第一次出现位置,从pos开始查找</li><li><code>int find(const char* s, int pos = 0) const; </code> //查找s第一次出现位置,从pos开始查找</li><li><code>int find(const char* s, int pos, int n) const; </code> //从pos位置查找s的前n个字符第一次位置</li><li><code>int find(const char c, int pos = 0) const; </code> //查找字符c第一次出现位置</li><li><code>int rfind(const string& str, int pos = npos) const;</code> //查找str最后一次位置,从pos开始查找</li><li><code>int rfind(const char* s, int pos = npos) const;</code> //查找s最后一次出现位置,从pos开始查找</li><li><code>int rfind(const char* s, int pos, int n) const;</code> //从pos查找s的前n个字符最后一次位置</li><li><code>int rfind(const char c, int pos = 0) const; </code> //查找字符c最后一次出现位置</li><li><code>string& replace(int pos, int n, const string& str); </code> //替换从pos开始n个字符为字符串str</li><li><code>string& replace(int pos, int n,const char* s); </code> //替换从pos开始的n个字符为字符串s</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//查找和替换</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//查找</span><br>string str1 = <span class="hljs-string">"abcdefgde"</span>;<br><br><span class="hljs-keyword">int</span> pos = str1.<span class="hljs-built_in">find</span>(<span class="hljs-string">"de"</span>);<br><br><span class="hljs-keyword">if</span> (pos == <span class="hljs-number">-1</span>)<br>{<br>cout << <span class="hljs-string">"未找到"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"pos = "</span> << pos << endl;<br>}<br><br><br>pos = str1.<span class="hljs-built_in">rfind</span>(<span class="hljs-string">"de"</span>);<br><br>cout << <span class="hljs-string">"pos = "</span> << pos << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//替换</span><br>string str1 = <span class="hljs-string">"abcdefgde"</span>;<br>str1.<span class="hljs-built_in">replace</span>(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-string">"1111"</span>);<br><br>cout << <span class="hljs-string">"str1 = "</span> << str1 << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><span class="hljs-comment">//test02();</span><br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>find查找是从左往后,rfind从右往左</li><li>find找到字符串后返回查找的第一个字符位置,找不到返回-1</li><li>replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串</li></ul><h4 id="3-1-6-string字符串比较"><a href="#3-1-6-string字符串比较" class="headerlink" title="3.1.6 string字符串比较"></a>3.1.6 string字符串比较</h4><p><strong>功能描述:</strong></p><ul><li>字符串之间的比较</li></ul><p><strong>比较方式:</strong></p><ul><li>字符串比较是按字符的ASCII码进行对比</li></ul><p>= 返回 0</p><p>> 返回 1 </p><p>< 返回 -1</p><p><strong>函数原型:</strong></p><ul><li><code>int compare(const string &s) const; </code> //与字符串s比较</li><li><code>int compare(const char *s) const;</code> //与字符串s比较</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//字符串比较</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>string s1 = <span class="hljs-string">"hello"</span>;<br>string s2 = <span class="hljs-string">"aello"</span>;<br><br><span class="hljs-keyword">int</span> ret = s1.<span class="hljs-built_in">compare</span>(s2);<br><br><span class="hljs-keyword">if</span> (ret == <span class="hljs-number">0</span>) {<br>cout << <span class="hljs-string">"s1 等于 s2"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (ret > <span class="hljs-number">0</span>)<br>{<br>cout << <span class="hljs-string">"s1 大于 s2"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"s1 小于 s2"</span> << endl;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大</p><h4 id="3-1-7-string字符存取"><a href="#3-1-7-string字符存取" class="headerlink" title="3.1.7 string字符存取"></a>3.1.7 string字符存取</h4><p>string中单个字符存取方式有两种</p><ul><li><code>char& operator[](int n); </code> //通过[]方式取字符</li><li><code>char& at(int n); </code> //通过at方法获取字符</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>string str = <span class="hljs-string">"hello world"</span>;<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < str.<span class="hljs-built_in">size</span>(); i++)<br>{<br>cout << str[i] << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < str.<span class="hljs-built_in">size</span>(); i++)<br>{<br>cout << str.<span class="hljs-built_in">at</span>(i) << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><br><span class="hljs-comment">//字符修改</span><br>str[<span class="hljs-number">0</span>] = <span class="hljs-string">'x'</span>;<br>str.<span class="hljs-built_in">at</span>(<span class="hljs-number">1</span>) = <span class="hljs-string">'x'</span>;<br>cout << str << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:string字符串中单个字符存取有两种方式,利用 [ ] 或 at</p><h4 id="3-1-8-string插入和删除"><a href="#3-1-8-string插入和删除" class="headerlink" title="3.1.8 string插入和删除"></a>3.1.8 string插入和删除</h4><p><strong>功能描述:</strong></p><ul><li>对string字符串进行插入和删除字符操作</li></ul><p><strong>函数原型:</strong></p><ul><li><code>string& insert(int pos, const char* s); </code> //插入字符串</li><li><code>string& insert(int pos, const string& str); </code> //插入字符串</li><li><code>string& insert(int pos, int n, char c);</code> //在指定位置插入n个字符c</li><li><code>string& erase(int pos, int n = npos);</code> //删除从Pos开始的n个字符 </li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//字符串插入和删除</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>string str = <span class="hljs-string">"hello"</span>;<br>str.<span class="hljs-built_in">insert</span>(<span class="hljs-number">1</span>, <span class="hljs-string">"111"</span>);<br>cout << str << endl;<br><br>str.<span class="hljs-built_in">erase</span>(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>); <span class="hljs-comment">//从1号位置开始3个字符</span><br>cout << str << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>插入和删除的起始下标都是从0开始</p><h4 id="3-1-9-string子串"><a href="#3-1-9-string子串" class="headerlink" title="3.1.9 string子串"></a>3.1.9 string子串</h4><p><strong>功能描述:</strong></p><ul><li>从字符串中获取想要的子串</li></ul><p><strong>函数原型:</strong></p><ul><li><code>string substr(int pos = 0, int n = npos) const;</code> //返回由pos开始的n个字符组成的字符串</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//子串</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>string str = <span class="hljs-string">"abcdefg"</span>;<br>string subStr = str.<span class="hljs-built_in">substr</span>(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>);<br>cout << <span class="hljs-string">"subStr = "</span> << subStr << endl;<br><br>string email = <span class="hljs-string">"hello@sina.com"</span>;<br><span class="hljs-keyword">int</span> pos = email.<span class="hljs-built_in">find</span>(<span class="hljs-string">"@"</span>);<br>string username = email.<span class="hljs-built_in">substr</span>(<span class="hljs-number">0</span>, pos);<br>cout << <span class="hljs-string">"username: "</span> << username << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>灵活的运用求子串功能,可以在实际开发中获取有效的信息</p><h3 id="3-2-vector容器"><a href="#3-2-vector容器" class="headerlink" title="3.2 vector容器"></a>3.2 vector容器</h3><h4 id="3-2-1-vector基本概念"><a href="#3-2-1-vector基本概念" class="headerlink" title="3.2.1 vector基本概念"></a>3.2.1 vector基本概念</h4><p><strong>功能:</strong></p><ul><li>vector数据结构和<strong>数组非常相似</strong>,也称为<strong>单端数组</strong></li></ul><p><strong>vector与普通数组区别:</strong></p><ul><li>不同之处在于数组是静态空间,而vector可以<strong>动态扩展</strong></li></ul><p><strong>动态扩展:</strong></p><ul><li>并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间</li></ul><p><img src="assets/clip_image002.jpg" alt="说明: 2015-11-10_151152"></p><ul><li>vector容器的迭代器是支持随机访问的迭代器</li></ul><h4 id="3-2-2-vector构造函数"><a href="#3-2-2-vector构造函数" class="headerlink" title="3.2.2 vector构造函数"></a>3.2.2 vector构造函数</h4><p><strong>功能描述:</strong></p><ul><li>创建vector容器</li></ul><p><strong>函数原型:</strong></p><ul><li><code>vector<T> v; </code> //采用模板实现类实现,默认构造函数</li><li><code>vector(v.begin(), v.end()); </code> //将v[begin(), end())区间中的元素拷贝给本身。</li><li><code>vector(n, elem);</code> //构造函数将n个elem拷贝给本身。</li><li><code>vector(const vector &vec);</code> //拷贝构造函数。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printVector</span><span class="hljs-params">(vector<<span class="hljs-keyword">int</span>>& v)</span> </span>{<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1; <span class="hljs-comment">//无参构造</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printVector</span>(v1);<br><br><span class="hljs-function">vector<<span class="hljs-keyword">int</span>> <span class="hljs-title">v2</span><span class="hljs-params">(v1.begin(), v1.end())</span></span>;<br><span class="hljs-built_in">printVector</span>(v2);<br><br><span class="hljs-function">vector<<span class="hljs-keyword">int</span>> <span class="hljs-title">v3</span><span class="hljs-params">(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>)</span></span>;<br><span class="hljs-built_in">printVector</span>(v3);<br><br><span class="hljs-function">vector<<span class="hljs-keyword">int</span>> <span class="hljs-title">v4</span><span class="hljs-params">(v3)</span></span>;<br><span class="hljs-built_in">printVector</span>(v4);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>vector的多种构造方式没有可比性,灵活使用即可</p><h4 id="3-2-3-vector赋值操作"><a href="#3-2-3-vector赋值操作" class="headerlink" title="3.2.3 vector赋值操作"></a>3.2.3 vector赋值操作</h4><p><strong>功能描述:</strong></p><ul><li>给vector容器进行赋值</li></ul><p><strong>函数原型:</strong></p><ul><li><code>vector& operator=(const vector &vec);</code>//重载等号操作符</li></ul><ul><li><code>assign(beg, end);</code> //将[beg, end)区间中的数据拷贝赋值给本身。</li><li><code>assign(n, elem);</code> //将n个elem拷贝赋值给本身。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printVector</span><span class="hljs-params">(vector<<span class="hljs-keyword">int</span>>& v)</span> </span>{<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//赋值操作</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1; <span class="hljs-comment">//无参构造</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printVector</span>(v1);<br><br>vector<<span class="hljs-keyword">int</span>>v2;<br>v2 = v1;<br><span class="hljs-built_in">printVector</span>(v2);<br><br>vector<<span class="hljs-keyword">int</span>>v3;<br>v3.<span class="hljs-built_in">assign</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>());<br><span class="hljs-built_in">printVector</span>(v3);<br><br>vector<<span class="hljs-keyword">int</span>>v4;<br>v4.<span class="hljs-built_in">assign</span>(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>);<br><span class="hljs-built_in">printVector</span>(v4);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><br></code></pre></td></tr></table></figure><p>总结: vector赋值方式比较简单,使用operator=,或者assign都可以</p><h4 id="3-2-4-vector容量和大小"><a href="#3-2-4-vector容量和大小" class="headerlink" title="3.2.4 vector容量和大小"></a>3.2.4 vector容量和大小</h4><p><strong>功能描述:</strong></p><ul><li>对vector容器的容量和大小操作</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>empty(); </code> //判断容器是否为空</p></li><li><p><code>capacity();</code> //容器的容量</p></li><li><p><code>size();</code> //返回容器中元素的个数</p></li><li><p><code>resize(int num);</code> //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。</p><p> //如果容器变短,则末尾超出容器长度的元素被删除。</p></li><li><p><code>resize(int num, elem);</code> //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。</p><p> //如果容器变短,则末尾超出容器长度的元素被删除</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printVector</span><span class="hljs-params">(vector<<span class="hljs-keyword">int</span>>& v)</span> </span>{<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printVector</span>(v1);<br><span class="hljs-keyword">if</span> (v1.<span class="hljs-built_in">empty</span>())<br>{<br>cout << <span class="hljs-string">"v1为空"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"v1不为空"</span> << endl;<br>cout << <span class="hljs-string">"v1的容量 = "</span> << v1.<span class="hljs-built_in">capacity</span>() << endl;<br>cout << <span class="hljs-string">"v1的大小 = "</span> << v1.<span class="hljs-built_in">size</span>() << endl;<br>}<br><br><span class="hljs-comment">//resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充</span><br>v1.<span class="hljs-built_in">resize</span>(<span class="hljs-number">15</span>,<span class="hljs-number">10</span>);<br><span class="hljs-built_in">printVector</span>(v1);<br><br><span class="hljs-comment">//resize 重新指定大小 ,若指定的更小,超出部分元素被删除</span><br>v1.<span class="hljs-built_in">resize</span>(<span class="hljs-number">5</span>);<br><span class="hljs-built_in">printVector</span>(v1);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>判断是否为空 — empty</li><li>返回元素个数 — size</li><li>返回容器容量 — capacity</li><li>重新指定大小 — resize</li></ul><h4 id="3-2-5-vector插入和删除"><a href="#3-2-5-vector插入和删除" class="headerlink" title="3.2.5 vector插入和删除"></a>3.2.5 vector插入和删除</h4><p><strong>功能描述:</strong></p><ul><li>对vector容器进行插入、删除操作</li></ul><p><strong>函数原型:</strong></p><ul><li><code>push_back(ele);</code> //尾部插入元素ele</li><li><code>pop_back();</code> //删除最后一个元素</li><li><code>insert(const_iterator pos, ele);</code> //迭代器指向位置pos插入元素ele</li><li><code>insert(const_iterator pos, int count,ele);</code>//迭代器指向位置pos插入count个元素ele</li><li><code>erase(const_iterator pos);</code> //删除迭代器指向的元素</li><li><code>erase(const_iterator start, const_iterator end);</code>//删除迭代器从start到end之间的元素</li><li><code>clear();</code> //删除容器中所有元素</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs C++"><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printVector</span><span class="hljs-params">(vector<<span class="hljs-keyword">int</span>>& v)</span> </span>{<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//插入和删除</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br><span class="hljs-comment">//尾插</span><br>v1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br>v1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br><span class="hljs-built_in">printVector</span>(v1);<br><span class="hljs-comment">//尾删</span><br>v1.<span class="hljs-built_in">pop_back</span>();<br><span class="hljs-built_in">printVector</span>(v1);<br><span class="hljs-comment">//插入</span><br>v1.<span class="hljs-built_in">insert</span>(v1.<span class="hljs-built_in">begin</span>(), <span class="hljs-number">100</span>);<br><span class="hljs-built_in">printVector</span>(v1);<br><br>v1.<span class="hljs-built_in">insert</span>(v1.<span class="hljs-built_in">begin</span>(), <span class="hljs-number">2</span>, <span class="hljs-number">1000</span>);<br><span class="hljs-built_in">printVector</span>(v1);<br><br><span class="hljs-comment">//删除</span><br>v1.<span class="hljs-built_in">erase</span>(v1.<span class="hljs-built_in">begin</span>());<br><span class="hljs-built_in">printVector</span>(v1);<br><br><span class="hljs-comment">//清空</span><br>v1.<span class="hljs-built_in">erase</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>());<br>v1.<span class="hljs-built_in">clear</span>();<br><span class="hljs-built_in">printVector</span>(v1);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>尾插 — push_back</li><li>尾删 — pop_back</li><li>插入 — insert (位置迭代器)</li><li>删除 — erase (位置迭代器)</li><li>清空 — clear </li></ul><h4 id="3-2-6-vector数据存取"><a href="#3-2-6-vector数据存取" class="headerlink" title="3.2.6 vector数据存取"></a>3.2.6 vector数据存取</h4><p><strong>功能描述:</strong></p><ul><li>对vector中的数据的存取操作</li></ul><p><strong>函数原型:</strong></p><ul><li><code>at(int idx); </code> //返回索引idx所指的数据</li><li><code>operator[]; </code> //返回索引idx所指的数据</li><li><code>front(); </code> //返回容器中第一个数据元素</li><li><code>back();</code> //返回容器中最后一个数据元素</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>>v1;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < v1.<span class="hljs-built_in">size</span>(); i++)<br>{<br>cout << v1[i] << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < v1.<span class="hljs-built_in">size</span>(); i++)<br>{<br>cout << v1.<span class="hljs-built_in">at</span>(i) << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br>cout << <span class="hljs-string">"v1的第一个元素为: "</span> << v1.<span class="hljs-built_in">front</span>() << endl;<br>cout << <span class="hljs-string">"v1的最后一个元素为: "</span> << v1.<span class="hljs-built_in">back</span>() << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>除了用迭代器获取vector容器中元素,[ ]和at也可以</li><li>front返回容器第一个元素</li><li>back返回容器最后一个元素</li></ul><h4 id="3-2-7-vector互换容器"><a href="#3-2-7-vector互换容器" class="headerlink" title="3.2.7 vector互换容器"></a>3.2.7 vector互换容器</h4><p><strong>功能描述:</strong></p><ul><li>实现两个容器内元素进行互换</li></ul><p><strong>函数原型:</strong></p><ul><li><code>swap(vec);</code> // 将vec与本身的元素互换</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printVector</span><span class="hljs-params">(vector<<span class="hljs-keyword">int</span>>& v)</span> </span>{<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>>v1;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printVector</span>(v1);<br><br>vector<<span class="hljs-keyword">int</span>>v2;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">10</span>; i > <span class="hljs-number">0</span>; i--)<br>{<br>v2.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printVector</span>(v2);<br><br><span class="hljs-comment">//互换容器</span><br>cout << <span class="hljs-string">"互换后"</span> << endl;<br>v1.<span class="hljs-built_in">swap</span>(v2);<br><span class="hljs-built_in">printVector</span>(v1);<br><span class="hljs-built_in">printVector</span>(v2);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">100000</span>; i++) {<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br><br>cout << <span class="hljs-string">"v的容量为:"</span> << v.<span class="hljs-built_in">capacity</span>() << endl;<br>cout << <span class="hljs-string">"v的大小为:"</span> << v.<span class="hljs-built_in">size</span>() << endl;<br><br>v.<span class="hljs-built_in">resize</span>(<span class="hljs-number">3</span>);<br><br>cout << <span class="hljs-string">"v的容量为:"</span> << v.<span class="hljs-built_in">capacity</span>() << endl;<br>cout << <span class="hljs-string">"v的大小为:"</span> << v.<span class="hljs-built_in">size</span>() << endl;<br><br><span class="hljs-comment">//收缩内存</span><br>vector<<span class="hljs-keyword">int</span>>(v).<span class="hljs-built_in">swap</span>(v); <span class="hljs-comment">//匿名对象</span><br><br>cout << <span class="hljs-string">"v的容量为:"</span> << v.<span class="hljs-built_in">capacity</span>() << endl;<br>cout << <span class="hljs-string">"v的大小为:"</span> << v.<span class="hljs-built_in">size</span>() << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><br></code></pre></td></tr></table></figure><p>总结:swap可以使两个容器互换,可以达到实用的收缩内存效果</p><h4 id="3-2-8-vector预留空间"><a href="#3-2-8-vector预留空间" class="headerlink" title="3.2.8 vector预留空间"></a>3.2.8 vector预留空间</h4><p><strong>功能描述:</strong></p><ul><li>减少vector在动态扩展容量时的扩展次数</li></ul><p><strong>函数原型:</strong></p><ul><li><code>reserve(int len);</code>//容器预留len个元素长度,预留位置不初始化,元素不可访问。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br><br><span class="hljs-comment">//预留空间</span><br>v.<span class="hljs-built_in">reserve</span>(<span class="hljs-number">100000</span>);<br><br><span class="hljs-keyword">int</span> num = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">int</span>* p = <span class="hljs-literal">NULL</span>;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">100000</span>; i++) {<br>v.<span class="hljs-built_in">push_back</span>(i);<br><span class="hljs-keyword">if</span> (p != &v[<span class="hljs-number">0</span>]) {<br>p = &v[<span class="hljs-number">0</span>];<br>num++;<br>}<br>}<br><br>cout << <span class="hljs-string">"num:"</span> << num << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br> <br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:如果数据量较大,可以一开始利用reserve预留空间</p><h3 id="3-3-deque容器"><a href="#3-3-deque容器" class="headerlink" title="3.3 deque容器"></a>3.3 deque容器</h3><h4 id="3-3-1-deque容器基本概念"><a href="#3-3-1-deque容器基本概念" class="headerlink" title="3.3.1 deque容器基本概念"></a>3.3.1 deque容器基本概念</h4><p><strong>功能:</strong></p><ul><li>双端数组,可以对头端进行插入删除操作</li></ul><p><strong>deque与vector区别:</strong></p><ul><li>vector对于头部的插入删除效率低,数据量越大,效率越低</li><li>deque相对而言,对头部的插入删除速度回比vector快</li><li>vector访问元素时的速度会比deque快,这和两者内部实现有关</li></ul><p><img src="assets/clip_image002-1547547642923.jpg" alt="说明: 2015-11-19_204101"></p><p>deque内部工作原理:</p><p>deque内部有个<strong>中控器</strong>,维护每段缓冲区中的内容,缓冲区中存放真实数据</p><p>中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间</p><p><img src="assets/clip_image002-1547547896341.jpg" alt="clip_image002-1547547896341"></p><ul><li>deque容器的迭代器也是支持随机访问的</li></ul><h4 id="3-3-2-deque构造函数"><a href="#3-3-2-deque构造函数" class="headerlink" title="3.3.2 deque构造函数"></a>3.3.2 deque构造函数</h4><p><strong>功能描述:</strong></p><ul><li>deque容器构造</li></ul><p><strong>函数原型:</strong></p><ul><li><code>deque<T></code> deqT; //默认构造形式</li><li><code>deque(beg, end);</code> //构造函数将[beg, end)区间中的元素拷贝给本身。</li><li><code>deque(n, elem);</code> //构造函数将n个elem拷贝给本身。</li><li><code>deque(const deque &deq);</code> //拷贝构造函数</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><deque></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printDeque</span><span class="hljs-params">(<span class="hljs-keyword">const</span> deque<<span class="hljs-keyword">int</span>>& d)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::const_iterator it = d.<span class="hljs-built_in">begin</span>(); it != d.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br><br>}<br>cout << endl;<br>}<br><span class="hljs-comment">//deque构造</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>deque<<span class="hljs-keyword">int</span>> d1; <span class="hljs-comment">//无参构造函数</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>d1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printDeque</span>(d1);<br><span class="hljs-function">deque<<span class="hljs-keyword">int</span>> <span class="hljs-title">d2</span><span class="hljs-params">(d1.begin(),d1.end())</span></span>;<br><span class="hljs-built_in">printDeque</span>(d2);<br><br>deque<<span class="hljs-keyword">int</span>><span class="hljs-built_in">d3</span>(<span class="hljs-number">10</span>,<span class="hljs-number">100</span>);<br><span class="hljs-built_in">printDeque</span>(d3);<br><br>deque<<span class="hljs-keyword">int</span>>d4 = d3;<br><span class="hljs-built_in">printDeque</span>(d4);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>deque容器和vector容器的构造方式几乎一致,灵活使用即可</p><h4 id="3-3-3-deque赋值操作"><a href="#3-3-3-deque赋值操作" class="headerlink" title="3.3.3 deque赋值操作"></a>3.3.3 deque赋值操作</h4><p><strong>功能描述:</strong></p><ul><li>给deque容器进行赋值</li></ul><p><strong>函数原型:</strong></p><ul><li><code>deque& operator=(const deque &deq); </code> //重载等号操作符</li></ul><ul><li><code>assign(beg, end);</code> //将[beg, end)区间中的数据拷贝赋值给本身。</li><li><code>assign(n, elem);</code> //将n个elem拷贝赋值给本身。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><deque></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printDeque</span><span class="hljs-params">(<span class="hljs-keyword">const</span> deque<<span class="hljs-keyword">int</span>>& d)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::const_iterator it = d.<span class="hljs-built_in">begin</span>(); it != d.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br><br>}<br>cout << endl;<br>}<br><span class="hljs-comment">//赋值操作</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>deque<<span class="hljs-keyword">int</span>> d1;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>d1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printDeque</span>(d1);<br><br>deque<<span class="hljs-keyword">int</span>>d2;<br>d2 = d1;<br><span class="hljs-built_in">printDeque</span>(d2);<br><br>deque<<span class="hljs-keyword">int</span>>d3;<br>d3.<span class="hljs-built_in">assign</span>(d1.<span class="hljs-built_in">begin</span>(), d1.<span class="hljs-built_in">end</span>());<br><span class="hljs-built_in">printDeque</span>(d3);<br><br>deque<<span class="hljs-keyword">int</span>>d4;<br>d4.<span class="hljs-built_in">assign</span>(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>);<br><span class="hljs-built_in">printDeque</span>(d4);<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:deque赋值操作也与vector相同,需熟练掌握</p><h4 id="3-3-4-deque大小操作"><a href="#3-3-4-deque大小操作" class="headerlink" title="3.3.4 deque大小操作"></a>3.3.4 deque大小操作</h4><p><strong>功能描述:</strong></p><ul><li>对deque容器的大小进行操作</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>deque.empty();</code> //判断容器是否为空</p></li><li><p><code>deque.size();</code> //返回容器中元素的个数</p></li><li><p><code>deque.resize(num);</code> //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。</p><p> //如果容器变短,则末尾超出容器长度的元素被删除。</p></li><li><p><code>deque.resize(num, elem);</code> //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。</p><p> //如果容器变短,则末尾超出容器长度的元素被删除。</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><deque></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printDeque</span><span class="hljs-params">(<span class="hljs-keyword">const</span> deque<<span class="hljs-keyword">int</span>>& d)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::const_iterator it = d.<span class="hljs-built_in">begin</span>(); it != d.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br><br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//大小操作</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>deque<<span class="hljs-keyword">int</span>> d1;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>d1.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-built_in">printDeque</span>(d1);<br><br><span class="hljs-comment">//判断容器是否为空</span><br><span class="hljs-keyword">if</span> (d1.<span class="hljs-built_in">empty</span>()) {<br>cout << <span class="hljs-string">"d1为空!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> {<br>cout << <span class="hljs-string">"d1不为空!"</span> << endl;<br><span class="hljs-comment">//统计大小</span><br>cout << <span class="hljs-string">"d1的大小为:"</span> << d1.<span class="hljs-built_in">size</span>() << endl;<br>}<br><br><span class="hljs-comment">//重新指定大小</span><br>d1.<span class="hljs-built_in">resize</span>(<span class="hljs-number">15</span>, <span class="hljs-number">1</span>);<br><span class="hljs-built_in">printDeque</span>(d1);<br><br>d1.<span class="hljs-built_in">resize</span>(<span class="hljs-number">5</span>);<br><span class="hljs-built_in">printDeque</span>(d1);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>deque没有容量的概念</li><li>判断是否为空 — empty</li><li>返回元素个数 — size</li><li>重新指定个数 — resize</li></ul><h4 id="3-3-5-deque-插入和删除"><a href="#3-3-5-deque-插入和删除" class="headerlink" title="3.3.5 deque 插入和删除"></a>3.3.5 deque 插入和删除</h4><p><strong>功能描述:</strong></p><ul><li>向deque容器中插入和删除数据</li></ul><p><strong>函数原型:</strong></p><p>两端插入操作:</p><ul><li><code>push_back(elem);</code> //在容器尾部添加一个数据</li><li><code>push_front(elem);</code> //在容器头部插入一个数据</li><li><code>pop_back();</code> //删除容器最后一个数据</li><li><code>pop_front();</code> //删除容器第一个数据</li></ul><p>指定位置操作:</p><ul><li><p><code>insert(pos,elem);</code> //在pos位置插入一个elem元素的拷贝,返回新数据的位置。</p></li><li><p><code>insert(pos,n,elem);</code> //在pos位置插入n个elem数据,无返回值。</p></li><li><p><code>insert(pos,beg,end);</code> //在pos位置插入[beg,end)区间的数据,无返回值。</p></li><li><p><code>clear();</code> //清空容器的所有数据</p></li><li><p><code>erase(beg,end);</code> //删除[beg,end)区间的数据,返回下一个数据的位置。</p></li><li><p><code>erase(pos);</code> //删除pos位置的数据,返回下一个数据的位置。</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><deque></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printDeque</span><span class="hljs-params">(<span class="hljs-keyword">const</span> deque<<span class="hljs-keyword">int</span>>& d)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::const_iterator it = d.<span class="hljs-built_in">begin</span>(); it != d.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br><br>}<br>cout << endl;<br>}<br><span class="hljs-comment">//两端操作</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>deque<<span class="hljs-keyword">int</span>> d;<br><span class="hljs-comment">//尾插</span><br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br><span class="hljs-comment">//头插</span><br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">100</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">200</span>);<br><br><span class="hljs-built_in">printDeque</span>(d);<br><br><span class="hljs-comment">//尾删</span><br>d.<span class="hljs-built_in">pop_back</span>();<br><span class="hljs-comment">//头删</span><br>d.<span class="hljs-built_in">pop_front</span>();<br><span class="hljs-built_in">printDeque</span>(d);<br>}<br><br><span class="hljs-comment">//插入</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>deque<<span class="hljs-keyword">int</span>> d;<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">100</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">200</span>);<br><span class="hljs-built_in">printDeque</span>(d);<br><br>d.<span class="hljs-built_in">insert</span>(d.<span class="hljs-built_in">begin</span>(), <span class="hljs-number">1000</span>);<br><span class="hljs-built_in">printDeque</span>(d);<br><br>d.<span class="hljs-built_in">insert</span>(d.<span class="hljs-built_in">begin</span>(), <span class="hljs-number">2</span>,<span class="hljs-number">10000</span>);<br><span class="hljs-built_in">printDeque</span>(d);<br><br>deque<<span class="hljs-keyword">int</span>>d2;<br>d2.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">1</span>);<br>d2.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">2</span>);<br>d2.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">3</span>);<br><br>d.<span class="hljs-built_in">insert</span>(d.<span class="hljs-built_in">begin</span>(), d2.<span class="hljs-built_in">begin</span>(), d2.<span class="hljs-built_in">end</span>());<br><span class="hljs-built_in">printDeque</span>(d);<br><br>}<br><br><span class="hljs-comment">//删除</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test03</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>deque<<span class="hljs-keyword">int</span>> d;<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">100</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">200</span>);<br><span class="hljs-built_in">printDeque</span>(d);<br><br>d.<span class="hljs-built_in">erase</span>(d.<span class="hljs-built_in">begin</span>());<br><span class="hljs-built_in">printDeque</span>(d);<br><br>d.<span class="hljs-built_in">erase</span>(d.<span class="hljs-built_in">begin</span>(), d.<span class="hljs-built_in">end</span>());<br>d.<span class="hljs-built_in">clear</span>();<br><span class="hljs-built_in">printDeque</span>(d);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-comment">//test02();</span><br><br> <span class="hljs-built_in">test03</span>();<br> <br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>插入和删除提供的位置是迭代器!</li><li>尾插 — push_back</li><li>尾删 — pop_back</li><li>头插 — push_front</li><li>头删 — pop_front</li></ul><h4 id="3-3-6-deque-数据存取"><a href="#3-3-6-deque-数据存取" class="headerlink" title="3.3.6 deque 数据存取"></a>3.3.6 deque 数据存取</h4><p><strong>功能描述:</strong></p><ul><li>对deque 中的数据的存取操作</li></ul><p><strong>函数原型:</strong></p><ul><li><code>at(int idx); </code> //返回索引idx所指的数据</li><li><code>operator[]; </code> //返回索引idx所指的数据</li><li><code>front(); </code> //返回容器中第一个数据元素</li><li><code>back();</code> //返回容器中最后一个数据元素</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><deque></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printDeque</span><span class="hljs-params">(<span class="hljs-keyword">const</span> deque<<span class="hljs-keyword">int</span>>& d)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::const_iterator it = d.<span class="hljs-built_in">begin</span>(); it != d.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br><br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//数据存取</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>deque<<span class="hljs-keyword">int</span>> d;<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">100</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">200</span>);<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < d.<span class="hljs-built_in">size</span>(); i++) {<br>cout << d[i] << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < d.<span class="hljs-built_in">size</span>(); i++) {<br>cout << d.<span class="hljs-built_in">at</span>(i) << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br>cout << <span class="hljs-string">"front:"</span> << d.<span class="hljs-built_in">front</span>() << endl;<br><br>cout << <span class="hljs-string">"back:"</span> << d.<span class="hljs-built_in">back</span>() << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>除了用迭代器获取deque容器中元素,[ ]和at也可以</li><li>front返回容器第一个元素</li><li>back返回容器最后一个元素</li></ul><h4 id="3-3-7-deque-排序"><a href="#3-3-7-deque-排序" class="headerlink" title="3.3.7 deque 排序"></a>3.3.7 deque 排序</h4><p><strong>功能描述:</strong></p><ul><li>利用算法实现对deque容器进行排序</li></ul><p><strong>算法:</strong></p><ul><li><code>sort(iterator beg, iterator end)</code> //对beg和end区间内元素进行排序</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><deque></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printDeque</span><span class="hljs-params">(<span class="hljs-keyword">const</span> deque<<span class="hljs-keyword">int</span>>& d)</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::const_iterator it = d.<span class="hljs-built_in">begin</span>(); it != d.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br><br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>deque<<span class="hljs-keyword">int</span>> d;<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>d.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">100</span>);<br>d.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">200</span>);<br><br><span class="hljs-built_in">printDeque</span>(d);<br><span class="hljs-built_in">sort</span>(d.<span class="hljs-built_in">begin</span>(), d.<span class="hljs-built_in">end</span>());<br><span class="hljs-built_in">printDeque</span>(d);<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:sort算法非常实用,使用时包含头文件 algorithm即可</p><h3 id="3-4-案例-评委打分"><a href="#3-4-案例-评委打分" class="headerlink" title="3.4 案例-评委打分"></a>3.4 案例-评委打分</h3><h4 id="3-4-1-案例描述"><a href="#3-4-1-案例描述" class="headerlink" title="3.4.1 案例描述"></a>3.4.1 案例描述</h4><p>有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。</p><h4 id="3-4-2-实现步骤"><a href="#3-4-2-实现步骤" class="headerlink" title="3.4.2 实现步骤"></a>3.4.2 实现步骤</h4><ol><li>创建五名选手,放到vector中</li><li>遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中</li><li>sort算法对deque容器中分数排序,去除最高和最低分</li><li>deque容器遍历一遍,累加总分</li><li>获取平均分</li></ol><p><strong>示例代码:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-comment">//选手类</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> score)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Score = score;<br>}<br><br>string m_Name; <span class="hljs-comment">//姓名</span><br><span class="hljs-keyword">int</span> m_Score; <span class="hljs-comment">//平均分</span><br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">createPerson</span><span class="hljs-params">(vector<Person>&v)</span></span><br><span class="hljs-function"></span>{<br>string nameSeed = <span class="hljs-string">"ABCDE"</span>;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">5</span>; i++)<br>{<br>string name = <span class="hljs-string">"选手"</span>;<br>name += nameSeed[i];<br><br><span class="hljs-keyword">int</span> score = <span class="hljs-number">0</span>;<br><br><span class="hljs-function">Person <span class="hljs-title">p</span><span class="hljs-params">(name, score)</span></span>;<br><br><span class="hljs-comment">//将创建的person对象 放入到容器中</span><br>v.<span class="hljs-built_in">push_back</span>(p);<br>}<br>}<br><br><span class="hljs-comment">//打分</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">setScore</span><span class="hljs-params">(vector<Person>&v)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (vector<Person>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++)<br>{<br><span class="hljs-comment">//将评委的分数 放入到deque容器中</span><br>deque<<span class="hljs-keyword">int</span>>d;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br><span class="hljs-keyword">int</span> score = <span class="hljs-built_in">rand</span>() % <span class="hljs-number">41</span> + <span class="hljs-number">60</span>; <span class="hljs-comment">// 60 ~ 100</span><br>d.<span class="hljs-built_in">push_back</span>(score);<br>}<br><br><span class="hljs-comment">//cout << "选手: " << it->m_Name << " 打分: " << endl;</span><br><span class="hljs-comment">//for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)</span><br><span class="hljs-comment">//{</span><br><span class="hljs-comment">//cout << *dit << " ";</span><br><span class="hljs-comment">//}</span><br><span class="hljs-comment">//cout << endl;</span><br><br><span class="hljs-comment">//排序</span><br><span class="hljs-built_in">sort</span>(d.<span class="hljs-built_in">begin</span>(), d.<span class="hljs-built_in">end</span>());<br><br><span class="hljs-comment">//去除最高和最低分</span><br>d.<span class="hljs-built_in">pop_back</span>();<br>d.<span class="hljs-built_in">pop_front</span>();<br><br><span class="hljs-comment">//取平均分</span><br><span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">for</span> (deque<<span class="hljs-keyword">int</span>>::iterator dit = d.<span class="hljs-built_in">begin</span>(); dit != d.<span class="hljs-built_in">end</span>(); dit++)<br>{<br>sum += *dit; <span class="hljs-comment">//累加每个评委的分数</span><br>}<br><br><span class="hljs-keyword">int</span> avg = sum / d.<span class="hljs-built_in">size</span>();<br><br><span class="hljs-comment">//将平均分 赋值给选手身上</span><br>it->m_Score = avg;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showScore</span><span class="hljs-params">(vector<Person>&v)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (vector<Person>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << <span class="hljs-string">"姓名: "</span> << it->m_Name << <span class="hljs-string">" 平均分: "</span> << it->m_Score << endl;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//随机数种子</span><br><span class="hljs-built_in">srand</span>((<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span>)<span class="hljs-built_in">time</span>(<span class="hljs-literal">NULL</span>));<br><br><span class="hljs-comment">//1、创建5名选手</span><br>vector<Person>v; <span class="hljs-comment">//存放选手容器</span><br><span class="hljs-built_in">createPerson</span>(v);<br><br><span class="hljs-comment">//测试</span><br><span class="hljs-comment">//for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)</span><br><span class="hljs-comment">//{</span><br><span class="hljs-comment">//cout << "姓名: " << (*it).m_Name << " 分数: " << (*it).m_Score << endl;</span><br><span class="hljs-comment">//}</span><br><br><span class="hljs-comment">//2、给5名选手打分</span><br><span class="hljs-built_in">setScore</span>(v);<br><br><span class="hljs-comment">//3、显示最后得分</span><br><span class="hljs-built_in">showScore</span>(v);<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong> 选取不同的容器操作数据,可以提升代码的效率</p><h3 id="3-5-stack容器"><a href="#3-5-stack容器" class="headerlink" title="3.5 stack容器"></a>3.5 stack容器</h3><h4 id="3-5-1-stack-基本概念"><a href="#3-5-1-stack-基本概念" class="headerlink" title="3.5.1 stack 基本概念"></a>3.5.1 stack 基本概念</h4><p><strong>概念:</strong>stack是一种<strong>先进后出</strong>(First In Last Out,FILO)的数据结构,它只有一个出口</p><p><img src="assets/clip_image002-1547604555425.jpg" alt="说明: 2015-11-15_195707"></p><p>栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为</p><p>栈中进入数据称为 — <strong>入栈</strong> <code>push</code></p><p>栈中弹出数据称为 — <strong>出栈</strong> <code>pop</code></p><p>生活中的栈:</p><p><img src="assets/clip_image002.png" alt="img"></p><p><img src="assets/clip_image002-1547605111510.jpg" alt="img"></p><h4 id="3-5-2-stack-常用接口"><a href="#3-5-2-stack-常用接口" class="headerlink" title="3.5.2 stack 常用接口"></a>3.5.2 stack 常用接口</h4><p>功能描述:栈容器常用的对外接口</p><p>构造函数:</p><ul><li><code>stack<T> stk;</code> //stack采用模板类实现, stack对象的默认构造形式</li><li><code>stack(const stack &stk);</code> //拷贝构造函数</li></ul><p>赋值操作:</p><ul><li><code>stack& operator=(const stack &stk);</code> //重载等号操作符</li></ul><p>数据存取:</p><ul><li><code>push(elem);</code> //向栈顶添加元素</li><li><code>pop();</code> //从栈顶移除第一个元素</li><li><code>top(); </code> //返回栈顶元素</li></ul><p>大小操作:</p><ul><li><code>empty();</code> //判断堆栈是否为空</li><li><code>size(); </code> //返回栈的大小</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stack></span></span><br><br><span class="hljs-comment">//栈容器常用接口</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//创建栈容器 栈容器必须符合先进后出</span><br>stack<<span class="hljs-keyword">int</span>> s;<br><br><span class="hljs-comment">//向栈中添加元素,叫做 压栈 入栈</span><br>s.<span class="hljs-built_in">push</span>(<span class="hljs-number">10</span>);<br>s.<span class="hljs-built_in">push</span>(<span class="hljs-number">20</span>);<br>s.<span class="hljs-built_in">push</span>(<span class="hljs-number">30</span>);<br><br><span class="hljs-keyword">while</span> (!s.<span class="hljs-built_in">empty</span>()) {<br><span class="hljs-comment">//输出栈顶元素</span><br>cout << <span class="hljs-string">"栈顶元素为: "</span> << s.<span class="hljs-built_in">top</span>() << endl;<br><span class="hljs-comment">//弹出栈顶元素</span><br>s.<span class="hljs-built_in">pop</span>();<br>}<br>cout << <span class="hljs-string">"栈的大小为:"</span> << s.<span class="hljs-built_in">size</span>() << endl;<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>入栈 — push</li><li>出栈 — pop</li><li>返回栈顶 — top</li><li>判断栈是否为空 — empty</li><li>返回栈大小 — size</li></ul><h3 id="3-6-queue-容器"><a href="#3-6-queue-容器" class="headerlink" title="3.6 queue 容器"></a>3.6 queue 容器</h3><h4 id="3-6-1-queue-基本概念"><a href="#3-6-1-queue-基本概念" class="headerlink" title="3.6.1 queue 基本概念"></a>3.6.1 queue 基本概念</h4><p><strong>概念:</strong>Queue是一种<strong>先进先出</strong>(First In First Out,FIFO)的数据结构,它有两个出口</p><p><img src="assets/clip_image002-1547606475892.jpg" alt="说明: 2015-11-15_214429"></p><p>队列容器允许从一端新增元素,从另一端移除元素</p><p>队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为</p><p>队列中进数据称为 — <strong>入队</strong> <code>push</code></p><p>队列中出数据称为 — <strong>出队</strong> <code>pop</code></p><p>生活中的队列:</p><p><img src="assets/1547606785041.png" alt="1547606785041"></p><h4 id="3-6-2-queue-常用接口"><a href="#3-6-2-queue-常用接口" class="headerlink" title="3.6.2 queue 常用接口"></a>3.6.2 queue 常用接口</h4><p>功能描述:栈容器常用的对外接口</p><p>构造函数:</p><ul><li><code>queue<T> que;</code> //queue采用模板类实现,queue对象的默认构造形式</li><li><code>queue(const queue &que);</code> //拷贝构造函数</li></ul><p>赋值操作:</p><ul><li><code>queue& operator=(const queue &que);</code> //重载等号操作符</li></ul><p>数据存取:</p><ul><li><code>push(elem);</code> //往队尾添加元素</li><li><code>pop();</code> //从队头移除第一个元素</li><li><code>back();</code> //返回最后一个元素</li><li><code>front(); </code> //返回第一个元素</li></ul><p>大小操作:</p><ul><li><code>empty();</code> //判断堆栈是否为空</li><li><code>size(); </code> //返回栈的大小</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><queue></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//创建队列</span><br>queue<Person> q;<br><br><span class="hljs-comment">//准备数据</span><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"唐僧"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"孙悟空"</span>, <span class="hljs-number">1000</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"猪八戒"</span>, <span class="hljs-number">900</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"沙僧"</span>, <span class="hljs-number">800</span>)</span></span>;<br><br><span class="hljs-comment">//向队列中添加元素 入队操作</span><br>q.<span class="hljs-built_in">push</span>(p1);<br>q.<span class="hljs-built_in">push</span>(p2);<br>q.<span class="hljs-built_in">push</span>(p3);<br>q.<span class="hljs-built_in">push</span>(p4);<br><br><span class="hljs-comment">//队列不提供迭代器,更不支持随机访问</span><br><span class="hljs-keyword">while</span> (!q.<span class="hljs-built_in">empty</span>()) {<br><span class="hljs-comment">//输出队头元素</span><br>cout << <span class="hljs-string">"队头元素-- 姓名: "</span> << q.<span class="hljs-built_in">front</span>().m_Name <br> << <span class="hljs-string">" 年龄: "</span><< q.<span class="hljs-built_in">front</span>().m_Age << endl;<br> <br>cout << <span class="hljs-string">"队尾元素-- 姓名: "</span> << q.<span class="hljs-built_in">back</span>().m_Name <br> << <span class="hljs-string">" 年龄: "</span> << q.<span class="hljs-built_in">back</span>().m_Age << endl;<br> <br>cout << endl;<br><span class="hljs-comment">//弹出队头元素</span><br>q.<span class="hljs-built_in">pop</span>();<br>}<br><br>cout << <span class="hljs-string">"队列大小为:"</span> << q.<span class="hljs-built_in">size</span>() << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>入队 — push</li><li>出队 — pop</li><li>返回队头元素 — front</li><li>返回队尾元素 — back</li><li>判断队是否为空 — empty</li><li>返回队列大小 — size</li></ul><h3 id="3-7-list容器"><a href="#3-7-list容器" class="headerlink" title="3.7 list容器"></a>3.7 list容器</h3><h4 id="3-7-1-list基本概念"><a href="#3-7-1-list基本概念" class="headerlink" title="3.7.1 list基本概念"></a>3.7.1 list基本概念</h4><p><strong>功能:</strong>将数据进行链式存储</p><p><strong>链表</strong>(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的</p><p>链表的组成:链表由一系列<strong>结点</strong>组成</p><p>结点的组成:一个是存储数据元素的<strong>数据域</strong>,另一个是存储下一个结点地址的<strong>指针域</strong></p><p>STL中的链表是一个双向循环链表</p><p><img src="assets/clip_image002-1547608564071.jpg" alt="说明: 2015-11-15_225145"></p><p>由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于<strong>双向迭代器</strong></p><p>list的优点:</p><ul><li>采用动态存储分配,不会造成内存浪费和溢出</li><li>链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素</li></ul><p>list的缺点:</p><ul><li>链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大</li></ul><p>List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。</p><p>总结:STL中<strong>List和vector是两个最常被使用的容器</strong>,各有优缺点</p><h4 id="3-7-2-list构造函数"><a href="#3-7-2-list构造函数" class="headerlink" title="3.7.2 list构造函数"></a>3.7.2 list构造函数</h4><p><strong>功能描述:</strong></p><ul><li>创建list容器</li></ul><p><strong>函数原型:</strong></p><ul><li><code>list<T> lst;</code> //list采用采用模板类实现,对象的默认构造形式:</li><li><code>list(beg,end);</code> //构造函数将[beg, end)区间中的元素拷贝给本身。</li><li><code>list(n,elem);</code> //构造函数将n个elem拷贝给本身。</li><li><code>list(const list &lst);</code> //拷贝构造函数。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><list></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">const</span> list<<span class="hljs-keyword">int</span>>& L)</span> </span>{<br><br><span class="hljs-keyword">for</span> (list<<span class="hljs-keyword">int</span>>::const_iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>list<<span class="hljs-keyword">int</span>>L1;<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br><span class="hljs-built_in">printList</span>(L1);<br><br>list<<span class="hljs-keyword">int</span>><span class="hljs-built_in">L2</span>(L1.<span class="hljs-built_in">begin</span>(),L1.<span class="hljs-built_in">end</span>());<br><span class="hljs-built_in">printList</span>(L2);<br><br>list<<span class="hljs-keyword">int</span>><span class="hljs-built_in">L3</span>(L2);<br><span class="hljs-built_in">printList</span>(L3);<br><br>list<<span class="hljs-keyword">int</span>><span class="hljs-built_in">L4</span>(<span class="hljs-number">10</span>, <span class="hljs-number">1000</span>);<br><span class="hljs-built_in">printList</span>(L4);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:list构造方式同其他几个STL常用容器,熟练掌握即可</p><h4 id="3-7-3-list-赋值和交换"><a href="#3-7-3-list-赋值和交换" class="headerlink" title="3.7.3 list 赋值和交换"></a>3.7.3 list 赋值和交换</h4><p><strong>功能描述:</strong></p><ul><li>给list容器进行赋值,以及交换list容器</li></ul><p><strong>函数原型:</strong></p><ul><li><code>assign(beg, end);</code> //将[beg, end)区间中的数据拷贝赋值给本身。</li><li><code>assign(n, elem);</code> //将n个elem拷贝赋值给本身。</li><li><code>list& operator=(const list &lst);</code> //重载等号操作符</li><li><code>swap(lst);</code> //将lst与本身的元素互换。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><list></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">const</span> list<<span class="hljs-keyword">int</span>>& L)</span> </span>{<br><br><span class="hljs-keyword">for</span> (list<<span class="hljs-keyword">int</span>>::const_iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//赋值和交换</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>list<<span class="hljs-keyword">int</span>>L1;<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><span class="hljs-built_in">printList</span>(L1);<br><br><span class="hljs-comment">//赋值</span><br>list<<span class="hljs-keyword">int</span>>L2;<br>L2 = L1;<br><span class="hljs-built_in">printList</span>(L2);<br><br>list<<span class="hljs-keyword">int</span>>L3;<br>L3.<span class="hljs-built_in">assign</span>(L2.<span class="hljs-built_in">begin</span>(), L2.<span class="hljs-built_in">end</span>());<br><span class="hljs-built_in">printList</span>(L3);<br><br>list<<span class="hljs-keyword">int</span>>L4;<br>L4.<span class="hljs-built_in">assign</span>(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>);<br><span class="hljs-built_in">printList</span>(L4);<br><br>}<br><br><span class="hljs-comment">//交换</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>list<<span class="hljs-keyword">int</span>>L1;<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br>list<<span class="hljs-keyword">int</span>>L2;<br>L2.<span class="hljs-built_in">assign</span>(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>);<br><br>cout << <span class="hljs-string">"交换前: "</span> << endl;<br><span class="hljs-built_in">printList</span>(L1);<br><span class="hljs-built_in">printList</span>(L2);<br><br>cout << endl;<br><br>L1.<span class="hljs-built_in">swap</span>(L2);<br><br>cout << <span class="hljs-string">"交换后: "</span> << endl;<br><span class="hljs-built_in">printList</span>(L1);<br><span class="hljs-built_in">printList</span>(L2);<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:list赋值和交换操作能够灵活运用即可</p><h4 id="3-7-4-list-大小操作"><a href="#3-7-4-list-大小操作" class="headerlink" title="3.7.4 list 大小操作"></a>3.7.4 list 大小操作</h4><p><strong>功能描述:</strong></p><ul><li>对list容器的大小进行操作</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>size(); </code> //返回容器中元素的个数</p></li><li><p><code>empty(); </code> //判断容器是否为空</p></li><li><p><code>resize(num);</code> //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。</p><p> //如果容器变短,则末尾超出容器长度的元素被删除。</p></li><li><p><code>resize(num, elem); </code> //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。</p><pre><code> //如果容器变短,则末尾超出容器长度的元素被删除。</code></pre></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><list></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">const</span> list<<span class="hljs-keyword">int</span>>& L)</span> </span>{<br><br><span class="hljs-keyword">for</span> (list<<span class="hljs-keyword">int</span>>::const_iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//大小操作</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>list<<span class="hljs-keyword">int</span>>L1;<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br><span class="hljs-keyword">if</span> (L1.<span class="hljs-built_in">empty</span>())<br>{<br>cout << <span class="hljs-string">"L1为空"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"L1不为空"</span> << endl;<br>cout << <span class="hljs-string">"L1的大小为: "</span> << L1.<span class="hljs-built_in">size</span>() << endl;<br>}<br><br><span class="hljs-comment">//重新指定大小</span><br>L1.<span class="hljs-built_in">resize</span>(<span class="hljs-number">10</span>);<br><span class="hljs-built_in">printList</span>(L1);<br><br>L1.<span class="hljs-built_in">resize</span>(<span class="hljs-number">2</span>);<br><span class="hljs-built_in">printList</span>(L1);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>判断是否为空 — empty</li><li>返回元素个数 — size</li><li>重新指定个数 — resize</li></ul><h4 id="3-7-5-list-插入和删除"><a href="#3-7-5-list-插入和删除" class="headerlink" title="3.7.5 list 插入和删除"></a>3.7.5 list 插入和删除</h4><p><strong>功能描述:</strong></p><ul><li>对list容器进行数据的插入和删除</li></ul><p><strong>函数原型:</strong></p><ul><li>push_back(elem);//在容器尾部加入一个元素</li><li>pop_back();//删除容器中最后一个元素</li><li>push_front(elem);//在容器开头插入一个元素</li><li>pop_front();//从容器开头移除第一个元素</li><li>insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。</li><li>insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。</li><li>insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。</li><li>clear();//移除容器的所有数据</li><li>erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。</li><li>erase(pos);//删除pos位置的数据,返回下一个数据的位置。</li><li>remove(elem);//删除容器中所有与elem值匹配的元素。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><list></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">const</span> list<<span class="hljs-keyword">int</span>>& L)</span> </span>{<br><br><span class="hljs-keyword">for</span> (list<<span class="hljs-keyword">int</span>>::const_iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//插入和删除</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>list<<span class="hljs-keyword">int</span>> L;<br><span class="hljs-comment">//尾插</span><br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br><span class="hljs-comment">//头插</span><br>L.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">100</span>);<br>L.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">200</span>);<br>L.<span class="hljs-built_in">push_front</span>(<span class="hljs-number">300</span>);<br><br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//尾删</span><br>L.<span class="hljs-built_in">pop_back</span>();<br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//头删</span><br>L.<span class="hljs-built_in">pop_front</span>();<br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//插入</span><br>list<<span class="hljs-keyword">int</span>>::iterator it = L.<span class="hljs-built_in">begin</span>();<br>L.<span class="hljs-built_in">insert</span>(++it, <span class="hljs-number">1000</span>);<br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//删除</span><br>it = L.<span class="hljs-built_in">begin</span>();<br>L.<span class="hljs-built_in">erase</span>(++it);<br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//移除</span><br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10000</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10000</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10000</span>);<br><span class="hljs-built_in">printList</span>(L);<br>L.<span class="hljs-built_in">remove</span>(<span class="hljs-number">10000</span>);<br><span class="hljs-built_in">printList</span>(L);<br> <br> <span class="hljs-comment">//清空</span><br>L.<span class="hljs-built_in">clear</span>();<br><span class="hljs-built_in">printList</span>(L);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>尾插 — push_back</li><li>尾删 — pop_back</li><li>头插 — push_front</li><li>头删 — pop_front</li><li>插入 — insert</li><li>删除 — erase</li><li>移除 — remove</li><li>清空 — clear</li></ul><h4 id="3-7-6-list-数据存取"><a href="#3-7-6-list-数据存取" class="headerlink" title="3.7.6 list 数据存取"></a>3.7.6 list 数据存取</h4><p><strong>功能描述:</strong></p><ul><li>对list容器中数据进行存取</li></ul><p><strong>函数原型:</strong></p><ul><li><code>front();</code> //返回第一个元素。</li><li><code>back();</code> //返回最后一个元素。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><list></span></span><br><br><span class="hljs-comment">//数据存取</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>list<<span class="hljs-keyword">int</span>>L1;<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>L1.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br><br><span class="hljs-comment">//cout << L1.at(0) << endl;//错误 不支持at访问数据</span><br><span class="hljs-comment">//cout << L1[0] << endl; //错误 不支持[]方式访问数据</span><br>cout << <span class="hljs-string">"第一个元素为: "</span> << L1.<span class="hljs-built_in">front</span>() << endl;<br>cout << <span class="hljs-string">"最后一个元素为: "</span> << L1.<span class="hljs-built_in">back</span>() << endl;<br><br><span class="hljs-comment">//list容器的迭代器是双向迭代器,不支持随机访问</span><br>list<<span class="hljs-keyword">int</span>>::iterator it = L1.<span class="hljs-built_in">begin</span>();<br><span class="hljs-comment">//it = it + 1;//错误,不可以跳跃访问,即使是+1</span><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>list容器中不可以通过[]或者at方式访问数据</li><li>返回第一个元素 — front</li><li>返回最后一个元素 — back</li></ul><h4 id="3-7-7-list-反转和排序"><a href="#3-7-7-list-反转和排序" class="headerlink" title="3.7.7 list 反转和排序"></a>3.7.7 list 反转和排序</h4><p><strong>功能描述:</strong></p><ul><li>将容器中的元素反转,以及将容器中的数据进行排序</li></ul><p><strong>函数原型:</strong></p><ul><li><code>reverse();</code> //反转链表</li><li><code>sort();</code> //链表排序</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">const</span> list<<span class="hljs-keyword">int</span>>& L)</span> </span>{<br><br><span class="hljs-keyword">for</span> (list<<span class="hljs-keyword">int</span>>::const_iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">myCompare</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val1 , <span class="hljs-keyword">int</span> val2)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> val1 > val2;<br>}<br><br><span class="hljs-comment">//反转和排序</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>list<<span class="hljs-keyword">int</span>> L;<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">90</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>L.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">70</span>);<br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//反转容器的元素</span><br>L.<span class="hljs-built_in">reverse</span>();<br><span class="hljs-built_in">printList</span>(L);<br><br><span class="hljs-comment">//排序</span><br>L.<span class="hljs-built_in">sort</span>(); <span class="hljs-comment">//默认的排序规则 从小到大</span><br><span class="hljs-built_in">printList</span>(L);<br><br>L.<span class="hljs-built_in">sort</span>(myCompare); <span class="hljs-comment">//指定规则,从大到小</span><br><span class="hljs-built_in">printList</span>(L);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>反转 — reverse</li><li>排序 — sort (成员函数)</li></ul><h4 id="3-7-8-排序案例"><a href="#3-7-8-排序案例" class="headerlink" title="3.7.8 排序案例"></a>3.7.8 排序案例</h4><p>案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高</p><p>排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><list></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age , <span class="hljs-keyword">int</span> height) {<br>m_Name = name;<br>m_Age = age;<br>m_Height = height;<br>}<br><br><span class="hljs-keyword">public</span>:<br>string m_Name; <span class="hljs-comment">//姓名</span><br><span class="hljs-keyword">int</span> m_Age; <span class="hljs-comment">//年龄</span><br><span class="hljs-keyword">int</span> m_Height; <span class="hljs-comment">//身高</span><br>};<br><br><br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">ComparePerson</span><span class="hljs-params">(Person& p1, Person& p2)</span> </span>{<br><br><span class="hljs-keyword">if</span> (p1.m_Age == p2.m_Age) {<br><span class="hljs-keyword">return</span> p1.m_Height > p2.m_Height;<br>}<br><span class="hljs-keyword">else</span><br>{<br><span class="hljs-keyword">return</span> p1.m_Age < p2.m_Age;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>list<Person> L;<br><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"刘备"</span>, <span class="hljs-number">35</span> , <span class="hljs-number">175</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"曹操"</span>, <span class="hljs-number">45</span> , <span class="hljs-number">180</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"孙权"</span>, <span class="hljs-number">40</span> , <span class="hljs-number">170</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"赵云"</span>, <span class="hljs-number">25</span> , <span class="hljs-number">190</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p5</span><span class="hljs-params">(<span class="hljs-string">"张飞"</span>, <span class="hljs-number">35</span> , <span class="hljs-number">160</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p6</span><span class="hljs-params">(<span class="hljs-string">"关羽"</span>, <span class="hljs-number">35</span> , <span class="hljs-number">200</span>)</span></span>;<br><br>L.<span class="hljs-built_in">push_back</span>(p1);<br>L.<span class="hljs-built_in">push_back</span>(p2);<br>L.<span class="hljs-built_in">push_back</span>(p3);<br>L.<span class="hljs-built_in">push_back</span>(p4);<br>L.<span class="hljs-built_in">push_back</span>(p5);<br>L.<span class="hljs-built_in">push_back</span>(p6);<br><br><span class="hljs-keyword">for</span> (list<Person>::iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << <span class="hljs-string">"姓名: "</span> << it->m_Name << <span class="hljs-string">" 年龄: "</span> << it->m_Age <br> << <span class="hljs-string">" 身高: "</span> << it->m_Height << endl;<br>}<br><br>cout << <span class="hljs-string">"---------------------------------"</span> << endl;<br>L.<span class="hljs-built_in">sort</span>(ComparePerson); <span class="hljs-comment">//排序</span><br><br><span class="hljs-keyword">for</span> (list<Person>::iterator it = L.<span class="hljs-built_in">begin</span>(); it != L.<span class="hljs-built_in">end</span>(); it++) {<br>cout << <span class="hljs-string">"姓名: "</span> << it->m_Name << <span class="hljs-string">" 年龄: "</span> << it->m_Age <br> << <span class="hljs-string">" 身高: "</span> << it->m_Height << endl;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序</li></ul><ul><li>高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂</li></ul><h3 id="3-8-set-multiset-容器"><a href="#3-8-set-multiset-容器" class="headerlink" title="3.8 set/ multiset 容器"></a>3.8 set/ multiset 容器</h3><h4 id="3-8-1-set基本概念"><a href="#3-8-1-set基本概念" class="headerlink" title="3.8.1 set基本概念"></a>3.8.1 set基本概念</h4><p><strong>简介:</strong></p><ul><li>所有元素都会在插入时自动被排序</li></ul><p><strong>本质:</strong></p><ul><li>set/multiset属于<strong>关联式容器</strong>,底层结构是用<strong>二叉树</strong>实现。</li></ul><p><strong>set和multiset区别</strong>:</p><ul><li>set不允许容器中有重复的元素</li><li>multiset允许容器中有重复的元素</li></ul><h4 id="3-8-2-set构造和赋值"><a href="#3-8-2-set构造和赋值" class="headerlink" title="3.8.2 set构造和赋值"></a>3.8.2 set构造和赋值</h4><p>功能描述:创建set容器以及赋值</p><p>构造:</p><ul><li><code>set<T> st;</code> //默认构造函数:</li><li><code>set(const set &st);</code> //拷贝构造函数</li></ul><p>赋值:</p><ul><li><code>set& operator=(const set &st);</code> //重载等号操作符</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printSet</span><span class="hljs-params">(set<<span class="hljs-keyword">int</span>> & s)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (set<<span class="hljs-keyword">int</span>>::iterator it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//构造和赋值</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>set<<span class="hljs-keyword">int</span>> s1;<br><br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br><span class="hljs-built_in">printSet</span>(s1);<br><br><span class="hljs-comment">//拷贝构造</span><br>set<<span class="hljs-keyword">int</span>><span class="hljs-built_in">s2</span>(s1);<br><span class="hljs-built_in">printSet</span>(s2);<br><br><span class="hljs-comment">//赋值</span><br>set<<span class="hljs-keyword">int</span>>s3;<br>s3 = s2;<br><span class="hljs-built_in">printSet</span>(s3);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>set容器插入数据时用insert</li><li>set容器插入数据的数据会自动排序</li></ul><h4 id="3-8-3-set大小和交换"><a href="#3-8-3-set大小和交换" class="headerlink" title="3.8.3 set大小和交换"></a>3.8.3 set大小和交换</h4><p><strong>功能描述:</strong></p><ul><li>统计set容器大小以及交换set容器</li></ul><p><strong>函数原型:</strong></p><ul><li><code>size();</code> //返回容器中元素的数目</li><li><code>empty();</code> //判断容器是否为空</li><li><code>swap(st);</code> //交换两个集合容器</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printSet</span><span class="hljs-params">(set<<span class="hljs-keyword">int</span>> & s)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (set<<span class="hljs-keyword">int</span>>::iterator it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//大小</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>set<<span class="hljs-keyword">int</span>> s1;<br><br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br><br><span class="hljs-keyword">if</span> (s1.<span class="hljs-built_in">empty</span>())<br>{<br>cout << <span class="hljs-string">"s1为空"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"s1不为空"</span> << endl;<br>cout << <span class="hljs-string">"s1的大小为: "</span> << s1.<span class="hljs-built_in">size</span>() << endl;<br>}<br><br>}<br><br><span class="hljs-comment">//交换</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>set<<span class="hljs-keyword">int</span>> s1;<br><br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br><br>set<<span class="hljs-keyword">int</span>> s2;<br><br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">100</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">300</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">200</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">400</span>);<br><br>cout << <span class="hljs-string">"交换前"</span> << endl;<br><span class="hljs-built_in">printSet</span>(s1);<br><span class="hljs-built_in">printSet</span>(s2);<br>cout << endl;<br><br>cout << <span class="hljs-string">"交换后"</span> << endl;<br>s1.<span class="hljs-built_in">swap</span>(s2);<br><span class="hljs-built_in">printSet</span>(s1);<br><span class="hljs-built_in">printSet</span>(s2);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>统计大小 — size</li><li>判断是否为空 — empty</li><li>交换容器 — swap</li></ul><h4 id="3-8-4-set插入和删除"><a href="#3-8-4-set插入和删除" class="headerlink" title="3.8.4 set插入和删除"></a>3.8.4 set插入和删除</h4><p><strong>功能描述:</strong></p><ul><li>set容器进行插入数据和删除数据</li></ul><p><strong>函数原型:</strong></p><ul><li><code>insert(elem);</code> //在容器中插入元素。</li><li><code>clear();</code> //清除所有元素</li><li><code>erase(pos);</code> //删除pos迭代器所指的元素,返回下一个元素的迭代器。</li><li><code>erase(beg, end);</code> //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。</li><li><code>erase(elem);</code> //删除容器中值为elem的元素。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printSet</span><span class="hljs-params">(set<<span class="hljs-keyword">int</span>> & s)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (set<<span class="hljs-keyword">int</span>>::iterator it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-comment">//插入和删除</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>set<<span class="hljs-keyword">int</span>> s1;<br><span class="hljs-comment">//插入</span><br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br><span class="hljs-built_in">printSet</span>(s1);<br><br><span class="hljs-comment">//删除</span><br>s1.<span class="hljs-built_in">erase</span>(s1.<span class="hljs-built_in">begin</span>());<br><span class="hljs-built_in">printSet</span>(s1);<br><br>s1.<span class="hljs-built_in">erase</span>(<span class="hljs-number">30</span>);<br><span class="hljs-built_in">printSet</span>(s1);<br><br><span class="hljs-comment">//清空</span><br><span class="hljs-comment">//s1.erase(s1.begin(), s1.end());</span><br>s1.<span class="hljs-built_in">clear</span>();<br><span class="hljs-built_in">printSet</span>(s1);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>插入 — insert</li><li>删除 — erase</li><li>清空 — clear</li></ul><h4 id="3-8-5-set查找和统计"><a href="#3-8-5-set查找和统计" class="headerlink" title="3.8.5 set查找和统计"></a>3.8.5 set查找和统计</h4><p><strong>功能描述:</strong></p><ul><li>对set容器进行查找数据以及统计数据</li></ul><p><strong>函数原型:</strong></p><ul><li><code>find(key);</code> //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();</li><li><code>count(key);</code> //统计key的元素个数</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><br><span class="hljs-comment">//查找和统计</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>set<<span class="hljs-keyword">int</span>> s1;<br><span class="hljs-comment">//插入</span><br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br><br><span class="hljs-comment">//查找</span><br>set<<span class="hljs-keyword">int</span>>::iterator pos = s1.<span class="hljs-built_in">find</span>(<span class="hljs-number">30</span>);<br><br><span class="hljs-keyword">if</span> (pos != s1.<span class="hljs-built_in">end</span>())<br>{<br>cout << <span class="hljs-string">"找到了元素 : "</span> << *pos << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"未找到元素"</span> << endl;<br>}<br><br><span class="hljs-comment">//统计</span><br><span class="hljs-keyword">int</span> num = s1.<span class="hljs-built_in">count</span>(<span class="hljs-number">30</span>);<br>cout << <span class="hljs-string">"num = "</span> << num << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>查找 — find (返回的是迭代器)</li><li>统计 — count (对于set,结果为0或者1)</li></ul><h4 id="3-8-6-set和multiset区别"><a href="#3-8-6-set和multiset区别" class="headerlink" title="3.8.6 set和multiset区别"></a>3.8.6 set和multiset区别</h4><p><strong>学习目标:</strong></p><ul><li>掌握set和multiset的区别</li></ul><p><strong>区别:</strong></p><ul><li>set不可以插入重复数据,而multiset可以</li><li>set插入数据的同时会返回插入结果,表示插入是否成功</li><li>multiset不会检测数据,因此可以插入重复数据</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><br><span class="hljs-comment">//set和multiset区别</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>set<<span class="hljs-keyword">int</span>> s;<br>pair<set<<span class="hljs-keyword">int</span>>::iterator, <span class="hljs-keyword">bool</span>> ret = s.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br><span class="hljs-keyword">if</span> (ret.second) {<br>cout << <span class="hljs-string">"第一次插入成功!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> {<br>cout << <span class="hljs-string">"第一次插入失败!"</span> << endl;<br>}<br><br>ret = s.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br><span class="hljs-keyword">if</span> (ret.second) {<br>cout << <span class="hljs-string">"第二次插入成功!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> {<br>cout << <span class="hljs-string">"第二次插入失败!"</span> << endl;<br>}<br> <br><span class="hljs-comment">//multiset</span><br>multiset<<span class="hljs-keyword">int</span>> ms;<br>ms.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>ms.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br><br><span class="hljs-keyword">for</span> (multiset<<span class="hljs-keyword">int</span>>::iterator it = ms.<span class="hljs-built_in">begin</span>(); it != ms.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>如果不允许插入重复数据可以利用set</li><li>如果需要插入重复数据利用multiset</li></ul><h4 id="3-8-7-pair对组创建"><a href="#3-8-7-pair对组创建" class="headerlink" title="3.8.7 pair对组创建"></a>3.8.7 pair对组创建</h4><p><strong>功能描述:</strong></p><ul><li>成对出现的数据,利用对组可以返回两个数据</li></ul><p><strong>两种创建方式:</strong></p><ul><li><code>pair<type, type> p ( value1, value2 );</code></li><li><code>pair<type, type> p = make_pair( value1, value2 );</code></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-comment">//对组创建</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-function">pair<string, <span class="hljs-keyword">int</span>> <span class="hljs-title">p</span><span class="hljs-params">(string(<span class="hljs-string">"Tom"</span>), <span class="hljs-number">20</span>)</span></span>;<br>cout << <span class="hljs-string">"姓名: "</span> << p.first << <span class="hljs-string">" 年龄: "</span> << p.second << endl;<br><br>pair<string, <span class="hljs-keyword">int</span>> p2 = <span class="hljs-built_in">make_pair</span>(<span class="hljs-string">"Jerry"</span>, <span class="hljs-number">10</span>);<br>cout << <span class="hljs-string">"姓名: "</span> << p2.first << <span class="hljs-string">" 年龄: "</span> << p2.second << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><p>两种方式都可以创建对组,记住一种即可</p><h4 id="3-8-8-set容器排序"><a href="#3-8-8-set容器排序" class="headerlink" title="3.8.8 set容器排序"></a>3.8.8 set容器排序</h4><p>学习目标:</p><ul><li>set容器默认排序规则为从小到大,掌握如何改变排序规则</li></ul><p>主要技术点:</p><ul><li>利用仿函数,可以改变排序规则</li></ul><p><strong>示例一</strong> set存放内置数据类型</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCompare</span> </span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> v1, <span class="hljs-keyword">int</span> v2)</span> </span>{<br><span class="hljs-keyword">return</span> v1 > v2;<br>}<br>};<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span><br><span class="hljs-function"></span>{ <br>set<<span class="hljs-keyword">int</span>> s1;<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s1.<span class="hljs-built_in">insert</span>(<span class="hljs-number">50</span>);<br><br><span class="hljs-comment">//默认从小到大</span><br><span class="hljs-keyword">for</span> (set<<span class="hljs-keyword">int</span>>::iterator it = s1.<span class="hljs-built_in">begin</span>(); it != s1.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><span class="hljs-comment">//指定排序规则</span><br>set<<span class="hljs-keyword">int</span>,MyCompare> s2;<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">10</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">40</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">20</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">30</span>);<br>s2.<span class="hljs-built_in">insert</span>(<span class="hljs-number">50</span>);<br><br><span class="hljs-keyword">for</span> (set<<span class="hljs-keyword">int</span>, MyCompare>::iterator it = s2.<span class="hljs-built_in">begin</span>(); it != s2.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:利用仿函数可以指定set容器的排序规则</p><p><strong>示例二</strong> set存放自定义数据类型</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><set></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br><br>};<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">comparePerson</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">const</span> Person& p1, <span class="hljs-keyword">const</span> Person &p2)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//按照年龄进行排序 降序</span><br><span class="hljs-keyword">return</span> p1.m_Age > p2.m_Age;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>set<Person, comparePerson> s;<br><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"刘备"</span>, <span class="hljs-number">23</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"关羽"</span>, <span class="hljs-number">27</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"张飞"</span>, <span class="hljs-number">25</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"赵云"</span>, <span class="hljs-number">21</span>)</span></span>;<br><br>s.<span class="hljs-built_in">insert</span>(p1);<br>s.<span class="hljs-built_in">insert</span>(p2);<br>s.<span class="hljs-built_in">insert</span>(p3);<br>s.<span class="hljs-built_in">insert</span>(p4);<br><br><span class="hljs-keyword">for</span> (set<Person, comparePerson>::iterator it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << <span class="hljs-string">"姓名: "</span> << it->m_Name << <span class="hljs-string">" 年龄: "</span> << it->m_Age << endl;<br>}<br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><p>对于自定义数据类型,set必须指定排序规则才可以插入数据</p><h3 id="3-9-map-multimap容器"><a href="#3-9-map-multimap容器" class="headerlink" title="3.9 map/ multimap容器"></a>3.9 map/ multimap容器</h3><h4 id="3-9-1-map基本概念"><a href="#3-9-1-map基本概念" class="headerlink" title="3.9.1 map基本概念"></a>3.9.1 map基本概念</h4><p><strong>简介:</strong></p><ul><li>map中所有元素都是pair</li><li>pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)</li><li>所有元素都会根据元素的键值自动排序</li></ul><p><strong>本质:</strong></p><ul><li>map/multimap属于<strong>关联式容器</strong>,底层结构是用二叉树实现。</li></ul><p><strong>优点:</strong></p><ul><li>可以根据key值快速找到value值</li></ul><p>map和multimap<strong>区别</strong>:</p><ul><li>map不允许容器中有重复key值元素</li><li>multimap允许容器中有重复key值元素</li></ul><h4 id="3-9-2-map构造和赋值"><a href="#3-9-2-map构造和赋值" class="headerlink" title="3.9.2 map构造和赋值"></a>3.9.2 map构造和赋值</h4><p><strong>功能描述:</strong></p><ul><li>对map容器进行构造和赋值操作</li></ul><p><strong>函数原型:</strong></p><p><strong>构造:</strong></p><ul><li><code>map<T1, T2> mp;</code> //map默认构造函数: </li><li><code>map(const map &mp);</code> //拷贝构造函数</li></ul><p><strong>赋值:</strong></p><ul><li><code>map& operator=(const map &mp);</code> //重载等号操作符</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><map></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printMap</span><span class="hljs-params">(map<<span class="hljs-keyword">int</span>,<span class="hljs-keyword">int</span>>&m)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>::iterator it = m.<span class="hljs-built_in">begin</span>(); it != m.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << <span class="hljs-string">"key = "</span> << it->first << <span class="hljs-string">" value = "</span> << it->second << endl;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>map<<span class="hljs-keyword">int</span>,<span class="hljs-keyword">int</span>>m; <span class="hljs-comment">//默认构造</span><br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">3</span>, <span class="hljs-number">30</span>));<br><span class="hljs-built_in">printMap</span>(m);<br><br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>><span class="hljs-built_in">m2</span>(m); <span class="hljs-comment">//拷贝构造</span><br><span class="hljs-built_in">printMap</span>(m2);<br><br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>m3;<br>m3 = m2; <span class="hljs-comment">//赋值</span><br><span class="hljs-built_in">printMap</span>(m3);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:map中所有元素都是成对出现,插入数据时候要使用对组</p><h4 id="3-9-3-map大小和交换"><a href="#3-9-3-map大小和交换" class="headerlink" title="3.9.3 map大小和交换"></a>3.9.3 map大小和交换</h4><p><strong>功能描述:</strong></p><ul><li>统计map容器大小以及交换map容器</li></ul><p>函数原型:</p><ul><li><code>size();</code> //返回容器中元素的数目</li><li><code>empty();</code> //判断容器是否为空</li><li><code>swap(st);</code> //交换两个集合容器</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><map></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printMap</span><span class="hljs-params">(map<<span class="hljs-keyword">int</span>,<span class="hljs-keyword">int</span>>&m)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>::iterator it = m.<span class="hljs-built_in">begin</span>(); it != m.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << <span class="hljs-string">"key = "</span> << it->first << <span class="hljs-string">" value = "</span> << it->second << endl;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>m;<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">3</span>, <span class="hljs-number">30</span>));<br><br><span class="hljs-keyword">if</span> (m.<span class="hljs-built_in">empty</span>())<br>{<br>cout << <span class="hljs-string">"m为空"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"m不为空"</span> << endl;<br>cout << <span class="hljs-string">"m的大小为: "</span> << m.<span class="hljs-built_in">size</span>() << endl;<br>}<br>}<br><br><br><span class="hljs-comment">//交换</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>m;<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">3</span>, <span class="hljs-number">30</span>));<br><br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>m2;<br>m2.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">4</span>, <span class="hljs-number">100</span>));<br>m2.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">5</span>, <span class="hljs-number">200</span>));<br>m2.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">6</span>, <span class="hljs-number">300</span>));<br><br>cout << <span class="hljs-string">"交换前"</span> << endl;<br><span class="hljs-built_in">printMap</span>(m);<br><span class="hljs-built_in">printMap</span>(m2);<br><br>cout << <span class="hljs-string">"交换后"</span> << endl;<br>m.<span class="hljs-built_in">swap</span>(m2);<br><span class="hljs-built_in">printMap</span>(m);<br><span class="hljs-built_in">printMap</span>(m2);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>统计大小 — size</li><li>判断是否为空 — empty</li><li>交换容器 — swap</li></ul><h4 id="3-9-4-map插入和删除"><a href="#3-9-4-map插入和删除" class="headerlink" title="3.9.4 map插入和删除"></a>3.9.4 map插入和删除</h4><p><strong>功能描述:</strong></p><ul><li>map容器进行插入数据和删除数据</li></ul><p><strong>函数原型:</strong></p><ul><li><code>insert(elem);</code> //在容器中插入元素。</li><li><code>clear();</code> //清除所有元素</li><li><code>erase(pos);</code> //删除pos迭代器所指的元素,返回下一个元素的迭代器。</li><li><code>erase(beg, end);</code> //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。</li><li><code>erase(key);</code> //删除容器中值为key的元素。</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><map></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printMap</span><span class="hljs-params">(map<<span class="hljs-keyword">int</span>,<span class="hljs-keyword">int</span>>&m)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>::iterator it = m.<span class="hljs-built_in">begin</span>(); it != m.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << <span class="hljs-string">"key = "</span> << it->first << <span class="hljs-string">" value = "</span> << it->second << endl;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//插入</span><br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>> m;<br><span class="hljs-comment">//第一种插入方式</span><br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>));<br><span class="hljs-comment">//第二种插入方式</span><br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>));<br><span class="hljs-comment">//第三种插入方式</span><br>m.<span class="hljs-built_in">insert</span>(map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>::<span class="hljs-built_in">value_type</span>(<span class="hljs-number">3</span>, <span class="hljs-number">30</span>));<br><span class="hljs-comment">//第四种插入方式</span><br>m[<span class="hljs-number">4</span>] = <span class="hljs-number">40</span>; <br><span class="hljs-built_in">printMap</span>(m);<br><br><span class="hljs-comment">//删除</span><br>m.<span class="hljs-built_in">erase</span>(m.<span class="hljs-built_in">begin</span>());<br><span class="hljs-built_in">printMap</span>(m);<br><br>m.<span class="hljs-built_in">erase</span>(<span class="hljs-number">3</span>);<br><span class="hljs-built_in">printMap</span>(m);<br><br><span class="hljs-comment">//清空</span><br>m.<span class="hljs-built_in">erase</span>(m.<span class="hljs-built_in">begin</span>(),m.<span class="hljs-built_in">end</span>());<br>m.<span class="hljs-built_in">clear</span>();<br><span class="hljs-built_in">printMap</span>(m);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>map插入方式很多,记住其一即可</li></ul><ul><li>插入 — insert </li><li>删除 — erase</li><li>清空 — clear</li></ul><h4 id="3-9-5-map查找和统计"><a href="#3-9-5-map查找和统计" class="headerlink" title="3.9.5 map查找和统计"></a>3.9.5 map查找和统计</h4><p><strong>功能描述:</strong></p><ul><li>对map容器进行查找数据以及统计数据</li></ul><p><strong>函数原型:</strong></p><ul><li><code>find(key);</code> //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();</li><li><code>count(key);</code> //统计key的元素个数</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><map></span></span><br><br><span class="hljs-comment">//查找和统计</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>m; <br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>));<br>m.<span class="hljs-built_in">insert</span>(pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>(<span class="hljs-number">3</span>, <span class="hljs-number">30</span>));<br><br><span class="hljs-comment">//查找</span><br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>>::iterator pos = m.<span class="hljs-built_in">find</span>(<span class="hljs-number">3</span>);<br><br><span class="hljs-keyword">if</span> (pos != m.<span class="hljs-built_in">end</span>())<br>{<br>cout << <span class="hljs-string">"找到了元素 key = "</span> << (*pos).first << <span class="hljs-string">" value = "</span> << (*pos).second << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"未找到元素"</span> << endl;<br>}<br><br><span class="hljs-comment">//统计</span><br><span class="hljs-keyword">int</span> num = m.<span class="hljs-built_in">count</span>(<span class="hljs-number">3</span>);<br>cout << <span class="hljs-string">"num = "</span> << num << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>查找 — find (返回的是迭代器)</li><li>统计 — count (对于map,结果为0或者1)</li></ul><h4 id="3-9-6-map容器排序"><a href="#3-9-6-map容器排序" class="headerlink" title="3.9.6 map容器排序"></a>3.9.6 map容器排序</h4><p><strong>学习目标:</strong></p><ul><li>map容器默认排序规则为 按照key值进行 从小到大排序,掌握如何改变排序规则</li></ul><p><strong>主要技术点:</strong></p><ul><li>利用仿函数,可以改变排序规则</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><map></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCompare</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> v1, <span class="hljs-keyword">int</span> v2)</span> </span>{<br><span class="hljs-keyword">return</span> v1 > v2;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">//默认从小到大排序</span><br><span class="hljs-comment">//利用仿函数实现从大到小排序</span><br>map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>, MyCompare> m;<br><br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>));<br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>));<br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(<span class="hljs-number">3</span>, <span class="hljs-number">30</span>));<br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(<span class="hljs-number">4</span>, <span class="hljs-number">40</span>));<br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(<span class="hljs-number">5</span>, <span class="hljs-number">50</span>));<br><br><span class="hljs-keyword">for</span> (map<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>, MyCompare>::iterator it = m.<span class="hljs-built_in">begin</span>(); it != m.<span class="hljs-built_in">end</span>(); it++) {<br>cout << <span class="hljs-string">"key:"</span> << it->first << <span class="hljs-string">" value:"</span> << it->second << endl;<br>}<br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>利用仿函数可以指定map容器的排序规则</li><li>对于自定义数据类型,map必须要指定排序规则,同set容器</li></ul><h3 id="3-10-案例-员工分组"><a href="#3-10-案例-员工分组" class="headerlink" title="3.10 案例-员工分组"></a>3.10 案例-员工分组</h3><h4 id="3-10-1-案例描述"><a href="#3-10-1-案例描述" class="headerlink" title="3.10.1 案例描述"></a>3.10.1 案例描述</h4><ul><li>公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作</li><li>员工信息有: 姓名 工资组成;部门分为:策划、美术、研发</li><li>随机给10名员工分配部门和工资</li><li>通过multimap进行信息的插入 key(部门编号) value(员工)</li><li>分部门显示员工信息</li></ul><h4 id="3-10-2-实现步骤"><a href="#3-10-2-实现步骤" class="headerlink" title="3.10.2 实现步骤"></a>3.10.2 实现步骤</h4><ol><li>创建10名员工,放到vector中</li><li>遍历vector容器,取出每个员工,进行随机分组</li><li>分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中</li><li>分部门显示员工信息</li></ol><p><strong>案例代码:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><map></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><ctime></span></span><br><br><span class="hljs-comment">/*</span><br><span class="hljs-comment">- 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作</span><br><span class="hljs-comment">- 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发</span><br><span class="hljs-comment">- 随机给10名员工分配部门和工资</span><br><span class="hljs-comment">- 通过multimap进行信息的插入 key(部门编号) value(员工)</span><br><span class="hljs-comment">- 分部门显示员工信息</span><br><span class="hljs-comment">*/</span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> CEHUA 0</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MEISHU 1</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> YANFA 2</span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Worker</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br>string m_Name;<br><span class="hljs-keyword">int</span> m_Salary;<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">createWorker</span><span class="hljs-params">(vector<Worker>&v)</span></span><br><span class="hljs-function"></span>{<br>string nameSeed = <span class="hljs-string">"ABCDEFGHIJ"</span>;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>Worker worker;<br>worker.m_Name = <span class="hljs-string">"员工"</span>;<br>worker.m_Name += nameSeed[i];<br><br>worker.m_Salary = <span class="hljs-built_in">rand</span>() % <span class="hljs-number">10000</span> + <span class="hljs-number">10000</span>; <span class="hljs-comment">// 10000 ~ 19999</span><br><span class="hljs-comment">//将员工放入到容器中</span><br>v.<span class="hljs-built_in">push_back</span>(worker);<br>}<br>}<br><br><span class="hljs-comment">//员工分组</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">setGroup</span><span class="hljs-params">(vector<Worker>&v,multimap<<span class="hljs-keyword">int</span>,Worker>&m)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">for</span> (vector<Worker>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++)<br>{<br><span class="hljs-comment">//产生随机部门编号</span><br><span class="hljs-keyword">int</span> deptId = <span class="hljs-built_in">rand</span>() % <span class="hljs-number">3</span>; <span class="hljs-comment">// 0 1 2 </span><br><br><span class="hljs-comment">//将员工插入到分组中</span><br><span class="hljs-comment">//key部门编号,value具体员工</span><br>m.<span class="hljs-built_in">insert</span>(<span class="hljs-built_in">make_pair</span>(deptId, *it));<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">showWorkerByGourp</span><span class="hljs-params">(multimap<<span class="hljs-keyword">int</span>,Worker>&m)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-comment">// 0 A B C 1 D E 2 F G ...</span><br>cout << <span class="hljs-string">"策划部门:"</span> << endl;<br><br>multimap<<span class="hljs-keyword">int</span>,Worker>::iterator pos = m.<span class="hljs-built_in">find</span>(CEHUA);<br><span class="hljs-keyword">int</span> count = m.<span class="hljs-built_in">count</span>(CEHUA); <span class="hljs-comment">// 统计具体人数</span><br><span class="hljs-keyword">int</span> index = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">for</span> (; pos != m.<span class="hljs-built_in">end</span>() && index < count; pos++ , index++)<br>{<br>cout << <span class="hljs-string">"姓名: "</span> << pos->second.m_Name << <span class="hljs-string">" 工资: "</span> << pos->second.m_Salary << endl;<br>}<br><br>cout << <span class="hljs-string">"----------------------"</span> << endl;<br>cout << <span class="hljs-string">"美术部门: "</span> << endl;<br>pos = m.<span class="hljs-built_in">find</span>(MEISHU);<br>count = m.<span class="hljs-built_in">count</span>(MEISHU); <span class="hljs-comment">// 统计具体人数</span><br>index = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">for</span> (; pos != m.<span class="hljs-built_in">end</span>() && index < count; pos++, index++)<br>{<br>cout << <span class="hljs-string">"姓名: "</span> << pos->second.m_Name << <span class="hljs-string">" 工资: "</span> << pos->second.m_Salary << endl;<br>}<br><br>cout << <span class="hljs-string">"----------------------"</span> << endl;<br>cout << <span class="hljs-string">"研发部门: "</span> << endl;<br>pos = m.<span class="hljs-built_in">find</span>(YANFA);<br>count = m.<span class="hljs-built_in">count</span>(YANFA); <span class="hljs-comment">// 统计具体人数</span><br>index = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">for</span> (; pos != m.<span class="hljs-built_in">end</span>() && index < count; pos++, index++)<br>{<br>cout << <span class="hljs-string">"姓名: "</span> << pos->second.m_Name << <span class="hljs-string">" 工资: "</span> << pos->second.m_Salary << endl;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">srand</span>((<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span>)<span class="hljs-built_in">time</span>(<span class="hljs-literal">NULL</span>));<br><br><span class="hljs-comment">//1、创建员工</span><br>vector<Worker>vWorker;<br><span class="hljs-built_in">createWorker</span>(vWorker);<br><br><span class="hljs-comment">//2、员工分组</span><br>multimap<<span class="hljs-keyword">int</span>, Worker>mWorker;<br><span class="hljs-built_in">setGroup</span>(vWorker, mWorker);<br><br><br><span class="hljs-comment">//3、分组显示员工</span><br><span class="hljs-built_in">showWorkerByGourp</span>(mWorker);<br><br><span class="hljs-comment">////测试</span><br><span class="hljs-comment">//for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)</span><br><span class="hljs-comment">//{</span><br><span class="hljs-comment">//cout << "姓名: " << it->m_Name << " 工资: " << it->m_Salary << endl;</span><br><span class="hljs-comment">//}</span><br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>当数据以键值对形式存在,可以考虑用map 或 multimap</li></ul><h2 id="4-STL-函数对象"><a href="#4-STL-函数对象" class="headerlink" title="4 STL- 函数对象"></a>4 STL- 函数对象</h2><h3 id="4-1-函数对象"><a href="#4-1-函数对象" class="headerlink" title="4.1 函数对象"></a>4.1 函数对象</h3><h4 id="4-1-1-函数对象概念"><a href="#4-1-1-函数对象概念" class="headerlink" title="4.1.1 函数对象概念"></a>4.1.1 函数对象概念</h4><p><strong>概念:</strong></p><ul><li>重载<strong>函数调用操作符</strong>的类,其对象常称为<strong>函数对象</strong></li><li><strong>函数对象</strong>使用重载的()时,行为类似函数调用,也叫<strong>仿函数</strong></li></ul><p><strong>本质:</strong></p><p>函数对象(仿函数)是一个<strong>类</strong>,不是一个函数</p><h4 id="4-1-2-函数对象使用"><a href="#4-1-2-函数对象使用" class="headerlink" title="4.1.2 函数对象使用"></a>4.1.2 函数对象使用</h4><p><strong>特点:</strong></p><ul><li>函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值</li><li>函数对象超出普通函数的概念,函数对象可以有自己的状态</li><li>函数对象可以作为参数传递</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-comment">//1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAdd</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span> :<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> v1,<span class="hljs-keyword">int</span> v2)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> v1 + v2;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>MyAdd myAdd;<br>cout << <span class="hljs-built_in">myAdd</span>(<span class="hljs-number">10</span>, <span class="hljs-number">10</span>) << endl;<br>}<br><br><span class="hljs-comment">//2、函数对象可以有自己的状态</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">MyPrint</span>()<br>{<br>count = <span class="hljs-number">0</span>;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(string test)</span></span><br><span class="hljs-function"></span>{<br>cout << test << endl;<br>count++; <span class="hljs-comment">//统计使用次数</span><br>}<br><br><span class="hljs-keyword">int</span> count; <span class="hljs-comment">//内部自己的状态</span><br>};<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>MyPrint myPrint;<br><span class="hljs-built_in">myPrint</span>(<span class="hljs-string">"hello world"</span>);<br><span class="hljs-built_in">myPrint</span>(<span class="hljs-string">"hello world"</span>);<br><span class="hljs-built_in">myPrint</span>(<span class="hljs-string">"hello world"</span>);<br>cout << <span class="hljs-string">"myPrint调用次数为: "</span> << myPrint.count << endl;<br>}<br><br><span class="hljs-comment">//3、函数对象可以作为参数传递</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">doPrint</span><span class="hljs-params">(MyPrint &mp , string test)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-built_in">mp</span>(test);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test03</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>MyPrint myPrint;<br><span class="hljs-built_in">doPrint</span>(myPrint, <span class="hljs-string">"Hello C++"</span>);<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><span class="hljs-comment">//test02();</span><br><span class="hljs-built_in">test03</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:</p><ul><li>仿函数写法非常灵活,可以作为参数进行传递。</li></ul><h3 id="4-2-谓词"><a href="#4-2-谓词" class="headerlink" title="4.2 谓词"></a>4.2 谓词</h3><h4 id="4-2-1-谓词概念"><a href="#4-2-1-谓词概念" class="headerlink" title="4.2.1 谓词概念"></a>4.2.1 谓词概念</h4><p><strong>概念:</strong></p><ul><li>返回bool类型的仿函数称为<strong>谓词</strong></li><li>如果operator()接受一个参数,那么叫做一元谓词</li><li>如果operator()接受两个参数,那么叫做二元谓词</li></ul><h4 id="4-2-2-一元谓词"><a href="#4-2-2-一元谓词" class="headerlink" title="4.2.2 一元谓词"></a>4.2.2 一元谓词</h4><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-comment">//1.一元谓词</span><br><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">GreaterFive</span>{</span><br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span> </span>{<br><span class="hljs-keyword">return</span> val > <span class="hljs-number">5</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br><br>vector<<span class="hljs-keyword">int</span>>::iterator it = <span class="hljs-built_in">find_if</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">GreaterFive</span>());<br><span class="hljs-keyword">if</span> (it == v.<span class="hljs-built_in">end</span>()) {<br>cout << <span class="hljs-string">"没找到!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> {<br>cout << <span class="hljs-string">"找到:"</span> << *it << endl;<br>}<br><br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:参数只有一个的谓词,称为一元谓词</p><h4 id="4-2-3-二元谓词"><a href="#4-2-3-二元谓词" class="headerlink" title="4.2.3 二元谓词"></a>4.2.3 二元谓词</h4><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-comment">//二元谓词</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCompare</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num1, <span class="hljs-keyword">int</span> num2)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> num1 > num2;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br><br><span class="hljs-comment">//默认从小到大</span><br><span class="hljs-built_in">sort</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>());<br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>cout << <span class="hljs-string">"----------------------------"</span> << endl;<br><br><span class="hljs-comment">//使用函数对象改变算法策略,排序从大到小</span><br><span class="hljs-built_in">sort</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">MyCompare</span>());<br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:参数只有两个的谓词,称为二元谓词</p><h3 id="4-3-内建函数对象"><a href="#4-3-内建函数对象" class="headerlink" title="4.3 内建函数对象"></a>4.3 内建函数对象</h3><h4 id="4-3-1-内建函数对象意义"><a href="#4-3-1-内建函数对象意义" class="headerlink" title="4.3.1 内建函数对象意义"></a>4.3.1 内建函数对象意义</h4><p><strong>概念:</strong></p><ul><li>STL内建了一些函数对象</li></ul><p><strong>分类:</strong></p><ul><li><p>算术仿函数</p></li><li><p>关系仿函数</p></li><li><p>逻辑仿函数</p></li></ul><p><strong>用法:</strong></p><ul><li>这些仿函数所产生的对象,用法和一般函数完全相同</li><li>使用内建函数对象,需要引入头文件 <code>#include<functional></code></li></ul><h4 id="4-3-2-算术仿函数"><a href="#4-3-2-算术仿函数" class="headerlink" title="4.3.2 算术仿函数"></a>4.3.2 算术仿函数</h4><p><strong>功能描述:</strong></p><ul><li>实现四则运算</li><li>其中negate是一元运算,其他都是二元运算</li></ul><p><strong>仿函数原型:</strong></p><ul><li><code>template<class T> T plus<T></code> //加法仿函数</li><li><code>template<class T> T minus<T></code> //减法仿函数</li><li><code>template<class T> T multiplies<T></code> //乘法仿函数</li><li><code>template<class T> T divides<T></code> //除法仿函数</li><li><code>template<class T> T modulus<T></code> //取模仿函数</li><li><code>template<class T> T negate<T></code> //取反仿函数</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><functional></span></span><br><span class="hljs-comment">//negate</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>negate<<span class="hljs-keyword">int</span>> n;<br>cout << <span class="hljs-built_in">n</span>(<span class="hljs-number">50</span>) << endl;<br>}<br><br><span class="hljs-comment">//plus</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>plus<<span class="hljs-keyword">int</span>> p;<br>cout << <span class="hljs-built_in">p</span>(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>) << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:使用内建函数对象时,需要引入头文件 <code>#include <functional></code></p><h4 id="4-3-3-关系仿函数"><a href="#4-3-3-关系仿函数" class="headerlink" title="4.3.3 关系仿函数"></a>4.3.3 关系仿函数</h4><p><strong>功能描述:</strong></p><ul><li>实现关系对比</li></ul><p><strong>仿函数原型:</strong></p><ul><li><code>template<class T> bool equal_to<T></code> //等于</li><li><code>template<class T> bool not_equal_to<T></code> //不等于</li><li><code>template<class T> bool greater<T></code> //大于</li><li><code>template<class T> bool greater_equal<T></code> //大于等于</li><li><code>template<class T> bool less<T></code> //小于</li><li><code>template<class T> bool less_equal<T></code> //小于等于</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><functional></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCompare</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> v1,<span class="hljs-keyword">int</span> v2)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> v1 > v2;<br>}<br>};<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br><br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><span class="hljs-comment">//自己实现仿函数</span><br><span class="hljs-comment">//sort(v.begin(), v.end(), MyCompare());</span><br><span class="hljs-comment">//STL内建仿函数 大于仿函数</span><br><span class="hljs-built_in">sort</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), greater<<span class="hljs-keyword">int</span>>());<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">int</span>>::iterator it = v.<span class="hljs-built_in">begin</span>(); it != v.<span class="hljs-built_in">end</span>(); it++) {<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:关系仿函数中最常用的就是greater<>大于</p><h4 id="4-3-4-逻辑仿函数"><a href="#4-3-4-逻辑仿函数" class="headerlink" title="4.3.4 逻辑仿函数"></a>4.3.4 逻辑仿函数</h4><p><strong>功能描述:</strong></p><ul><li>实现逻辑运算</li></ul><p><strong>函数原型:</strong></p><ul><li><code>template<class T> bool logical_and<T></code> //逻辑与</li><li><code>template<class T> bool logical_or<T></code> //逻辑或</li><li><code>template<class T> bool logical_not<T></code> //逻辑非</li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><functional></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">bool</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-literal">true</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-literal">false</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-literal">true</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-literal">false</span>);<br><br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">bool</span>>::iterator it = v.<span class="hljs-built_in">begin</span>();it!= v.<span class="hljs-built_in">end</span>();it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br><br><span class="hljs-comment">//逻辑非 将v容器搬运到v2中,并执行逻辑非运算</span><br>vector<<span class="hljs-keyword">bool</span>> v2;<br>v2.<span class="hljs-built_in">resize</span>(v.<span class="hljs-built_in">size</span>());<br><span class="hljs-built_in">transform</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), v2.<span class="hljs-built_in">begin</span>(), logical_not<<span class="hljs-keyword">bool</span>>());<br><span class="hljs-keyword">for</span> (vector<<span class="hljs-keyword">bool</span>>::iterator it = v2.<span class="hljs-built_in">begin</span>(); it != v2.<span class="hljs-built_in">end</span>(); it++)<br>{<br>cout << *it << <span class="hljs-string">" "</span>;<br>}<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:逻辑仿函数实际应用较少,了解即可</p><h2 id="5-STL-常用算法"><a href="#5-STL-常用算法" class="headerlink" title="5 STL- 常用算法"></a>5 STL- 常用算法</h2><p><strong>概述</strong>:</p><ul><li>算法主要是由头文件<code><algorithm></code> <code><functional></code> <code><numeric></code>组成。</li></ul><ul><li><code><algorithm></code>是所有STL头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等</li><li><code><numeric></code>体积很小,只包括几个在序列上面进行简单数学运算的模板函数</li><li><code><functional></code>定义了一些模板类,用以声明函数对象。</li></ul><h3 id="5-1-常用遍历算法"><a href="#5-1-常用遍历算法" class="headerlink" title="5.1 常用遍历算法"></a>5.1 常用遍历算法</h3><p><strong>学习目标:</strong></p><ul><li>掌握常用的遍历算法</li></ul><p><strong>算法简介:</strong></p><ul><li><code>for_each</code> //遍历容器</li><li><code>transform</code> //搬运容器到另一个容器中</li></ul><h4 id="5-1-1-for-each"><a href="#5-1-1-for-each" class="headerlink" title="5.1.1 for_each"></a>5.1.1 for_each</h4><p><strong>功能描述:</strong></p><ul><li>实现遍历容器</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>for_each(iterator beg, iterator end, _func); </code></p><p>// 遍历算法 遍历容器元素</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// _func 函数或者函数对象</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-comment">//普通函数</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">print01</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span> </span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br><span class="hljs-comment">//函数对象</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">print02</span> </span><br><span class="hljs-class">{</span><br> <span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span> </span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-comment">//for_each算法基本用法</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) <br>{<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br><br><span class="hljs-comment">//遍历算法</span><br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), print01);<br>cout << endl;<br><br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">print02</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>for_each在实际开发中是最常用遍历算法,需要熟练掌握</p><h4 id="5-1-2-transform"><a href="#5-1-2-transform" class="headerlink" title="5.1.2 transform"></a>5.1.2 transform</h4><p><strong>功能描述:</strong></p><ul><li>搬运容器到另一个容器中</li></ul><p><strong>函数原型:</strong></p><ul><li><code>transform(iterator beg1, iterator end1, iterator beg2, _func);</code></li></ul><p>//beg1 源容器开始迭代器</p><p>//end1 源容器结束迭代器</p><p>//beg2 目标容器开始迭代器</p><p>//_func 函数或者函数对象</p><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-comment">//常用遍历算法 搬运 transform</span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TransForm</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> val;<br>}<br><br>};<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>>v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br><br>vector<<span class="hljs-keyword">int</span>>vTarget; <span class="hljs-comment">//目标容器</span><br><br>vTarget.<span class="hljs-built_in">resize</span>(v.<span class="hljs-built_in">size</span>()); <span class="hljs-comment">// 目标容器需要提前开辟空间</span><br><br><span class="hljs-built_in">transform</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), vTarget.<span class="hljs-built_in">begin</span>(), <span class="hljs-built_in">TransForm</span>());<br><br>for_each(vTarget.<span class="hljs-built_in">begin</span>(), vTarget.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">MyPrint</span>());<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong> 搬运的目标容器必须要提前开辟空间,否则无法正常搬运</p><h3 id="5-2-常用查找算法"><a href="#5-2-常用查找算法" class="headerlink" title="5.2 常用查找算法"></a>5.2 常用查找算法</h3><p>学习目标:</p><ul><li>掌握常用的查找算法</li></ul><p><strong>算法简介:</strong></p><ul><li><code>find</code> //查找元素</li><li><code>find_if</code> //按条件查找元素</li><li><code>adjacent_find</code> //查找相邻重复元素</li><li><code>binary_search</code> //二分查找法</li><li><code>count</code> //统计元素个数</li><li><code>count_if</code> //按条件统计元素个数</li></ul><h4 id="5-2-1-find"><a href="#5-2-1-find" class="headerlink" title="5.2.1 find"></a>5.2.1 find</h4><p><strong>功能描述:</strong></p><ul><li>查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>find(iterator beg, iterator end, value); </code></p><p>// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// value 查找的元素</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {<br>v.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">1</span>);<br>}<br><span class="hljs-comment">//查找容器中是否有 5 这个元素</span><br>vector<<span class="hljs-keyword">int</span>>::iterator it = <span class="hljs-built_in">find</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-number">5</span>);<br><span class="hljs-keyword">if</span> (it == v.<span class="hljs-built_in">end</span>()) <br>{<br>cout << <span class="hljs-string">"没有找到!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> <br>{<br>cout << <span class="hljs-string">"找到:"</span> << *it << endl;<br>}<br>}<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age) <br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><span class="hljs-comment">//重载==</span><br><span class="hljs-keyword">bool</span> <span class="hljs-keyword">operator</span>==(<span class="hljs-keyword">const</span> Person& p) <br>{<br><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>->m_Name == p.m_Name && <span class="hljs-keyword">this</span>->m_Age == p.m_Age) <br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>}<br><span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>}<br><br><span class="hljs-keyword">public</span>:<br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span> </span>{<br><br>vector<Person> v;<br><br><span class="hljs-comment">//创建数据</span><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"aaa"</span>, <span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"bbb"</span>, <span class="hljs-number">20</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"ccc"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"ddd"</span>, <span class="hljs-number">40</span>)</span></span>;<br><br>v.<span class="hljs-built_in">push_back</span>(p1);<br>v.<span class="hljs-built_in">push_back</span>(p2);<br>v.<span class="hljs-built_in">push_back</span>(p3);<br>v.<span class="hljs-built_in">push_back</span>(p4);<br><br>vector<Person>::iterator it = <span class="hljs-built_in">find</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), p2);<br><span class="hljs-keyword">if</span> (it == v.<span class="hljs-built_in">end</span>()) <br>{<br>cout << <span class="hljs-string">"没有找到!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> <br>{<br>cout << <span class="hljs-string">"找到姓名:"</span> << it->m_Name << <span class="hljs-string">" 年龄: "</span> << it->m_Age << endl;<br>}<br>}<br></code></pre></td></tr></table></figure><p>总结: 利用find可以在容器中找指定的元素,返回值是<strong>迭代器</strong></p><h4 id="5-2-2-find-if"><a href="#5-2-2-find-if" class="headerlink" title="5.2.2 find_if"></a>5.2.2 find_if</h4><p><strong>功能描述:</strong></p><ul><li>按条件查找元素</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>find_if(iterator beg, iterator end, _Pred); </code></p><p>// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// _Pred 函数或者谓词(返回bool类型的仿函数)</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span><br><br><span class="hljs-comment">//内置数据类型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GreaterFive</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> val > <span class="hljs-number">5</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br><br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {<br>v.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">1</span>);<br>}<br><br>vector<<span class="hljs-keyword">int</span>>::iterator it = <span class="hljs-built_in">find_if</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">GreaterFive</span>());<br><span class="hljs-keyword">if</span> (it == v.<span class="hljs-built_in">end</span>()) {<br>cout << <span class="hljs-string">"没有找到!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> {<br>cout << <span class="hljs-string">"找到大于5的数字:"</span> << *it << endl;<br>}<br>}<br><br><span class="hljs-comment">//自定义数据类型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> {</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><span class="hljs-keyword">public</span>:<br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Greater20</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(Person &p)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> p.m_Age > <span class="hljs-number">20</span>;<br>}<br><br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span> </span>{<br><br>vector<Person> v;<br><br><span class="hljs-comment">//创建数据</span><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"aaa"</span>, <span class="hljs-number">10</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"bbb"</span>, <span class="hljs-number">20</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"ccc"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"ddd"</span>, <span class="hljs-number">40</span>)</span></span>;<br><br>v.<span class="hljs-built_in">push_back</span>(p1);<br>v.<span class="hljs-built_in">push_back</span>(p2);<br>v.<span class="hljs-built_in">push_back</span>(p3);<br>v.<span class="hljs-built_in">push_back</span>(p4);<br><br>vector<Person>::iterator it = <span class="hljs-built_in">find_if</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">Greater20</span>());<br><span class="hljs-keyword">if</span> (it == v.<span class="hljs-built_in">end</span>())<br>{<br>cout << <span class="hljs-string">"没有找到!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"找到姓名:"</span> << it->m_Name << <span class="hljs-string">" 年龄: "</span> << it->m_Age << endl;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p>总结:find_if按条件查找使查找更加灵活,提供的仿函数可以改变不同的策略</p><h4 id="5-2-3-adjacent-find"><a href="#5-2-3-adjacent-find" class="headerlink" title="5.2.3 adjacent_find"></a>5.2.3 adjacent_find</h4><p><strong>功能描述:</strong></p><ul><li>查找相邻重复元素</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>adjacent_find(iterator beg, iterator end); </code></p><p>// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">1</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">2</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">5</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">2</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">3</span>);<br><br><span class="hljs-comment">//查找相邻重复元素</span><br>vector<<span class="hljs-keyword">int</span>>::iterator it = <span class="hljs-built_in">adjacent_find</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>());<br><span class="hljs-keyword">if</span> (it == v.<span class="hljs-built_in">end</span>()) {<br>cout << <span class="hljs-string">"找不到!"</span> << endl;<br>}<br><span class="hljs-keyword">else</span> {<br>cout << <span class="hljs-string">"找到相邻重复元素为:"</span> << *it << endl;<br>}<br>}<br></code></pre></td></tr></table></figure><p>总结:面试题中如果出现查找相邻重复元素,记得用STL中的adjacent_find算法</p><h4 id="5-2-4-binary-search"><a href="#5-2-4-binary-search" class="headerlink" title="5.2.4 binary_search"></a>5.2.4 binary_search</h4><p><strong>功能描述:</strong></p><ul><li>查找指定元素是否存在</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>bool binary_search(iterator beg, iterator end, value); </code></p><p>// 查找指定的元素,查到 返回true 否则false</p><p>// 注意: 在<strong>无序序列中不可用</strong></p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// value 查找的元素</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>>v;<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br>{<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br><span class="hljs-comment">//二分查找</span><br><span class="hljs-keyword">bool</span> ret = <span class="hljs-built_in">binary_search</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(),<span class="hljs-number">2</span>);<br><span class="hljs-keyword">if</span> (ret)<br>{<br>cout << <span class="hljs-string">"找到了"</span> << endl;<br>}<br><span class="hljs-keyword">else</span><br>{<br>cout << <span class="hljs-string">"未找到"</span> << endl;<br>}<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>二分查找法查找效率很高,值得注意的是查找的容器中元素必须的有序序列</p><h4 id="5-2-5-count"><a href="#5-2-5-count" class="headerlink" title="5.2.5 count"></a>5.2.5 count</h4><p><strong>功能描述:</strong></p><ul><li>统计元素个数</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>count(iterator beg, iterator end, value); </code></p><p>// 统计元素出现次数</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// value 统计的元素</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-comment">//内置数据类型</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">1</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">2</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">5</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">3</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br><br><span class="hljs-keyword">int</span> num = <span class="hljs-built_in">count</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-number">4</span>);<br><br>cout << <span class="hljs-string">"4的个数为: "</span> << num << endl;<br>}<br><br><span class="hljs-comment">//自定义数据类型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><span class="hljs-keyword">bool</span> <span class="hljs-keyword">operator</span>==(<span class="hljs-keyword">const</span> Person & p)<br>{<br><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>->m_Age == p.m_Age)<br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>}<br><span class="hljs-keyword">else</span><br>{<br><span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>}<br>}<br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<Person> v;<br><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"刘备"</span>, <span class="hljs-number">35</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"关羽"</span>, <span class="hljs-number">35</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"张飞"</span>, <span class="hljs-number">35</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"赵云"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p5</span><span class="hljs-params">(<span class="hljs-string">"曹操"</span>, <span class="hljs-number">25</span>)</span></span>;<br><br>v.<span class="hljs-built_in">push_back</span>(p1);<br>v.<span class="hljs-built_in">push_back</span>(p2);<br>v.<span class="hljs-built_in">push_back</span>(p3);<br>v.<span class="hljs-built_in">push_back</span>(p4);<br>v.<span class="hljs-built_in">push_back</span>(p5);<br> <br> <span class="hljs-function">Person <span class="hljs-title">p</span><span class="hljs-params">(<span class="hljs-string">"诸葛亮"</span>,<span class="hljs-number">35</span>)</span></span>;<br><br><span class="hljs-keyword">int</span> num = <span class="hljs-built_in">count</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), p);<br>cout << <span class="hljs-string">"num = "</span> << num << endl;<br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong> 统计自定义数据类型时候,需要配合重载 <code>operator==</code></p><h4 id="5-2-6-count-if"><a href="#5-2-6-count-if" class="headerlink" title="5.2.6 count_if"></a>5.2.6 count_if</h4><p><strong>功能描述:</strong></p><ul><li>按条件统计元素个数</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>count_if(iterator beg, iterator end, _Pred); </code></p><p>// 按条件统计元素出现次数</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// _Pred 谓词</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Greater4</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> val >= <span class="hljs-number">4</span>;<br>}<br>};<br><br><span class="hljs-comment">//内置数据类型</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">1</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">2</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">5</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">3</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">4</span>);<br><br><span class="hljs-keyword">int</span> num = <span class="hljs-built_in">count_if</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">Greater4</span>());<br><br>cout << <span class="hljs-string">"大于4的个数为: "</span> << num << endl;<br>}<br><br><span class="hljs-comment">//自定义数据类型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-built_in">Person</span>(string name, <span class="hljs-keyword">int</span> age)<br>{<br><span class="hljs-keyword">this</span>->m_Name = name;<br><span class="hljs-keyword">this</span>->m_Age = age;<br>}<br><br>string m_Name;<br><span class="hljs-keyword">int</span> m_Age;<br>};<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AgeLess35</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">const</span> Person &p)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> p.m_Age < <span class="hljs-number">35</span>;<br>}<br>};<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test02</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<Person> v;<br><br><span class="hljs-function">Person <span class="hljs-title">p1</span><span class="hljs-params">(<span class="hljs-string">"刘备"</span>, <span class="hljs-number">35</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p2</span><span class="hljs-params">(<span class="hljs-string">"关羽"</span>, <span class="hljs-number">35</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p3</span><span class="hljs-params">(<span class="hljs-string">"张飞"</span>, <span class="hljs-number">35</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p4</span><span class="hljs-params">(<span class="hljs-string">"赵云"</span>, <span class="hljs-number">30</span>)</span></span>;<br><span class="hljs-function">Person <span class="hljs-title">p5</span><span class="hljs-params">(<span class="hljs-string">"曹操"</span>, <span class="hljs-number">25</span>)</span></span>;<br><br>v.<span class="hljs-built_in">push_back</span>(p1);<br>v.<span class="hljs-built_in">push_back</span>(p2);<br>v.<span class="hljs-built_in">push_back</span>(p3);<br>v.<span class="hljs-built_in">push_back</span>(p4);<br>v.<span class="hljs-built_in">push_back</span>(p5);<br><br><span class="hljs-keyword">int</span> num = <span class="hljs-built_in">count_if</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">AgeLess35</span>());<br>cout << <span class="hljs-string">"小于35岁的个数:"</span> << num << endl;<br>}<br><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-comment">//test01();</span><br><br><span class="hljs-built_in">test02</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>按值统计用count,按条件统计用count_if</p><h3 id="5-3-常用排序算法"><a href="#5-3-常用排序算法" class="headerlink" title="5.3 常用排序算法"></a>5.3 常用排序算法</h3><p><strong>学习目标:</strong></p><ul><li>掌握常用的排序算法</li></ul><p><strong>算法简介:</strong></p><ul><li><code>sort</code> //对容器内元素进行排序</li><li><code>random_shuffle</code> //洗牌 指定范围内的元素随机调整次序</li><li><code>merge </code> // 容器元素合并,并存储到另一容器中</li><li><code>reverse</code> // 反转指定范围的元素</li></ul><h4 id="5-3-1-sort"><a href="#5-3-1-sort" class="headerlink" title="5.3.1 sort"></a>5.3.1 sort</h4><p><strong>功能描述:</strong></p><ul><li>对容器内元素进行排序</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>sort(iterator beg, iterator end, _Pred); </code></p><p>// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// _Pred 谓词</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">myPrint</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span> </span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br><span class="hljs-comment">//sort默认从小到大排序</span><br><span class="hljs-built_in">sort</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>());<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), myPrint);<br>cout << endl;<br><br><span class="hljs-comment">//从大到小排序</span><br><span class="hljs-built_in">sort</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), greater<<span class="hljs-keyword">int</span>>());<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), myPrint);<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>sort属于开发中最常用的算法之一,需熟练掌握</p><h4 id="5-3-2-random-shuffle"><a href="#5-3-2-random-shuffle" class="headerlink" title="5.3.2 random_shuffle"></a>5.3.2 random_shuffle</h4><p><strong>功能描述:</strong></p><ul><li>洗牌 指定范围内的元素随机调整次序</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>random_shuffle(iterator beg, iterator end); </code></p><p>// 指定范围内的元素随机调整次序</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><ctime></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-built_in">srand</span>((<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span>)<span class="hljs-built_in">time</span>(<span class="hljs-literal">NULL</span>));<br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span> ; i < <span class="hljs-number">10</span>;i++)<br>{<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br><br><span class="hljs-comment">//打乱顺序</span><br><span class="hljs-built_in">random_shuffle</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>());<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>random_shuffle洗牌算法比较实用,使用时记得加随机数种子</p><h4 id="5-3-3-merge"><a href="#5-3-3-merge" class="headerlink" title="5.3.3 merge"></a>5.3.3 merge</h4><p><strong>功能描述:</strong></p><ul><li>两个容器元素合并,并存储到另一容器中</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); </code></p><p>// 容器元素合并,并存储到另一容器中</p><p>// 注意: 两个容器必须是<strong>有序的</strong></p><p>// beg1 容器1开始迭代器<br>// end1 容器1结束迭代器<br>// beg2 容器2开始迭代器<br>// end2 容器2结束迭代器<br>// dest 目标容器开始迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br>vector<<span class="hljs-keyword">int</span>> v2;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span> ; i++) <br> {<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>v2.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">1</span>);<br>}<br><br>vector<<span class="hljs-keyword">int</span>> vtarget;<br><span class="hljs-comment">//目标容器需要提前开辟空间</span><br>vtarget.<span class="hljs-built_in">resize</span>(v1.<span class="hljs-built_in">size</span>() + v2.<span class="hljs-built_in">size</span>());<br><span class="hljs-comment">//合并 需要两个有序序列</span><br><span class="hljs-built_in">merge</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), vtarget.<span class="hljs-built_in">begin</span>());<br>for_each(vtarget.<span class="hljs-built_in">begin</span>(), vtarget.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>merge合并的两个容器必须的有序序列</p><h4 id="5-3-4-reverse"><a href="#5-3-4-reverse" class="headerlink" title="5.3.4 reverse"></a>5.3.4 reverse</h4><p><strong>功能描述:</strong></p><ul><li>将容器内元素进行反转</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>reverse(iterator beg, iterator end); </code></p><p>// 反转指定范围的元素</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br><br>cout << <span class="hljs-string">"反转前: "</span> << endl;<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br><br>cout << <span class="hljs-string">"反转后: "</span> << endl;<br><br><span class="hljs-built_in">reverse</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>());<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>reverse反转区间内元素,面试题可能涉及到</p><h3 id="5-4-常用拷贝和替换算法"><a href="#5-4-常用拷贝和替换算法" class="headerlink" title="5.4 常用拷贝和替换算法"></a>5.4 常用拷贝和替换算法</h3><p><strong>学习目标:</strong></p><ul><li>掌握常用的拷贝和替换算法</li></ul><p><strong>算法简介:</strong></p><ul><li><code>copy</code> // 容器内指定范围的元素拷贝到另一容器中</li><li><code>replace</code> // 将容器内指定范围的旧元素修改为新元素</li><li><code>replace_if </code> // 容器内指定范围满足条件的元素替换为新元素</li><li><code>swap</code> // 互换两个容器的元素</li></ul><h4 id="5-4-1-copy"><a href="#5-4-1-copy" class="headerlink" title="5.4.1 copy"></a>5.4.1 copy</h4><p><strong>功能描述:</strong></p><ul><li>容器内指定范围的元素拷贝到另一容器中</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>copy(iterator beg, iterator end, iterator dest); </code></p><p>// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// dest 目标起始迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {<br>v1.<span class="hljs-built_in">push_back</span>(i + <span class="hljs-number">1</span>);<br>}<br>vector<<span class="hljs-keyword">int</span>> v2;<br>v2.<span class="hljs-built_in">resize</span>(v1.<span class="hljs-built_in">size</span>());<br><span class="hljs-built_in">copy</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), v2.<span class="hljs-built_in">begin</span>());<br><br>for_each(v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>利用copy算法在拷贝时,目标容器记得提前开辟空间</p><h4 id="5-4-2-replace"><a href="#5-4-2-replace" class="headerlink" title="5.4.2 replace"></a>5.4.2 replace</h4><p><strong>功能描述:</strong></p><ul><li>将容器内指定范围的旧元素修改为新元素</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>replace(iterator beg, iterator end, oldvalue, newvalue); </code></p><p>// 将区间内旧元素 替换成 新元素</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// oldvalue 旧元素</p><p>// newvalue 新元素</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br><br>cout << <span class="hljs-string">"替换前:"</span> << endl;<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br><br><span class="hljs-comment">//将容器中的20 替换成 2000</span><br>cout << <span class="hljs-string">"替换后:"</span> << endl;<br><span class="hljs-built_in">replace</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-number">20</span>,<span class="hljs-number">2000</span>);<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>replace会替换区间内满足条件的元素</p><h4 id="5-4-3-replace-if"><a href="#5-4-3-replace-if" class="headerlink" title="5.4.3 replace_if"></a>5.4.3 replace_if</h4><p><strong>功能描述:</strong> </p><ul><li>将区间内满足条件的元素,替换成指定元素</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>replace_if(iterator beg, iterator end, _pred, newvalue); </code></p><p>// 按条件替换元素,满足条件的替换成指定元素</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// _pred 谓词</p><p>// newvalue 替换的新元素</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReplaceGreater30</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">return</span> val >= <span class="hljs-number">30</span>;<br>}<br><br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">30</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">40</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">50</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">10</span>);<br>v.<span class="hljs-built_in">push_back</span>(<span class="hljs-number">20</span>);<br><br>cout << <span class="hljs-string">"替换前:"</span> << endl;<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br><br><span class="hljs-comment">//将容器中大于等于的30 替换成 3000</span><br>cout << <span class="hljs-string">"替换后:"</span> << endl;<br><span class="hljs-built_in">replace_if</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">ReplaceGreater30</span>(), <span class="hljs-number">3000</span>);<br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>replace_if按条件查找,可以利用仿函数灵活筛选满足的条件</p><h4 id="5-4-4-swap"><a href="#5-4-4-swap" class="headerlink" title="5.4.4 swap"></a>5.4.4 swap</h4><p><strong>功能描述:</strong></p><ul><li>互换两个容器的元素</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>swap(container c1, container c2); </code></p><p>// 互换两个容器的元素</p><p>// c1容器1</p><p>// c2容器2</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br>vector<<span class="hljs-keyword">int</span>> v2;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>v2.<span class="hljs-built_in">push_back</span>(i+<span class="hljs-number">100</span>);<br>}<br><br>cout << <span class="hljs-string">"交换前: "</span> << endl;<br>for_each(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>for_each(v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br><br>cout << <span class="hljs-string">"交换后: "</span> << endl;<br><span class="hljs-built_in">swap</span>(v1, v2);<br>for_each(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>for_each(v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>swap交换容器时,注意交换的容器要同种类型</p><h3 id="5-5-常用算术生成算法"><a href="#5-5-常用算术生成算法" class="headerlink" title="5.5 常用算术生成算法"></a>5.5 常用算术生成算法</h3><p><strong>学习目标:</strong></p><ul><li>掌握常用的算术生成算法</li></ul><p><strong>注意:</strong></p><ul><li>算术生成算法属于小型算法,使用时包含的头文件为 <code>#include <numeric></code></li></ul><p><strong>算法简介:</strong></p><ul><li><p><code>accumulate</code> // 计算容器元素累计总和</p></li><li><p><code>fill</code> // 向容器中添加元素</p></li></ul><h4 id="5-5-1-accumulate"><a href="#5-5-1-accumulate" class="headerlink" title="5.5.1 accumulate"></a>5.5.1 accumulate</h4><p><strong>功能描述:</strong></p><ul><li> 计算区间内 容器元素累计总和</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>accumulate(iterator beg, iterator end, value); </code></p><p>// 计算容器元素累计总和</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// value 起始值</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><numeric></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i <= <span class="hljs-number">100</span>; i++) {<br>v.<span class="hljs-built_in">push_back</span>(i);<br>}<br><br><span class="hljs-keyword">int</span> total = <span class="hljs-built_in">accumulate</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-number">0</span>);<br><br>cout << <span class="hljs-string">"total = "</span> << total << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>accumulate使用时头文件注意是 numeric,这个算法很实用</p><h4 id="5-5-2-fill"><a href="#5-5-2-fill" class="headerlink" title="5.5.2 fill"></a>5.5.2 fill</h4><p><strong>功能描述:</strong></p><ul><li>向容器中填充指定的元素</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>fill(iterator beg, iterator end, value); </code></p><p>// 向容器中填充元素</p><p>// beg 开始迭代器</p><p>// end 结束迭代器</p><p>// value 填充的值</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><numeric></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><br>vector<<span class="hljs-keyword">int</span>> v;<br>v.<span class="hljs-built_in">resize</span>(<span class="hljs-number">10</span>);<br><span class="hljs-comment">//填充</span><br><span class="hljs-built_in">fill</span>(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-number">100</span>);<br><br>for_each(v.<span class="hljs-built_in">begin</span>(), v.<span class="hljs-built_in">end</span>(), <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong>利用fill可以将容器区间内元素填充为 指定的值</p><h3 id="5-6-常用集合算法"><a href="#5-6-常用集合算法" class="headerlink" title="5.6 常用集合算法"></a>5.6 常用集合算法</h3><p><strong>学习目标:</strong></p><ul><li>掌握常用的集合算法</li></ul><p><strong>算法简介:</strong></p><ul><li><p><code>set_intersection</code> // 求两个容器的交集</p></li><li><p><code>set_union</code> // 求两个容器的并集</p></li><li><p><code>set_difference </code> // 求两个容器的差集</p></li></ul><h4 id="5-6-1-set-intersection"><a href="#5-6-1-set-intersection" class="headerlink" title="5.6.1 set_intersection"></a>5.6.1 set_intersection</h4><p><strong>功能描述:</strong></p><ul><li>求两个容器的交集</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); </code></p><p>// 求两个集合的交集</p><p>// <strong>注意:两个集合必须是有序序列</strong></p><p>// beg1 容器1开始迭代器<br>// end1 容器1结束迭代器<br>// beg2 容器2开始迭代器<br>// end2 容器2结束迭代器<br>// dest 目标容器开始迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br>vector<<span class="hljs-keyword">int</span>> v2;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++)<br> {<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>v2.<span class="hljs-built_in">push_back</span>(i+<span class="hljs-number">5</span>);<br>}<br><br>vector<<span class="hljs-keyword">int</span>> vTarget;<br><span class="hljs-comment">//取两个里面较小的值给目标容器开辟空间</span><br>vTarget.<span class="hljs-built_in">resize</span>(<span class="hljs-built_in">min</span>(v1.<span class="hljs-built_in">size</span>(), v2.<span class="hljs-built_in">size</span>()));<br><br><span class="hljs-comment">//返回目标容器的最后一个元素的迭代器地址</span><br>vector<<span class="hljs-keyword">int</span>>::iterator itEnd = <br> <span class="hljs-built_in">set_intersection</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), vTarget.<span class="hljs-built_in">begin</span>());<br><br>for_each(vTarget.<span class="hljs-built_in">begin</span>(), itEnd, <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong> </p><ul><li>求交集的两个集合必须的有序序列</li><li>目标容器开辟空间需要从<strong>两个容器中取小值</strong></li><li>set_intersection返回值既是交集中最后一个元素的位置</li></ul><h4 id="5-6-2-set-union"><a href="#5-6-2-set-union" class="headerlink" title="5.6.2 set_union"></a>5.6.2 set_union</h4><p><strong>功能描述:</strong></p><ul><li>求两个集合的并集</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); </code></p><p>// 求两个集合的并集</p><p>// <strong>注意:两个集合必须是有序序列</strong></p><p>// beg1 容器1开始迭代器<br>// end1 容器1结束迭代器<br>// beg2 容器2开始迭代器<br>// end2 容器2结束迭代器<br>// dest 目标容器开始迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br>vector<<span class="hljs-keyword">int</span>> v2;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>v2.<span class="hljs-built_in">push_back</span>(i+<span class="hljs-number">5</span>);<br>}<br><br>vector<<span class="hljs-keyword">int</span>> vTarget;<br><span class="hljs-comment">//取两个容器的和给目标容器开辟空间</span><br>vTarget.<span class="hljs-built_in">resize</span>(v1.<span class="hljs-built_in">size</span>() + v2.<span class="hljs-built_in">size</span>());<br><br><span class="hljs-comment">//返回目标容器的最后一个元素的迭代器地址</span><br>vector<<span class="hljs-keyword">int</span>>::iterator itEnd = <br> <span class="hljs-built_in">set_union</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), vTarget.<span class="hljs-built_in">begin</span>());<br><br>for_each(vTarget.<span class="hljs-built_in">begin</span>(), itEnd, <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong> </p><ul><li>求并集的两个集合必须的有序序列</li><li>目标容器开辟空间需要<strong>两个容器相加</strong></li><li>set_union返回值既是并集中最后一个元素的位置</li></ul><h4 id="5-6-3-set-difference"><a href="#5-6-3-set-difference" class="headerlink" title="5.6.3 set_difference"></a>5.6.3 set_difference</h4><p><strong>功能描述:</strong></p><ul><li>求两个集合的差集</li></ul><p><strong>函数原型:</strong></p><ul><li><p><code>set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); </code></p><p>// 求两个集合的差集</p><p>// <strong>注意:两个集合必须是有序序列</strong></p><p>// beg1 容器1开始迭代器<br>// end1 容器1结束迭代器<br>// beg2 容器2开始迭代器<br>// end2 容器2结束迭代器<br>// dest 目标容器开始迭代器</p></li></ul><p><strong>示例:</strong></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm></span></span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">myPrint</span></span><br><span class="hljs-class">{</span><br><span class="hljs-keyword">public</span>:<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">(<span class="hljs-keyword">int</span> val)</span></span><br><span class="hljs-function"></span>{<br>cout << val << <span class="hljs-string">" "</span>;<br>}<br>};<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">test01</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br>vector<<span class="hljs-keyword">int</span>> v1;<br>vector<<span class="hljs-keyword">int</span>> v2;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {<br>v1.<span class="hljs-built_in">push_back</span>(i);<br>v2.<span class="hljs-built_in">push_back</span>(i+<span class="hljs-number">5</span>);<br>}<br><br>vector<<span class="hljs-keyword">int</span>> vTarget;<br><span class="hljs-comment">//取两个里面较大的值给目标容器开辟空间</span><br>vTarget.<span class="hljs-built_in">resize</span>( <span class="hljs-built_in">max</span>(v1.<span class="hljs-built_in">size</span>() , v2.<span class="hljs-built_in">size</span>()));<br><br><span class="hljs-comment">//返回目标容器的最后一个元素的迭代器地址</span><br>cout << <span class="hljs-string">"v1与v2的差集为: "</span> << endl;<br>vector<<span class="hljs-keyword">int</span>>::iterator itEnd = <br> <span class="hljs-built_in">set_difference</span>(v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), vTarget.<span class="hljs-built_in">begin</span>());<br>for_each(vTarget.<span class="hljs-built_in">begin</span>(), itEnd, <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br><br><br>cout << <span class="hljs-string">"v2与v1的差集为: "</span> << endl;<br>itEnd = <span class="hljs-built_in">set_difference</span>(v2.<span class="hljs-built_in">begin</span>(), v2.<span class="hljs-built_in">end</span>(), v1.<span class="hljs-built_in">begin</span>(), v1.<span class="hljs-built_in">end</span>(), vTarget.<span class="hljs-built_in">begin</span>());<br>for_each(vTarget.<span class="hljs-built_in">begin</span>(), itEnd, <span class="hljs-built_in">myPrint</span>());<br>cout << endl;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br><span class="hljs-built_in">test01</span>();<br><br><span class="hljs-built_in">system</span>(<span class="hljs-string">"pause"</span>);<br><br><span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><p><strong>总结:</strong> </p><ul><li>求差集的两个集合必须的有序序列</li><li>目标容器开辟空间需要从<strong>两个容器取较大值</strong></li><li>set_difference返回值既是差集中最后一个元素的位置</li></ul>]]></content>
</entry>
<entry>
<title>算法笔记本</title>
<link href="/2021/01/09/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0%E6%9C%AC/"/>
<url>/2021/01/09/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0%E6%9C%AC/</url>
<content type="html"><![CDATA[<h1 id="算法笔记本"><a href="#算法笔记本" class="headerlink" title="算法笔记本"></a><strong>算法笔记本</strong></h1><h2 id="基础算法"><a href="#基础算法" class="headerlink" title="基础算法"></a>基础算法</h2><blockquote><p>得背下来</p></blockquote><p>[收藏的网站]([<a href="https://www.cnblogs.com/cao-lei/p/6742815.html">内排序]八大经典排序合集 - C3Stones - 博客园 (cnblogs.com)</a>)</p><h3 id="快排"><a href="#快排" class="headerlink" title="快排"></a>快排</h3><blockquote><p>分治思想</p><p>不同于常规的,不是一次排序一个数,而是直接将某个数的俩测排好</p></blockquote><p><img src="https://xiaomai-aliyunoss.oss-cn-shenzhen.aliyuncs.com/img/20211118170025.jpg" alt="1258817-20190325195811497-310078615"></p><p><img src="https://xiaomai-aliyunoss.oss-cn-shenzhen.aliyuncs.com/img/20211118170836.gif" alt="5b56d02d73d145ca9be2c0431f1f6d68_tplv-k3u1fbpfcp-watermark (1)"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;<br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N = <span class="hljs-number">1e6</span> + <span class="hljs-number">10</span>;<br><br><span class="hljs-keyword">int</span> n;<br><span class="hljs-keyword">int</span> q[N];<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">quick_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> q[],<span class="hljs-keyword">int</span> left,<span class="hljs-keyword">int</span> right)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">if</span>(left>=right) <span class="hljs-keyword">return</span> ;<br> <span class="hljs-keyword">int</span> x=q[(left+right+<span class="hljs-number">1</span>)/<span class="hljs-number">2</span>],i=left<span class="hljs-number">-1</span>,j=right+<span class="hljs-number">1</span>;<span class="hljs-comment">//可换成x=q[(left+right)/2]</span><br> <span class="hljs-keyword">while</span>(i<j)<br> {<br> <span class="hljs-keyword">do</span> i++ ;<span class="hljs-keyword">while</span>(q[i]<x);<br> <span class="hljs-keyword">do</span> j-- ;<span class="hljs-keyword">while</span>(q[j]>x); <br> <span class="hljs-keyword">if</span>(i<j) swap(q[i],q[j]);<br> }<br> <br> quick_sort(q,left,i<span class="hljs-number">-1</span>);<span class="hljs-comment">//可换成j</span><br> quick_sort(q,i,right);<span class="hljs-comment">//可换成j-1</span><br> <br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&n);<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++) <br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&q[i]);<br> <br> quick_sort(q,<span class="hljs-number">0</span>,n<span class="hljs-number">-1</span>);<br> <br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++)<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>,q[i]);<br> <br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h3 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a><strong>归并排序</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;<br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">1000010</span>;<br><br><span class="hljs-keyword">int</span> n;<br><span class="hljs-keyword">int</span> q[N],temp[N];<br><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">merge_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> q[],<span class="hljs-keyword">int</span> left,<span class="hljs-keyword">int</span> right)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span>(left>=right) <span class="hljs-keyword">return</span> ;<br> @WWWWWWWW*x<br> <span class="hljs-keyword">int</span> mid=(left+right)/<span class="hljs-number">2</span>;[p;<span class="hljs-string">'p-=l.;;;;76'</span>]<br> <br> merge_sort(q,left,mid), merge_sort(q,mid+<span class="hljs-number">1</span>,right);<br> <br> <span class="hljs-keyword">int</span> x=<span class="hljs-number">0</span>,i=left,j=mid+<span class="hljs-number">1</span>;<br> <br> <span class="hljs-keyword">while</span>(i<=mid&&j<=right)<span class="hljs-comment">//注意这里的 <=</span><br> <span class="hljs-keyword">if</span>(q[i]<=q[j]) temp[x++]=q[i++];<span class="hljs-comment">//注意这里的 <=</span><br> <span class="hljs-keyword">else</span> temp[x++]=q[j++];<br> <span class="hljs-keyword">while</span>(i<=mid) temp[x++]=q[i++];<span class="hljs-comment">//注意这里的 <=</span><br> <span class="hljs-keyword">while</span>(j<=right) temp[x++]=q[j++];<span class="hljs-comment">//注意这里的 <=</span><br> <br> <span class="hljs-keyword">for</span>(i=left,j=<span class="hljs-number">0</span>;i<=right;i++,j++)<span class="hljs-comment">//注意这里的 <=</span><br> q[i]=temp[j];<br> <br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&n);<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++)<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&q[i]);<br> <br> merge_sort(q,<span class="hljs-number">0</span>,n<span class="hljs-number">-1</span>);<br> <br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++)<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>,q[i]);<br>}<br></code></pre></td></tr></table></figure><h3 id="二分"><a href="#二分" class="headerlink" title="二分"></a><strong>二分</strong></h3><h3 id="时间复杂度"><a href="#时间复杂度" class="headerlink" title="时间复杂度"></a><strong>时间复杂度</strong></h3><blockquote><p> 一般ACM或者笔试题的时间限制是1秒或2秒。<br>在这种情况下,C++代码中的操作次数控制在 107∼108107∼108 为最佳。</p></blockquote><p>下面给出在不同数据范围下,代码的时间复杂度和算法该如何选择:</p><ul><li>n≤30n≤30, 指数级别, dfs+剪枝,状态压缩dp</li><li>n≤100n≤100 => O(n3)O(n3),floyd,dp,高斯消元</li><li>n≤1000n≤1000 => O(n2)O(n2),O(n2logn)O(n2logn),dp,二分,朴素版Dijkstra、朴素版Prim、Bellman-Ford</li><li>n≤10000n≤10000 => O(n∗n√)O(n∗n),块状链表、分块、莫队</li><li>n≤100000n≤100000 => O(nlogn)O(nlogn) => 各种sort,线段树、树状数组、set/map、heap、拓扑排序、dijkstra+heap、prim+heap、Kruskal、spfa、求凸包、求半平面交、二分、CDQ分治、整体二分、后缀数组、树链剖分、动态树</li><li>n≤1000000n≤1000000 => O(n)O(n), 以及常数较小的 O(nlogn)O(nlogn) 算法 => 单调队列、 hash、双指针扫描、并查集,kmp、AC自动机,常数比较小的 O(nlogn)O(nlogn) 的做法:sort、树状数组、heap、dijkstra、spfa</li><li>n≤10000000n≤10000000 => O(n)O(n),双指针扫描、kmp、AC自动机、线性筛素数</li><li>n≤109n≤109 => O(n√)O(n),判断质数</li><li>n≤1018n≤1018 => O(logn)O(logn),最大公约数,快速幂,数位DP</li><li>n≤101000n≤101000 => O((logn)2)O((logn)2),高精度加减乘除</li><li>n≤10100000n≤10100000 => O(logk×loglogk),k表示位数O(logk×loglogk),k表示位数,高精度加减、FFT/NTT</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-number">1</span>插入排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insert_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < n; i ++ )<br> {<br> <span class="hljs-keyword">int</span> x = a[i];<br> <span class="hljs-keyword">int</span> j = i<span class="hljs-number">-1</span>;<br><br> <span class="hljs-keyword">while</span> (j >= <span class="hljs-number">0</span> && x < a[j])<br> {<br> a[j+<span class="hljs-number">1</span>] = a[j];<br> j -- ;<br> }<br> a[j+<span class="hljs-number">1</span>] = x;<br> }<br>}<br><span class="hljs-number">2</span>选择排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">select_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; i ++ )<br> {<br> <span class="hljs-keyword">int</span> k = i;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i+<span class="hljs-number">1</span>; j < n; j ++ )<br> {<br> <span class="hljs-keyword">if</span> (a[j] < a[k])<br> k = j;<br> }<br> swap(a[i], a[k]);<br> }<br><br>}<br><span class="hljs-number">3</span>冒泡排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">bubble_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n<span class="hljs-number">-1</span>; i >= <span class="hljs-number">1</span>; i -- )<br> {<br> <span class="hljs-keyword">bool</span> flag = <span class="hljs-literal">true</span>;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j <= i; j ++ )<br> <span class="hljs-keyword">if</span> (a[j<span class="hljs-number">-1</span>] > a[j])<br> {<br> swap(a[j<span class="hljs-number">-1</span>], a[j]);<br> flag = <span class="hljs-literal">false</span>;<br> }<br> <span class="hljs-keyword">if</span> (flag) <span class="hljs-keyword">return</span>;<br> }<br>}<br><span class="hljs-number">4</span>希尔排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">shell_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> gap = n >> <span class="hljs-number">1</span>; gap; gap >>= <span class="hljs-number">1</span>)<br> {<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = gap; i < n; i ++ )<br> {<br> <span class="hljs-keyword">int</span> x = a[i];<br> <span class="hljs-keyword">int</span> j;<br> <span class="hljs-keyword">for</span> (j = i; j >= gap && a[j-gap] > x; j -= gap)<br> a[j] = a[j-gap];<br> a[j] = x;<br> }<br> }<br>}<br><span class="hljs-number">5</span>快速排序(最快)<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">quick_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> l, <span class="hljs-keyword">int</span> r)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span> (l >= r) <span class="hljs-keyword">return</span> ;<br><br> <span class="hljs-keyword">int</span> x = a[l+r>><span class="hljs-number">1</span>], i = l<span class="hljs-number">-1</span>, j = r+<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">while</span> (i < j)<br> {<br> <span class="hljs-keyword">while</span> (a[++ i] < x);<br> <span class="hljs-keyword">while</span> (a[-- j] > x);<br> <span class="hljs-keyword">if</span> (i < j) swap(a[i], a[j]);<br> }<br> sort(l, j), sort(j+<span class="hljs-number">1</span>, r);<br>}<br><span class="hljs-number">6</span>归并排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">merge_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> l, <span class="hljs-keyword">int</span> r)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span> (l >= r) <span class="hljs-keyword">return</span>;<br> <span class="hljs-keyword">int</span> temp[N];<br> <span class="hljs-keyword">int</span> mid = l+r>><span class="hljs-number">1</span>;<br> merge_sort(l, mid), merge_sort(mid+<span class="hljs-number">1</span>, r);<br> <span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>, i = l, j = mid+<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">while</span> (i <= mid && j <= r)<br> {<br> <span class="hljs-keyword">if</span> (a[i] < a[j]) temp[k ++ ] = a[i ++ ];<br> <span class="hljs-keyword">else</span> temp[k ++ ] = a[j ++ ];<br><br> }<br> <span class="hljs-keyword">while</span> (i <= mid) temp[k ++ ] = a[i ++ ];<br> <span class="hljs-keyword">while</span> (j <= r) temp[k ++ ] = a[j ++ ];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = l, j = <span class="hljs-number">0</span>; i <= r; i ++ , j ++ ) a[i] = temp[j];<br>}<br><span class="hljs-number">7</span>堆排序(须知此排序为使用了模拟堆,为了使最后一个非叶子节点的编号为n/<span class="hljs-number">2</span>,数组编号从<span class="hljs-number">1</span>开始)<br>https:<span class="hljs-comment">//www.cnblogs.com/wanglei5205/p/8733524.html</span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">down</span><span class="hljs-params">(<span class="hljs-keyword">int</span> u)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> t = u;<br> <span class="hljs-keyword">if</span> (u<<<span class="hljs-number">1</span> <= n && h[u<<<span class="hljs-number">1</span>] < h[t]) t = u<<<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">if</span> ((u<<<span class="hljs-number">1</span>|<span class="hljs-number">1</span>) <= n && h[u<<<span class="hljs-number">1</span>|<span class="hljs-number">1</span>] < h[t]) t = u<<<span class="hljs-number">1</span>|<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">if</span> (u != t)<br> {<br> swap(h[u], h[t]);<br> down(t);<br> }<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= n; i ++ ) <span class="hljs-built_in">cin</span> >> h[i];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n/<span class="hljs-number">2</span>; i; i -- ) down(i);<br> <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>)<br> {<br> <span class="hljs-keyword">if</span> (!n) <span class="hljs-keyword">break</span>;<br> <span class="hljs-built_in">cout</span> << h[<span class="hljs-number">1</span>] << <span class="hljs-string">' '</span>;<br> h[<span class="hljs-number">1</span>] = h[n];<br> n -- ;<br> down(<span class="hljs-number">1</span>);<br> }<br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><span class="hljs-number">8</span>基数排序<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">maxbit</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> maxv = a[<span class="hljs-number">0</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < n; i ++ )<br> <span class="hljs-keyword">if</span> (maxv < a[i])<br> maxv = a[i];<br> <span class="hljs-keyword">int</span> cnt = <span class="hljs-number">1</span>;<br> <span class="hljs-keyword">while</span> (maxv >= <span class="hljs-number">10</span>) maxv /= <span class="hljs-number">10</span>, cnt ++ ;<br><br> <span class="hljs-keyword">return</span> cnt;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">radixsort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> t = maxbit();<br> <span class="hljs-keyword">int</span> radix = <span class="hljs-number">1</span>;<br><br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= t; i ++ )<br> {<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < <span class="hljs-number">10</span>; j ++ ) count[j] = <span class="hljs-number">0</span>;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < n; j ++ )<br> {<br> <span class="hljs-keyword">int</span> k = (a[j] / radix) % <span class="hljs-number">10</span>;<br> count[k] ++ ;<br> }<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j < <span class="hljs-number">10</span>; j ++ ) count[j] += count[j<span class="hljs-number">-1</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = n<span class="hljs-number">-1</span>; j >= <span class="hljs-number">0</span>; j -- )<br> {<br> <span class="hljs-keyword">int</span> k = (a[j] / radix) % <span class="hljs-number">10</span>;<br> temp[count[k]<span class="hljs-number">-1</span>] = a[j];<br> count[k] -- ;<br> }<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < n; j ++ ) a[j] = temp[j];<br> radix *= <span class="hljs-number">10</span>;<br> }<br><br>}<br><span class="hljs-number">9</span>计数排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">counting_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> sorted[N];<br> <span class="hljs-keyword">int</span> maxv = a[<span class="hljs-number">0</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < n; i ++ )<br> <span class="hljs-keyword">if</span> (maxv < a[i])<br> maxv = a[i];<br> <span class="hljs-keyword">int</span> count[maxv+<span class="hljs-number">1</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; i ++ ) count[a[i]] ++ ;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= maxv; i ++ ) count[i] += count[i<span class="hljs-number">-1</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n<span class="hljs-number">-1</span>; i >= <span class="hljs-number">0</span>; i -- )<br> {<br> sorted[count[a[i]]<span class="hljs-number">-1</span>] = a[i];<br> count[a[i]] -- ;<br> }<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; i ++ ) a[i] = sorted[i];<br>}<br><span class="hljs-number">10</span>桶排序(基数排序是桶排序的特例,优势是可以处理浮点数和负数,劣势是还要配合别的排序函数)<br><br><span class="hljs-function"><span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> <span class="hljs-title">bucketSort</span><span class="hljs-params">(<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>& nums)</span> </span>{<br> <span class="hljs-keyword">int</span> n = nums.size();<br> <span class="hljs-keyword">int</span> maxv = *max_element(nums.begin(), nums.end());<br> <span class="hljs-keyword">int</span> minv = *min_element(nums.begin(), nums.end());<br> <span class="hljs-keyword">int</span> bs = <span class="hljs-number">1000</span>;<br> <span class="hljs-keyword">int</span> m = (maxv-minv)/bs+<span class="hljs-number">1</span>;<br> <span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> > bucket(m);<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; ++i) {<br> bucket[(nums[i]-minv)/bs].push_back(nums[i]);<br> }<br> <span class="hljs-keyword">int</span> idx = <span class="hljs-number">0</span>;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < m; ++i) {<br> <span class="hljs-keyword">int</span> sz = bucket[i].size();<br> bucket[i] = quickSort(bucket[i]);<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < sz; ++j) {<br> nums[idx++] = bucket[i][j];<br> }<br> }<br> <span class="hljs-keyword">return</span> nums;<br>}<br><br></code></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>蓝桥杯算法比赛笔记</title>
<link href="/2021/01/09/%E8%93%9D%E6%A1%A5%E6%9D%AF%E7%AC%94%E8%AE%B0%E6%9C%AC/"/>
<url>/2021/01/09/%E8%93%9D%E6%A1%A5%E6%9D%AF%E7%AC%94%E8%AE%B0%E6%9C%AC/</url>
<content type="html"><![CDATA[<h1 id="递归与递推"><a href="#递归与递推" class="headerlink" title="递归与递推"></a>递归与递推</h1><h2 id="递归实现指数型枚举"><a href="#递归实现指数型枚举" class="headerlink" title="递归实现指数型枚举"></a>递归实现指数型枚举</h2><blockquote><p>从 1∼n 这 n 个整数中随机选取任意多个,输出所有可能的选择方案。</p><p>就是判断选和不选</p></blockquote><p><a href="https://www.acwing.com/problem/content/94/">92. 递归实现指数型枚举 - AcWing题库</a></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">16</span>;<br><br><span class="hljs-keyword">int</span> n;<br><br><span class="hljs-keyword">int</span> state[N];<span class="hljs-comment">//0表示无数据,1表示×,2表示√</span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">dfs</span><span class="hljs-params">(<span class="hljs-keyword">int</span> current)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span>(current>n)<span class="hljs-comment">//边界判断,到边界了就把数据输出</span><br> {<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=current;i++)<span class="hljs-comment">//注意等于号</span><br> {<br> <span class="hljs-keyword">if</span>(state[i]==<span class="hljs-number">2</span>)<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>,i);<span class="hljs-comment">//用i表示第i个数的状态</span><br> }<br> <span class="hljs-built_in">puts</span>(<span class="hljs-string">""</span>);<br> <span class="hljs-keyword">return</span>;<br> }<br> <br> state[current]=<span class="hljs-number">1</span>;<br> dfs(current+<span class="hljs-number">1</span>);<br> state[current]=<span class="hljs-number">0</span>;<br> <br> state[current]=<span class="hljs-number">2</span>;<br> dfs(current+<span class="hljs-number">1</span>);<br> state[current]=<span class="hljs-number">0</span>;<br> <br> <span class="hljs-keyword">return</span>;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&n);<br> dfs(<span class="hljs-number">1</span>);<br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h2 id="递归实现排列形枚举"><a href="#递归实现排列形枚举" class="headerlink" title="递归实现排列形枚举"></a>递归实现排列形枚举</h2><blockquote><p> 把 1∼n这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">10</span>;<br><br><span class="hljs-keyword">int</span> n;<br><br><span class="hljs-keyword">int</span> states[N];<br><span class="hljs-keyword">bool</span> used[N];<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">dfs</span><span class="hljs-params">(<span class="hljs-keyword">int</span> current)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span>(current>=n)<br> {<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=current;i++)<br> {<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d"</span>,states[i]);<br> }<br> <span class="hljs-built_in">puts</span>(<span class="hljs-string">""</span>);<br> <span class="hljs-keyword">return</span>;<br> }<br> <br> fot(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=n;i++)<br> {<br> <span class="hljs-keyword">if</span>(!used[i])<br> {<br> states[current]=i;<br> used[i]=<span class="hljs-literal">true</span>;<br> <br> dfs(current+<span class="hljs-number">1</span>);<br> used[i]=flase;<br> states[current]=<span class="hljs-number">0</span>;<br> }<br> }<br> <br> <span class="hljs-keyword">return</span>;<br><br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&n);<br> dfs(<span class="hljs-number">1</span>);<br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h2 id="递归实现组合形枚举"><a href="#递归实现组合形枚举" class="headerlink" title="递归实现组合形枚举"></a>递归实现组合形枚举</h2><blockquote><p>从 1∼n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;<br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">99</span>;<br><br><span class="hljs-keyword">int</span> n;<br><span class="hljs-keyword">int</span> m;<br><br><span class="hljs-keyword">int</span> stats[N];<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">dfs</span><span class="hljs-params">(<span class="hljs-keyword">int</span> current,<span class="hljs-keyword">int</span> start)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span>(current<span class="hljs-number">-1</span> + n-start+<span class="hljs-number">1</span><m) <span class="hljs-keyword">return</span>;<span class="hljs-comment">//剪枝,例如抽到1,5,之后,后面不需要抽了</span><br> <span class="hljs-keyword">if</span>(current==m+<span class="hljs-number">1</span>)<br> {<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<current;i++)<br> {<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>,stats[i]);<br> }<br> <span class="hljs-built_in">puts</span>(<span class="hljs-string">""</span>);<br> <span class="hljs-keyword">return</span>;<br> }<br> <br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=start;i<=n;i++)<br> {<br> stats[current]=i;<br> dfs(current+<span class="hljs-number">1</span>,i+<span class="hljs-number">1</span>);<br> stats[current]=<span class="hljs-number">0</span>;<br> }<br> <span class="hljs-keyword">return</span>;<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %d"</span>,&n,&m);<br> dfs(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>);<br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><br></code></pre></td></tr></table></figure><h1 id="二分"><a href="#二分" class="headerlink" title="二分"></a>二分</h1><blockquote><p>单调的函数都可以二分,可以二分的不一定单调(求数的根号三)</p></blockquote><h2 id="整数二分"><a href="#整数二分" class="headerlink" title="整数二分"></a>整数二分</h2><blockquote><p>整合二分最为复杂,因为有越界的问题</p></blockquote><p>整数二分步骤:<br>1.找一个区间[L,R],使得答案一定在该区间中<br>2找一个判断条件,使得该判断条件具有二段性,并且答案一定是该二段性的分界点。</p><blockquote><p>寻找的值是 符合判断条件时 的边界的值</p><p>及左区间(符合判断条件)的最大值,或者右区间(符合判断条件)的最小值</p></blockquote><p>3.分析终点M在该判断条件下是否成立,如果成立,考虑答案在哪个区间;如果不成立,考虑答案在哪个区间;<br>4.如果更新方式写的是R = Mid(和L=Mid+1),则不用做任何处理;如果更新方式写的是L= Mid(和R=Mid-1),则需要在计算Mid时加上1。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span> l=<span class="hljs-number">0</span>;<br><span class="hljs-keyword">int</span> r=n<span class="hljs-number">-1</span>;<span class="hljs-comment">//注意这个n-</span><br><br><span class="hljs-comment">//形式一</span><br><span class="hljs-keyword">while</span>(l<r)<span class="hljs-comment">//注意这个s <</span><br>{<br> <span class="hljs-keyword">int</span> mid=l+r>><span class="hljs-number">1</span>;<span class="hljs-comment">//>>1代表除以二</span><br> <span class="hljs-keyword">if</span>(a[mid]>=x)<span class="hljs-comment">//注意这个>=</span><br> r=mid;<br> <span class="hljs-keyword">else</span> l=mid+<span class="hljs-number">1</span>;<br>}<br><br><span class="hljs-comment">//形式二</span><br><span class="hljs-keyword">while</span>(l<r)<br>{<br> <span class="hljs-keyword">int</span> mid=l+r+<span class="hljs-number">1</span>>><span class="hljs-number">1</span>;<br> <span class="hljs-keyword">if</span>(a[mid]<=x)<br> l=mid;<br> <span class="hljs-keyword">else</span><br> r=mid<span class="hljs-number">-1</span>;<br>}<br></code></pre></td></tr></table></figure><p>参考典型例题:</p><p> <a href="https://www.acwing.com/problem/content/791/">789. 数的范围 - AcWing题库</a></p><h1 id="基础算法"><a href="#基础算法" class="headerlink" title="基础算法"></a>基础算法</h1><blockquote><p>得背下来</p></blockquote><h2 id="快排"><a href="#快排" class="headerlink" title="快排"></a>快排</h2><blockquote><p>分治思想</p><p>不同于常规的,不是一次排序一个数,而是直接将某个数的俩测排好</p></blockquote><p><img src="https://xiaomai-aliyunoss.oss-cn-shenzhen.aliyuncs.com/img/20211118170025.jpg" alt="1258817-20190325195811497-310078615"></p><p><img src="https://xiaomai-aliyunoss.oss-cn-shenzhen.aliyuncs.com/img/20211118170836.gif" alt="5b56d02d73d145ca9be2c0431f1f6d68_tplv-k3u1fbpfcp-watermark (1)"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;<br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N = <span class="hljs-number">1e6</span> + <span class="hljs-number">10</span>;<br><br><span class="hljs-keyword">int</span> n;<br><span class="hljs-keyword">int</span> q[N];<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">quick_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> q[],<span class="hljs-keyword">int</span> left,<span class="hljs-keyword">int</span> right)</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-keyword">if</span>(left>=right) <span class="hljs-keyword">return</span> ;<br> <span class="hljs-keyword">int</span> x=q[(left+right+<span class="hljs-number">1</span>)/<span class="hljs-number">2</span>],i=left<span class="hljs-number">-1</span>,j=right+<span class="hljs-number">1</span>;<span class="hljs-comment">//可换成x=q[(left+right)/2]</span><br> <span class="hljs-keyword">while</span>(i<j)<br> {<br> <span class="hljs-keyword">do</span> i++ ;<span class="hljs-keyword">while</span>(q[i]<x);<br> <span class="hljs-keyword">do</span> j-- ;<span class="hljs-keyword">while</span>(q[j]>x); <br> <span class="hljs-keyword">if</span>(i<j) swap(q[i],q[j]);<br> }<br> <br> quick_sort(q,left,i<span class="hljs-number">-1</span>);<span class="hljs-comment">//可换成j</span><br> quick_sort(q,i,right);<span class="hljs-comment">//可换成j-1</span><br> <br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br><span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&n);<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++) <br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&q[i]);<br> <br> quick_sort(q,<span class="hljs-number">0</span>,n<span class="hljs-number">-1</span>);<br> <br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++)<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>,q[i]);<br> <br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h2 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a><strong>归并排序</strong></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span><br><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;<br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">1000010</span>;<br><br><span class="hljs-keyword">int</span> n;<br><span class="hljs-keyword">int</span> q[N],temp[N];<br><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">merge_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> q[],<span class="hljs-keyword">int</span> left,<span class="hljs-keyword">int</span> right)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span>(left>=right) <span class="hljs-keyword">return</span> ;<br> @WWWWWWWW*x<br> <span class="hljs-keyword">int</span> mid=(left+right)/<span class="hljs-number">2</span>;[p;<span class="hljs-string">'p-=l.;;;;76'</span>]<br> <br> merge_sort(q,left,mid), merge_sort(q,mid+<span class="hljs-number">1</span>,right);<br> <br> <span class="hljs-keyword">int</span> x=<span class="hljs-number">0</span>,i=left,j=mid+<span class="hljs-number">1</span>;<br> <br> <span class="hljs-keyword">while</span>(i<=mid&&j<=right)<span class="hljs-comment">//注意这里的 <=</span><br> <span class="hljs-keyword">if</span>(q[i]<=q[j]) temp[x++]=q[i++];<span class="hljs-comment">//注意这里的 <=</span><br> <span class="hljs-keyword">else</span> temp[x++]=q[j++];<br> <span class="hljs-keyword">while</span>(i<=mid) temp[x++]=q[i++];<span class="hljs-comment">//注意这里的 <=</span><br> <span class="hljs-keyword">while</span>(j<=right) temp[x++]=q[j++];<span class="hljs-comment">//注意这里的 <=</span><br> <br> <span class="hljs-keyword">for</span>(i=left,j=<span class="hljs-number">0</span>;i<=right;i++,j++)<span class="hljs-comment">//注意这里的 <=</span><br> q[i]=temp[j];<br> <br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&n);<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++)<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>,&q[i]);<br> <br> merge_sort(q,<span class="hljs-number">0</span>,n<span class="hljs-number">-1</span>);<br> <br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<n;i++)<br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>,q[i]);<br>}<br></code></pre></td></tr></table></figure><h2 id="二分-1"><a href="#二分-1" class="headerlink" title="二分"></a><strong>二分</strong></h2><h2 id="排列组合"><a href="#排列组合" class="headerlink" title="排列组合"></a><strong>排列组合</strong></h2><blockquote><p>排列:考虑顺序</p><p>组合:不考虑顺序</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-number">1</span>插入排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insert_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < n; i ++ )<br> {<br> <span class="hljs-keyword">int</span> x = a[i];<br> <span class="hljs-keyword">int</span> j = i<span class="hljs-number">-1</span>;<br><br> <span class="hljs-keyword">while</span> (j >= <span class="hljs-number">0</span> && x < a[j])<br> {<br> a[j+<span class="hljs-number">1</span>] = a[j];<br> j -- ;<br> }<br> a[j+<span class="hljs-number">1</span>] = x;<br> }<br>}<br><span class="hljs-number">2</span>选择排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">select_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; i ++ )<br> {<br> <span class="hljs-keyword">int</span> k = i;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i+<span class="hljs-number">1</span>; j < n; j ++ )<br> {<br> <span class="hljs-keyword">if</span> (a[j] < a[k])<br> k = j;<br> }<br> swap(a[i], a[k]);<br> }<br><br>}<br><span class="hljs-number">3</span>冒泡排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">bubble_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n<span class="hljs-number">-1</span>; i >= <span class="hljs-number">1</span>; i -- )<br> {<br> <span class="hljs-keyword">bool</span> flag = <span class="hljs-literal">true</span>;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j <= i; j ++ )<br> <span class="hljs-keyword">if</span> (a[j<span class="hljs-number">-1</span>] > a[j])<br> {<br> swap(a[j<span class="hljs-number">-1</span>], a[j]);<br> flag = <span class="hljs-literal">false</span>;<br> }<br> <span class="hljs-keyword">if</span> (flag) <span class="hljs-keyword">return</span>;<br> }<br>}<br><span class="hljs-number">4</span>希尔排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">shell_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> gap = n >> <span class="hljs-number">1</span>; gap; gap >>= <span class="hljs-number">1</span>)<br> {<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = gap; i < n; i ++ )<br> {<br> <span class="hljs-keyword">int</span> x = a[i];<br> <span class="hljs-keyword">int</span> j;<br> <span class="hljs-keyword">for</span> (j = i; j >= gap && a[j-gap] > x; j -= gap)<br> a[j] = a[j-gap];<br> a[j] = x;<br> }<br> }<br>}<br><span class="hljs-number">5</span>快速排序(最快)<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">quick_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> l, <span class="hljs-keyword">int</span> r)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span> (l >= r) <span class="hljs-keyword">return</span> ;<br><br> <span class="hljs-keyword">int</span> x = a[l+r>><span class="hljs-number">1</span>], i = l<span class="hljs-number">-1</span>, j = r+<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">while</span> (i < j)<br> {<br> <span class="hljs-keyword">while</span> (a[++ i] < x);<br> <span class="hljs-keyword">while</span> (a[-- j] > x);<br> <span class="hljs-keyword">if</span> (i < j) swap(a[i], a[j]);<br> }<br> sort(l, j), sort(j+<span class="hljs-number">1</span>, r);<br>}<br><span class="hljs-number">6</span>归并排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">merge_sort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> l, <span class="hljs-keyword">int</span> r)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">if</span> (l >= r) <span class="hljs-keyword">return</span>;<br> <span class="hljs-keyword">int</span> temp[N];<br> <span class="hljs-keyword">int</span> mid = l+r>><span class="hljs-number">1</span>;<br> merge_sort(l, mid), merge_sort(mid+<span class="hljs-number">1</span>, r);<br> <span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>, i = l, j = mid+<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">while</span> (i <= mid && j <= r)<br> {<br> <span class="hljs-keyword">if</span> (a[i] < a[j]) temp[k ++ ] = a[i ++ ];<br> <span class="hljs-keyword">else</span> temp[k ++ ] = a[j ++ ];<br><br> }<br> <span class="hljs-keyword">while</span> (i <= mid) temp[k ++ ] = a[i ++ ];<br> <span class="hljs-keyword">while</span> (j <= r) temp[k ++ ] = a[j ++ ];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = l, j = <span class="hljs-number">0</span>; i <= r; i ++ , j ++ ) a[i] = temp[j];<br>}<br><span class="hljs-number">7</span>堆排序(须知此排序为使用了模拟堆,为了使最后一个非叶子节点的编号为n/<span class="hljs-number">2</span>,数组编号从<span class="hljs-number">1</span>开始)<br>https:<span class="hljs-comment">//www.cnblogs.com/wanglei5205/p/8733524.html</span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">down</span><span class="hljs-params">(<span class="hljs-keyword">int</span> u)</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> t = u;<br> <span class="hljs-keyword">if</span> (u<<<span class="hljs-number">1</span> <= n && h[u<<<span class="hljs-number">1</span>] < h[t]) t = u<<<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">if</span> ((u<<<span class="hljs-number">1</span>|<span class="hljs-number">1</span>) <= n && h[u<<<span class="hljs-number">1</span>|<span class="hljs-number">1</span>] < h[t]) t = u<<<span class="hljs-number">1</span>|<span class="hljs-number">1</span>;<br> <span class="hljs-keyword">if</span> (u != t)<br> {<br> swap(h[u], h[t]);<br> down(t);<br> }<br>}<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= n; i ++ ) <span class="hljs-built_in">cin</span> >> h[i];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n/<span class="hljs-number">2</span>; i; i -- ) down(i);<br> <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>)<br> {<br> <span class="hljs-keyword">if</span> (!n) <span class="hljs-keyword">break</span>;<br> <span class="hljs-built_in">cout</span> << h[<span class="hljs-number">1</span>] << <span class="hljs-string">' '</span>;<br> h[<span class="hljs-number">1</span>] = h[n];<br> n -- ;<br> down(<span class="hljs-number">1</span>);<br> }<br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br><span class="hljs-number">8</span>基数排序<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">maxbit</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> maxv = a[<span class="hljs-number">0</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < n; i ++ )<br> <span class="hljs-keyword">if</span> (maxv < a[i])<br> maxv = a[i];<br> <span class="hljs-keyword">int</span> cnt = <span class="hljs-number">1</span>;<br> <span class="hljs-keyword">while</span> (maxv >= <span class="hljs-number">10</span>) maxv /= <span class="hljs-number">10</span>, cnt ++ ;<br><br> <span class="hljs-keyword">return</span> cnt;<br>}<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">radixsort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> t = maxbit();<br> <span class="hljs-keyword">int</span> radix = <span class="hljs-number">1</span>;<br><br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= t; i ++ )<br> {<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < <span class="hljs-number">10</span>; j ++ ) count[j] = <span class="hljs-number">0</span>;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < n; j ++ )<br> {<br> <span class="hljs-keyword">int</span> k = (a[j] / radix) % <span class="hljs-number">10</span>;<br> count[k] ++ ;<br> }<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j < <span class="hljs-number">10</span>; j ++ ) count[j] += count[j<span class="hljs-number">-1</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = n<span class="hljs-number">-1</span>; j >= <span class="hljs-number">0</span>; j -- )<br> {<br> <span class="hljs-keyword">int</span> k = (a[j] / radix) % <span class="hljs-number">10</span>;<br> temp[count[k]<span class="hljs-number">-1</span>] = a[j];<br> count[k] -- ;<br> }<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < n; j ++ ) a[j] = temp[j];<br> radix *= <span class="hljs-number">10</span>;<br> }<br><br>}<br><span class="hljs-number">9</span>计数排序<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">counting_sort</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-keyword">int</span> sorted[N];<br> <span class="hljs-keyword">int</span> maxv = a[<span class="hljs-number">0</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < n; i ++ )<br> <span class="hljs-keyword">if</span> (maxv < a[i])<br> maxv = a[i];<br> <span class="hljs-keyword">int</span> count[maxv+<span class="hljs-number">1</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; i ++ ) count[a[i]] ++ ;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= maxv; i ++ ) count[i] += count[i<span class="hljs-number">-1</span>];<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n<span class="hljs-number">-1</span>; i >= <span class="hljs-number">0</span>; i -- )<br> {<br> sorted[count[a[i]]<span class="hljs-number">-1</span>] = a[i];<br> count[a[i]] -- ;<br> }<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; i ++ ) a[i] = sorted[i];<br>}<br><span class="hljs-number">10</span>桶排序(基数排序是桶排序的特例,优势是可以处理浮点数和负数,劣势是还要配合别的排序函数)<br><br><span class="hljs-function"><span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> <span class="hljs-title">bucketSort</span><span class="hljs-params">(<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>& nums)</span> </span>{<br> <span class="hljs-keyword">int</span> n = nums.size();<br> <span class="hljs-keyword">int</span> maxv = *max_element(nums.begin(), nums.end());<br> <span class="hljs-keyword">int</span> minv = *min_element(nums.begin(), nums.end());<br> <span class="hljs-keyword">int</span> bs = <span class="hljs-number">1000</span>;<br> <span class="hljs-keyword">int</span> m = (maxv-minv)/bs+<span class="hljs-number">1</span>;<br> <span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> > bucket(m);<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; ++i) {<br> bucket[(nums[i]-minv)/bs].push_back(nums[i]);<br> }<br> <span class="hljs-keyword">int</span> idx = <span class="hljs-number">0</span>;<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < m; ++i) {<br> <span class="hljs-keyword">int</span> sz = bucket[i].size();<br> bucket[i] = quickSort(bucket[i]);<br> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < sz; ++j) {<br> nums[idx++] = bucket[i][j];<br> }<br> }<br> <span class="hljs-keyword">return</span> nums;<br>}<br><br></code></pre></td></tr></table></figure><h1 id="细碎知识点"><a href="#细碎知识点" class="headerlink" title="细碎知识点"></a>细碎知识点</h1><h2 id="x的y次方"><a href="#x的y次方" class="headerlink" title="x的y次方"></a>x的y次方</h2><ul><li>#include<math.h>中pow(x,y)表示x的y次方,exp(x)表示e的x次方</li><li>100=1e2=1e+2</li></ul><h2 id="某些函数"><a href="#某些函数" class="headerlink" title="某些函数"></a>某些函数</h2><ul><li>memcpy(b,a,sezeof a) 作用是将a数组的数复制到到b数组里面</li><li>memset(a,0,sezeof a) 作用是将a数组所有字节都变成0</li></ul><h2 id="2的20次方"><a href="#2的20次方" class="headerlink" title="2的20次方"></a>2的20次方</h2><p>2^20^=2^10^ * 2^10^ =1024*1024=1048576≈10^6^=1M bit</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><algorithm></span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;<br><span class="hljs-keyword">int</span> n,r,max_x,max_y;<br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">7009</span>;<br><span class="hljs-keyword">int</span> sum[N][N];<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">print</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-comment">/*for(int i=5;i<=5;i++)</span><br><span class="hljs-comment"> {</span><br><span class="hljs-comment"> for(int j=5;j<=5;j++)</span><br><span class="hljs-comment"> {</span><br><span class="hljs-comment"> cout<<sum[i][j]<<" ";</span><br><span class="hljs-comment"> }</span><br><span class="hljs-comment"> cout<<endl;</span><br><span class="hljs-comment"> }*/</span><br> <span class="hljs-built_in">cout</span><<<span class="hljs-built_in">endl</span>;<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=max_x<span class="hljs-number">-10</span>;i<=max_x+<span class="hljs-number">10</span>;i++)<br> {<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j=max_x<span class="hljs-number">-10</span>;j<=max_x+<span class="hljs-number">10</span>;j++)<br> {<br> <span class="hljs-built_in">cout</span><<sum[i][j]<<<span class="hljs-string">" "</span>;<br> }<br> <br> <span class="hljs-built_in">cout</span><<<span class="hljs-built_in">endl</span>;<br> }<br>}<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>{<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %d"</span>,&n,&r);<br> r=min(<span class="hljs-number">5001</span>,r);<br> max_x=r;<br> max_y=r;<br> <span class="hljs-keyword">while</span>(n--)<br> {<br> <span class="hljs-keyword">int</span> x,y,w;<br> <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %d %d"</span>,&x,&y,&w);<br> <br> sum[x+<span class="hljs-number">1</span>][y+<span class="hljs-number">1</span>]+=w;<br> max_x=max(max_x,x+<span class="hljs-number">1</span>);<br> max_y=max(max_y,y+<span class="hljs-number">1</span>);<br> }<br> <span class="hljs-comment">//print();</span><br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=<span class="hljs-number">5000</span>;i++)<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j=<span class="hljs-number">1</span>;j<=<span class="hljs-number">5000</span>;j++)<br> {<br> <span class="hljs-comment">//sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+sum[i][j];</span><br> sum[i][j] += sum[i - <span class="hljs-number">1</span>][j] + sum[i][j - <span class="hljs-number">1</span>] - sum[i - <span class="hljs-number">1</span>][j - <span class="hljs-number">1</span>];<br> }<br> <br> <span class="hljs-comment">//print();</span><br> <span class="hljs-keyword">int</span> res=<span class="hljs-number">0</span>;<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=<span class="hljs-number">5000</span>;i++)<br> {<br> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j=<span class="hljs-number">1</span>;j<=<span class="hljs-number">5000</span>;j++)<br> {<br> <span class="hljs-keyword">int</span> x1=i,y1=j,x2=i+r<span class="hljs-number">-1</span>,y2=j+r<span class="hljs-number">-1</span>;<br> <span class="hljs-comment">//if(x2>(max_x)||y2>(max_y)) continue;</span><br> res=max(sum[x2][y2]-sum[x2][y1<span class="hljs-number">-1</span>]-sum[x1<span class="hljs-number">-1</span>][y2]+sum[x1<span class="hljs-number">-1</span>][y1<span class="hljs-number">-1</span>],res);<br> <span class="hljs-comment">//if(x2>5001||y2>5001) continue;</span><br> <span class="hljs-comment">//res = max(res, sum[i][j] - sum[i - r][j] - sum[i][j - r] + sum[i - r][j - r]);</span><br> <span class="hljs-comment">//printf("max=%d,x1=%d,y1=%d,x2=%d,y2=%d\n",max,x1,y1,x2,y2);</span><br> <br> }<br> <br> }<br> <span class="hljs-comment">/* printf("max_x=%d,max_y=%d\n",max_x,max_y);</span><br><span class="hljs-comment"> printf("sum[max_x][max_y]=\n%d \n",sum[max_x][max_y]);</span><br><span class="hljs-comment"> printf("sum[5000][5000]=\n%d \n",sum[5000][5000]);*/</span><br> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d"</span>,res);<br> <span class="hljs-comment">//print();</span><br> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></table></figure><h2 id="绝对值"><a href="#绝对值" class="headerlink" title="绝对值"></a>绝对值</h2><p><code>abs()</code>函数</p><h2 id="向上取整"><a href="#向上取整" class="headerlink" title="向上取整"></a>向上取整</h2><p><code>floor(a/b)</code> 和 <code>a/b</code>向下取整</p><p>ceil(double(a/b) 和 (a+b-1)/b 是向上取整</p><h2 id="a-b-返回正数值"><a href="#a-b-返回正数值" class="headerlink" title="a%b 返回正数值"></a>a%b 返回正数值</h2><p>当a,b>0时,<code>a%b<0</code>,<code>(a%b+b)%b>0</code></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c++">cout <<<span class="hljs-number">-4</span>%<span class="hljs-number">3</span><<endl;<span class="hljs-comment">//结果是-1</span><br>cout << (<span class="hljs-number">-4</span> % <span class="hljs-number">3</span>+<span class="hljs-number">3</span>)%<span class="hljs-number">3</span>;<span class="hljs-comment">//结果是2</span><br></code></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>unity快捷键</title>
<link href="/2021/01/08/Unity%E5%BF%AB%E6%8D%B7%E9%94%AE/"/>
<url>/2021/01/08/Unity%E5%BF%AB%E6%8D%B7%E9%94%AE/</url>
<content type="html"><![CDATA[<h1 id="unity快捷键"><a href="#unity快捷键" class="headerlink" title="unity快捷键"></a>unity快捷键</h1><h2 id="一、变换工具-6个"><a href="#一、变换工具-6个" class="headerlink" title="一、变换工具 6个"></a>一、变换工具 6个</h2><p><strong>快捷键Q–Hand(手形)工具 可以平移整个Scene视图</strong></p><p><strong>快捷键W–Translate(移动)工具 移动所选择的游戏对象</strong></p><p><strong>快捷键E–Rotate(旋转)工具 按任意角度旋转游戏对象</strong></p><p><strong>快捷键R–Scale(缩放)工具 缩放选中的游戏对象</strong></p><p>快捷键T–横切面工具 可以沿着横截面缩放,沿着横截面的中心点旋转</p><p>快捷键Z–Center工具 改变游戏对象的轴心点</p><p>快捷键X–Local工具 改变物体的坐标</p><h2 id="二、播放控件-3个"><a href="#二、播放控件-3个" class="headerlink" title="二、播放控件 3个"></a>二、播放控件 3个</h2><p>Ctrl+P–播放按钮 播放/运行,对游戏场景进行预览</p><p>Ctrl+Shift+P–暂停按钮 暂停/中断,停止预览</p><p>Ctrl+Alt+P–逐帧播放按钮 单帧进行预览</p><h2 id="三、File(文件)菜单快捷键-6个"><a href="#三、File(文件)菜单快捷键-6个" class="headerlink" title="三、File(文件)菜单快捷键 6个"></a>三、File(文件)菜单快捷键 6个</h2><p>Ctrl+N–New Scene 新建场景</p><p>Ctrl+O–Open Scene 打开场景</p><p>Ctrl+S–Save Scene 保存场景</p><p>Ctrl+Shift+S–Save Scene as 场景另存为</p><p>Ctrl+Shift+B–Build Setings 发布设置</p><p>Ctrl+B–Build & Run 发布并运行</p><h2 id="四、Edit(编辑)菜单-11个"><a href="#四、Edit(编辑)菜单-11个" class="headerlink" title="四、Edit(编辑)菜单 11个"></a>四、Edit(编辑)菜单 11个</h2><p>(在Mac机中,用command键替换Ctrl,以下出现的Ctrl均同)</p><p><strong>Ctrl+Z–Undo Selection Change 撤销上一步操作</strong></p><p><strong>Ctrl+Y–Redo 反撤销(mac机为command+Shift+Z)</strong></p><p>Ctrl+X–Cut 剪切</p><p>Ctrl+C–Copy 复制</p><p>Ctrl+V–Paste 粘贴</p><p>Ctrl+D–Duplicate 复制并粘贴 </p><p>Shift+Del–Delete 删除(mac机位command+Del)</p><p>快捷键F–Frame Selected 居中并最大化当前选中的物体</p><p>Ctrl+F–Find 搜索,按照名称查找物体</p><p>Shift+F–Lock view to Selected 锁定搜索到的物体</p><p>Ctrl+A–Select All 选择全部</p><h2 id="五、Assets菜单快捷键-1个"><a href="#五、Assets菜单快捷键-1个" class="headerlink" title="五、Assets菜单快捷键 1个"></a>五、Assets菜单快捷键 1个</h2><p>Ctrl+R–Refresh 刷新场景</p><h2 id="六、GameObject菜单快捷键-5个"><a href="#六、GameObject菜单快捷键-5个" class="headerlink" title="六、GameObject菜单快捷键 5个"></a>六、GameObject菜单快捷键 5个</h2><p>Ctrl+Shift+N–Create Empty 创建一个空的游戏对象</p><p>Alt+Shift+N–Create Empty Child 创建一个空的子物体</p><p>Ctrl+Alt+F–Move to New 移动游戏对象到视图的中心点</p><p>Ctrl+Shift+F–Align With View</p><p>Alt+Shift+A–Toggle Active Stave</p><h2 id="七、Component菜单快捷键"><a href="#七、Component菜单快捷键" class="headerlink" title="七、Component菜单快捷键"></a>七、Component菜单快捷键</h2><p>Ctrl+Shift+A–add 添加组件</p><h2 id="八、切换视图快捷键-12个"><a href="#八、切换视图快捷键-12个" class="headerlink" title="八、切换视图快捷键 12个"></a>八、切换视图快捷键 12个</h2><p>Ctrl+Tab切换–next Windows 下个窗口</p><p>Ctrl+Shift+Tab切换–Previous Windows 上个窗口</p><p>Ctrl+1切换–Scene视图</p><p>Ctrl+2切换–Game视图</p><p>Ctrl+3切换–Inspector视图</p><p>Ctrl+4切换–Hierarchy视图</p><p>Ctrl+5切换–Project视图</p><p>Ctrl+6切换–Animation视图</p><p>Ctrl+7切换–Profiler视图</p><p>Ctrl+9切换–Asset Store Unity资源商店</p><p>Ctrl+0切换–Asset Server 资源服务器</p><p>Ctrl+Shift+C切换–Console 控制台</p><p>以上三~八为菜单栏快捷键</p><h2 id="九、操作Scene视图的快捷键"><a href="#九、操作Scene视图的快捷键" class="headerlink" title="九、操作Scene视图的快捷键"></a>九、操作Scene视图的快捷键</h2><p>Alt+鼠标左键–旋转视图</p><p>Alt+鼠标右键–放大缩小视图</p><p>Alt+鼠标中键–移动视图</p><p>鼠标右键+w/s/a/d–向上/下/左/右 飞行浏览视图</p><p>鼠标右键+w/s/a/d+Shift–向上/下/左/右 快速飞行浏览视图</p><h2 id="十、※特好用快捷键※"><a href="#十、※特好用快捷键※" class="headerlink" title="十、※特好用快捷键※"></a><strong>十、※特好用快捷键</strong>※</h2><p>Alt + 文件夹小箭头–展开或关闭所有层级</p>]]></content>
</entry>
<entry>
<title>markdown语法</title>
<link href="/2021/01/07/markdown%E8%AF%AD%E6%B3%95/"/>
<url>/2021/01/07/markdown%E8%AF%AD%E6%B3%95/</url>
<content type="html"><![CDATA[<h1 id="markdown语法"><a href="#markdown语法" class="headerlink" title="markdown语法"></a>markdown语法</h1><p>[官方教程地址](<a href="https://markdown.com.cn/intro.html">Markdown 入门基础 | Markdown 官方教程</a>)</p><h2 id="常用语法"><a href="#常用语法" class="headerlink" title="常用语法"></a>常用语法</h2><h3 id="1-代码块"><a href="#1-代码块" class="headerlink" title="1. 代码块"></a>1. 代码块</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">//代码块</span><br>```C<br></code></pre></td></tr></table></figure><h3 id="2-标题"><a href="#2-标题" class="headerlink" title="2.标题"></a><strong>2.标题</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"># 一级标题<br>## 二级标题<br>...依次类推<br></code></pre></td></tr></table></figure><h3 id="3-图片"><a href="#3-图片" class="headerlink" title="3.图片"></a><strong>3.图片</strong></h3><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs awk">本地图片-> ![本地图片](..<span class="hljs-regexp">/img/</span>壁纸<span class="hljs-number">1</span>.png)<br>联网图片-> ![联网图片](https:<span class="hljs-regexp">//</span>th.wallhaven.cc<span class="hljs-regexp">/small/</span>pk/pkw6y3.jpg)<br></code></pre></td></tr></table></figure><p>本地图片-> <img src="../img/%E5%A3%81%E7%BA%B81.png" alt="本地图片"><br>联网图片-> <img src="https://w.wallhaven.cc/full/pk/wallhaven-pkgkkp.png"></p><h3 id="4-字体格式"><a href="#4-字体格式" class="headerlink" title="4.字体格式"></a><strong>4.字体格式</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">//加粗 Ctrl+B</span><br>**加粗** <br><span class="hljs-comment">//删除</span><br>~~删除~~<br><span class="hljs-comment">//斜体 Ctrl+I</span><br>*斜体*<br></code></pre></td></tr></table></figure><ul><li><strong>加粗</strong> </li><li><del>删除</del></li><li><em>斜体</em></li></ul><h3 id="5-引用"><a href="#5-引用" class="headerlink" title="5.引用"></a><strong>5.引用</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c">> 我只是引用<br></code></pre></td></tr></table></figure><blockquote><p>我只是引用</p></blockquote><h3 id="6-分割线"><a href="#6-分割线" class="headerlink" title="6.分割线"></a><strong>6.分割线</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c">---<br></code></pre></td></tr></table></figure><hr><h3 id="7-无序列表"><a href="#7-无序列表" class="headerlink" title="7.无序列表"></a><strong>7.无序列表</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs c">- 我是无序列表 <br>+ 我是无序列表 <br>* 我是无序列表<br> <br><span class="hljs-number">1.</span> 我是无序列表 <br></code></pre></td></tr></table></figure><ul><li>我是无序列表 </li></ul><ul><li>我是无序列表 </li></ul><ul><li>我是无序列表 </li></ul><ol><li>我是无序列表 </li></ol><h3 id="8-表格"><a href="#8-表格" class="headerlink" title="8.表格"></a><strong>8.表格</strong></h3><blockquote><p>这里建议直接右键 🤭</p><p>或者 Ctrl + T</p></blockquote><table><thead><tr><th align="left">姓名</th><th>学号</th><th>成绩</th></tr></thead><tbody><tr><td align="left"></td><td></td><td></td></tr></tbody></table><h3 id="9-超链接"><a href="#9-超链接" class="headerlink" title="9.超链接"></a><strong>9.超链接</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c">[我是超链接](https:<span class="hljs-comment">//meiyingg.github.io/)</span><br></code></pre></td></tr></table></figure><p><a href="https://meiyingg.github.io/">我是超链接</a></p><h2 id="进阶语法"><a href="#进阶语法" class="headerlink" title="进阶语法"></a><strong>进阶语法</strong></h2><h3 id="1-任务列表"><a href="#1-任务列表" class="headerlink" title="1.任务列表"></a><strong>1.任务列表</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c">- [x] 我是任务列表<br>- [ ] 单击修改完成状态<br></code></pre></td></tr></table></figure><ul><li><p><input disabled="" type="checkbox"> 我是任务列表</p></li><li><p><input disabled="" type="checkbox"> 单击修改完成状态</p></li></ul><h3 id="2-图片细调"><a href="#2-图片细调" class="headerlink" title="2.图片细调"></a><strong>2.图片细调</strong></h3><p><img src="https://img-blog.csdnimg.cn/20200822014538211.png#pic_left" alt="我是居左图片"></p><h2 id="有意思的其他语法"><a href="#有意思的其他语法" class="headerlink" title="有意思的其他语法"></a><strong>有意思的其他语法</strong></h2><h3 id="1-标题模板"><a href="#1-标题模板" class="headerlink" title="1.标题模板"></a><strong>1.标题模板</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs c">---<br>title: 标题<br>date: <span class="hljs-number">2021</span><span class="hljs-number">-1</span><span class="hljs-number">-3</span><br>layout: 还不懂<br>excerpt: 解释用的<br>index_img: 封面地址<br>---<br></code></pre></td></tr></table></figure><blockquote><p>不好展示,开头自带</p></blockquote><h3 id="2-内嵌网页"><a href="#2-内嵌网页" class="headerlink" title="2.内嵌网页"></a><strong>2.内嵌网页</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c"><iframe src=<span class="hljs-string">"https://meiyingg.github.io/"</span> width=<span class="hljs-string">"100%"</span> height=<span class="hljs-string">"800"</span> name=<span class="hljs-string">"topFrame"</span> scrolling=<span class="hljs-string">"yes"</span> noresize=<span class="hljs-string">"noresize"</span> frameborder=<span class="hljs-string">"0"</span> id=<span class="hljs-string">"topFrame"</span>><br> </iframe><br></code></pre></td></tr></table></figure><iframe src="https://meiyingg.github.io/" width="100%" height="800" name="topFrame" scrolling="yes" noresize="noresize" frameborder="0" id="topFrame"> </iframe><h3 id="3-插入目录"><a href="#3-插入目录" class="headerlink" title="3.插入目录"></a><strong>3.插入目录</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c">[TOC]<br></code></pre></td></tr></table></figure><p>[TOC] </p>]]></content>
</entry>
<entry>
<title>期末复习法宝</title>
<link href="/2021/01/03/%E6%9C%9F%E6%9C%AB%E5%A4%8D%E4%B9%A0%E6%B3%95%E5%AE%9D/"/>
<url>/2021/01/03/%E6%9C%9F%E6%9C%AB%E5%A4%8D%E4%B9%A0%E6%B3%95%E5%AE%9D/</url>
<content type="html"><![CDATA[<p style="text-align:center;color:#8EC0E4;font-size:1.5em;font-weight: bold;">期末复习法宝</p><p>普通玩家-><a href="http://www.wesane.com/game/910/">网址入口</a></p><p>视野加成buff版本-></p><iframe src="https://www.mutegame.com/170/" width="110%" height="100%" name="topFrame" scrolling="no" noresize="noresize" frameborder="1" id="topFrame"> </iframe><!--PC版--><div id="SOHUCS" ></div><script charset="utf-8" type="text/javascript" src="https://cy-cdn.kuaizhan.com/upload/changyan.js" ></script><script type="text/javascript">window.changyan.api.config({appid: 'cyvQ9FbUr',conf: 'prod_e9bd67443b6486cbb67b6fbd83c3be60'});</script>]]></content>
</entry>
<entry>
<title>windows cmd 指令集</title>
<link href="/2020/12/21/windows%20cmd%20%E6%8C%87%E4%BB%A4%E9%9B%86/"/>
<url>/2020/12/21/windows%20cmd%20%E6%8C%87%E4%BB%A4%E9%9B%86/</url>
<content type="html"><![CDATA[<h1 id="windows-cmd-指令集"><a href="#windows-cmd-指令集" class="headerlink" title="windows cmd 指令集"></a><strong>windows cmd 指令集</strong></h1><h2 id="目录操作"><a href="#目录操作" class="headerlink" title="目录操作"></a><strong>目录操作</strong></h2><h3 id="切换目录,进入指定文件夹:"><a href="#切换目录,进入指定文件夹:" class="headerlink" title="切换目录,进入指定文件夹:"></a><strong>切换目录,进入指定文件夹:</strong></h3><ul><li>切换磁盘:<code>d:</code>(进入 d 盘)</li><li>切换磁盘和目录:<code>cd /d d:/test</code>(进入 d 盘 test 文件夹)</li><li>进入文件夹:<code>cd \test1\test2</code>(进入 test2 文件夹)</li><li>返回根目录:<code>cd \</code></li><li>回到上级目录:<code>cd ..</code></li><li>新建文件夹:<code>md test</code></li></ul><h3 id="显示目录内容:"><a href="#显示目录内容:" class="headerlink" title="显示目录内容:"></a><strong>显示目录内容:</strong></h3><ul><li>显示目录中文件列表:<code>dir</code></li><li>显示目录结构:<code>tree d:\test</code>(d 盘 test 目录)</li><li>显示当前目录位置:<code>cd</code></li><li>显示指定磁盘的当前目录位置:<code>cd d:</code></li></ul><h3 id="关机、重启、注销、休眠、定时"><a href="#关机、重启、注销、休眠、定时" class="headerlink" title="关机、重启、注销、休眠、定时"></a><strong>关机、重启、注销、休眠、定时</strong></h3><ul><li>关机:<code>shutdown /s</code></li><li>重启:<code>shutdown /r</code></li><li>注销:<code>shutdown /l</code></li><li>休眠:<code>shutdown /h /f</code></li><li>取消关机:<code>shutdown /a</code></li><li>定时关机:<code>shutdown /s /t 3600</code>(3600 秒后关机)</li></ul>]]></content>
</entry>
</search>