<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Markmap</title>
<style>
* {
  margin: 0;
  padding: 0;
}
#mindmap {
  display: block;
  width: 100vw;
  height: 100vh;
}
</style>
<link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/@highlightjs/cdn-assets@11.8.0/styles/default.min.css"><link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/markmap-toolbar@0.15.6/dist/style.css">
</head>
<body>
<svg id="mindmap"></svg>
<script src="https://gcore.jsdelivr.net/npm/d3@7.8.5/dist/d3.min.js"></script><script src="https://gcore.jsdelivr.net/npm/markmap-view@0.15.6/dist/browser/index.js"></script><script src="https://gcore.jsdelivr.net/npm/markmap-toolbar@0.15.6/dist/index.js"></script><script>(r => {
                setTimeout(r);
              })(() => {
  const {
    markmap,
    mm
  } = window;
  const {
    el
  } = markmap.Toolbar.create(mm);
  el.setAttribute('style', 'position:absolute;bottom:20px;right:20px');
  document.body.append(el);
})</script><script>((getMarkmap, getOptions, root2, jsonOptions) => {
                const markmap = getMarkmap();
                window.mm = markmap.Markmap.create(
                  "svg#mindmap",
                  (getOptions || markmap.deriveOptions)(jsonOptions),
                  root2
                );
              })(() => window.markmap,null,{"type":"heading","depth":0,"payload":{"lines":[0,1]},"content":"模板","children":[{"type":"heading","depth":1,"payload":{"lines":[1,2]},"content":"C++函数模板","children":[{"type":"heading","depth":2,"payload":{"lines":[3,4]},"content":"函数模板的概念","children":[{"type":"paragraph","depth":3,"payload":{"lines":[4,6]},"content":"函数模板是用于生成函数的模板。在编译阶段,编译器会根据函数模板的使用情况创建出函数名相同,参数类型由编译器判断的若干函数。<br>\n 通过函数模板创建的函数体相同,不同点在于参数类型。","children":[]}]},{"type":"heading","depth":2,"payload":{"lines":[6,7]},"content":"函数模板的使用","children":[{"type":"blockquote","depth":3,"payload":{"lines":[7,9]},"content":"","children":[{"type":"paragraph","depth":4,"payload":{"lines":[7,9]},"content":"每当在一个编译单元(经过预处理的.cpp文件)中使用了函数模板,则必须在该单元中定义一个与函数模板相对应的函数。<br>\n 因此,建议在头文件中对函数模板进行声明定义。","children":[]}]},{"type":"heading","depth":3,"payload":{"lines":[9,10]},"content":"函数模板的声明:","children":[{"type":"fence","depth":4,"content":"<pre><code class=\"language-c++\"><span class=\"hljs-keyword\">template</span> &lt;<span class=\"hljs-keyword\">typename</span> T&gt; <span class=\"hljs-comment\">// 其中T表示任意类型,参数类型和返回值都可以指定为T</span>\n返回类型 函数名(参数列表);\n{\n  函数体\n}\n</code></pre>\n","children":[],"payload":{"lines":[10,17]}}]},{"type":"heading","depth":3,"payload":{"lines":[18,19]},"content":"示例代码","children":[{"type":"ordered_list","depth":4,"payload":{"lines":[19,20],"startIndex":1},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[19,20],"index":1},"content":"1. 新建一个头文件<code>template.h</code>,文件内容如下:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-h\">#pragma once\n#ifndef _COMPARE_H\n#define _COMPARE_H\n\ntemplate&lt;typename T&gt;\nbool isEqual(T i, T j);\n\ntemplate&lt;typename T&gt;\nbool isEqual(T i, T j)\n{\n    return i == j;\n}\n\n#endif // !_COMPARE_H\n</code></pre>\n","children":[],"payload":{"lines":[20,36]}},{"type":"ordered_list","depth":4,"payload":{"lines":[36,37],"startIndex":2},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[36,37],"index":2},"content":"2. 新建<code>main.cpp</code>,在<code>main.cpp</code>中使用函数模板:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&lt;iostream&gt;</span></span>\n<span class=\"hljs-keyword\">using</span> <span class=\"hljs-keyword\">namespace</span> std;\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&quot;compare.h&quot;</span></span>\n\n<span class=\"hljs-function\"><span class=\"hljs-type\">int</span> <span class=\"hljs-title\">main</span><span class=\"hljs-params\">(<span class=\"hljs-type\">int</span> argc, <span class=\"hljs-type\">char</span> *argv[])</span>\n</span>{\n    cout &lt;&lt; <span class=\"hljs-built_in\">isEqual</span>(<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">1</span>) &lt;&lt; endl;\n    cout &lt;&lt; <span class=\"hljs-built_in\">isEqual</span>(<span class=\"hljs-number\">22</span>, <span class=\"hljs-number\">1</span>) &lt;&lt; endl;\n    cout &lt;&lt; <span class=\"hljs-built_in\">isEqual</span>(<span class=\"hljs-number\">0.1</span>, <span class=\"hljs-number\">0.2</span>) &lt;&lt; endl;\n    cout &lt;&lt; <span class=\"hljs-built_in\">isEqual</span>(<span class=\"hljs-number\">1.0f</span>, <span class=\"hljs-number\">1.0f</span>) &lt;&lt; endl;\n    cout &lt;&lt; <span class=\"hljs-built_in\">isEqual</span>(<span class=\"hljs-string\">&quot;TTT&quot;</span>, <span class=\"hljs-string\">&quot;TTT&quot;</span>) &lt;&lt; endl;\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">0</span>;\n}\n\n</code></pre>\n","children":[],"payload":{"lines":[37,53]}},{"type":"ordered_list","depth":4,"payload":{"lines":[53,54],"startIndex":3},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[53,54],"index":3},"content":"3. 编译运行<code>main.cpp</code>,效果如下","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-terminal\">1\n0\n0\n1\n1\n</code></pre>\n","children":[],"payload":{"lines":[54,61]}}]}]},{"type":"heading","depth":2,"payload":{"lines":[62,63]},"content":"函数模板的特化","children":[{"type":"blockquote","depth":3,"payload":{"lines":[63,65]},"content":"","children":[{"type":"paragraph","depth":4,"payload":{"lines":[63,65]},"content":"函数模板的特化是指在实例化模板时,对特定类型的实参进行特殊处理,即当实参为特定类型时,使用该类型的函数体。<br>\n 特化需要为函数模板添加新的定义,方式如下","children":[]}]},{"type":"fence","depth":3,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-keyword\">template</span> &lt;&gt;\n返回类型 函数名&lt;具体类型&gt;(参数列表)\n{ \n  函数体\n}\n</code></pre>\n","children":[],"payload":{"lines":[65,72]}},{"type":"heading","depth":3,"payload":{"lines":[72,73]},"content":"示例代码","children":[{"type":"ordered_list","depth":4,"payload":{"lines":[73,74],"startIndex":1},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[73,74],"index":1},"content":"1. 修改<code>main.cpp</code>文件,修改成如下代码:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&lt;iostream&gt;</span></span>\n<span class=\"hljs-keyword\">using</span> <span class=\"hljs-keyword\">namespace</span> std;\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&quot;compare.h&quot;</span></span>\n\n<span class=\"hljs-function\"><span class=\"hljs-type\">int</span> <span class=\"hljs-title\">main</span><span class=\"hljs-params\">(<span class=\"hljs-type\">int</span> argc, <span class=\"hljs-type\">char</span> *argv[])</span>\n</span>{   \n    <span class=\"hljs-type\">char</span> str1[<span class=\"hljs-number\">20</span>] = <span class=\"hljs-string\">&quot;Hello&quot;</span>;\n    <span class=\"hljs-type\">char</span> str2[<span class=\"hljs-number\">20</span>] = <span class=\"hljs-string\">&quot;Hello&quot;</span>;\n    <span class=\"hljs-keyword\">if</span> (<span class=\"hljs-built_in\">isEqual</span>(str1, str2)) \n    {\n        cout &lt;&lt; <span class=\"hljs-string\">&quot;Equal&quot;</span> &lt;&lt; endl;\n    }\n    <span class=\"hljs-keyword\">else</span>\n    {\n        cout &lt;&lt; <span class=\"hljs-string\">&quot;Not Equal&quot;</span> &lt;&lt; endl;\n    }\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">0</span>;\n}\n</code></pre>\n","children":[],"payload":{"lines":[74,94]}},{"type":"ordered_list","depth":4,"payload":{"lines":[94,95],"startIndex":2},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[94,95],"index":2},"content":"2. 编译运行<code>main.cpp</code>,效果如下","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-terminal\">Not Equal\n</code></pre>\n","children":[],"payload":{"lines":[95,98]}},{"type":"bullet_list","depth":4,"payload":{"lines":[99,101]},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[99,100]},"content":"原来是<code>isEqual</code>函数模板在比较字符串时,默认使用<code>==</code>运算符,而<code>==</code>运算符比较的是字符串的地址,所以返回了<code>Not Equal</code>。","children":[]},{"type":"list_item","depth":5,"payload":{"lines":[100,101]},"content":"字符串内容比较,应该使用<code>strcmp函数</code>。","children":[]}]},{"type":"ordered_list","depth":4,"payload":{"lines":[101,102],"startIndex":3},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[101,102],"index":3},"content":"3. 修改<code>compare.h</code>文件,添加如下代码:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-keyword\">template</span> &lt;&gt;\n<span class=\"hljs-type\">bool</span> <span class=\"hljs-built_in\">isEqual</span>&lt;<span class=\"hljs-type\">char</span>*&gt;(<span class=\"hljs-type\">char</span>* s1, <span class=\"hljs-type\">char</span>* s2)\n{\n    <span class=\"hljs-keyword\">return</span> (<span class=\"hljs-built_in\">strcmp</span>(s1, s2) == <span class=\"hljs-number\">0</span>);\n}\n</code></pre>\n","children":[],"payload":{"lines":[102,109]}},{"type":"ordered_list","depth":4,"payload":{"lines":[110,111],"startIndex":4},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[110,111],"index":4},"content":"4. 编译运行<code>main.cpp</code>,效果如下","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-terminal\">Equal\n</code></pre>\n","children":[],"payload":{"lines":[111,114]}}]}]}]},{"type":"heading","depth":1,"payload":{"lines":[115,116]},"content":"C++类模板","children":[{"type":"heading","depth":2,"payload":{"lines":[116,117]},"content":"类模板的概念","children":[{"type":"paragraph","depth":3,"payload":{"lines":[117,119]},"content":"类模板是用于生成类的模板。在编译阶段,编译器会根据类模板的使用情况创建出仅有部分成员的数据类型和部分成员函数参数类型不同,其他完全相同的若干类。<br>\n 通过类模板,可以尝试写出用于存放不同类型数据的容器。","children":[]}]},{"type":"heading","depth":2,"payload":{"lines":[119,120]},"content":"类模板的使用","children":[{"type":"heading","depth":3,"payload":{"lines":[120,121]},"content":"类模板的声明","children":[{"type":"blockquote","depth":4,"payload":{"lines":[121,123]},"content":"","children":[{"type":"paragraph","depth":5,"payload":{"lines":[121,123]},"content":"类模板的声明和函数模板的声明类似,只是类模板的声明中包含了一个模板参数列表,用于指定模板参数<br>\n类模板的声明如下,其中T代表任意类型,由用户指定","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-keyword\">template</span> &lt;<span class=\"hljs-keyword\">typename</span> T,...&gt;\n<span class=\"hljs-keyword\">class</span> 类名\n{\n    类成员\n}\n</code></pre>\n","children":[],"payload":{"lines":[123,130]}},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-keyword\">template</span> &lt;<span class=\"hljs-keyword\">typename</span> T,...&gt;\n返回类型 类名&lt;T,...&gt;::函数名(参数列表)\n{\n    函数体\n}\n</code></pre>\n","children":[],"payload":{"lines":[131,138]}}]},{"type":"heading","depth":3,"payload":{"lines":[138,139]},"content":"实例代码","children":[{"type":"ordered_list","depth":4,"payload":{"lines":[139,140],"startIndex":1},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[139,140],"index":1},"content":"1. 编写通用数组类,创建<code>MyArray.h</code>头文件,添加如下代码:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-h\">#pragma once\n#ifndef _MYARRAY_H\n#define _MYARRAY_H\n\ntemplate &lt;typename T&gt;\nclass MyArray\n{\nprivate:\n    T data[20];\n    int len;\npublic:\n    MyArray();\n    T indexof(int index);\n    void addValue(T value);\n};\n\ntemplate &lt;typename T&gt;\nMyArray&lt;T&gt;::MyArray() : len(0)\n{\n}\n\ntemplate &lt;typename T&gt;\nT MyArray&lt;T&gt;::indexof(int index)\n{\n    return data[index];\n}\n\ntemplate &lt;typename T&gt;\nvoid MyArray&lt;T&gt;::addValue(T value)\n{\n    data[len] = value;\n    len++;\n}\n\n#endif // !_MYARRAY_H\n</code></pre>\n","children":[],"payload":{"lines":[140,177]}},{"type":"ordered_list","depth":4,"payload":{"lines":[177,178],"startIndex":2},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[177,178],"index":2},"content":"2. 测试整形数组,创建<code>main.cpp</code>源文件,添加如下代码:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&lt;iostream&gt;</span></span>\n<span class=\"hljs-keyword\">using</span> <span class=\"hljs-keyword\">namespace</span> std;\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&quot;MyArray.h&quot;</span></span>\n\n<span class=\"hljs-function\"><span class=\"hljs-type\">int</span> <span class=\"hljs-title\">main</span><span class=\"hljs-params\">(<span class=\"hljs-type\">int</span> argc, <span class=\"hljs-type\">char</span> *argv[])</span>\n</span>{   \n    MyArray&lt;<span class=\"hljs-type\">int</span>&gt; arr;\n    <span class=\"hljs-keyword\">for</span> (<span class=\"hljs-type\">int</span> i = <span class=\"hljs-number\">0</span>; i &lt; <span class=\"hljs-number\">10</span>; ++i)\n    {\n        arr.<span class=\"hljs-built_in\">addValue</span>(i);\n    }\n    <span class=\"hljs-keyword\">for</span> (<span class=\"hljs-type\">int</span> j = <span class=\"hljs-number\">0</span>; j &lt; <span class=\"hljs-number\">10</span>; ++j)\n    {\n        cout &lt;&lt; arr.<span class=\"hljs-built_in\">indexof</span>(j) &lt;&lt; endl;\n    }\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">0</span>;\n}\n</code></pre>\n","children":[],"payload":{"lines":[178,197]}},{"type":"ordered_list","depth":4,"payload":{"lines":[197,198],"startIndex":3},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[197,198],"index":3},"content":"3. 编译并运行程序,输出结果如下:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-terminal\">0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n</code></pre>\n","children":[],"payload":{"lines":[198,210]}},{"type":"ordered_list","depth":4,"payload":{"lines":[211,212],"startIndex":4},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[211,212],"index":4},"content":"4. 测试浮点型数组,修改<code>main.cpp</code>源文件,代码如下:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&lt;iostream&gt;</span></span>\n<span class=\"hljs-keyword\">using</span> <span class=\"hljs-keyword\">namespace</span> std;\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&quot;MyArray.h&quot;</span></span>\n\n<span class=\"hljs-function\"><span class=\"hljs-type\">int</span> <span class=\"hljs-title\">main</span><span class=\"hljs-params\">(<span class=\"hljs-type\">int</span> argc, <span class=\"hljs-type\">char</span> *argv[])</span>\n</span>{   \n    MyArray&lt;<span class=\"hljs-type\">double</span>&gt; arr;\n    <span class=\"hljs-keyword\">for</span> (<span class=\"hljs-type\">int</span> i = <span class=\"hljs-number\">0</span>; i &lt; <span class=\"hljs-number\">10</span>; ++i)\n    {\n        arr.<span class=\"hljs-built_in\">addValue</span>(i+<span class=\"hljs-number\">.112</span>f);\n    }\n    <span class=\"hljs-keyword\">for</span> (<span class=\"hljs-type\">int</span> j = <span class=\"hljs-number\">0</span>; j &lt; <span class=\"hljs-number\">10</span>; ++j)\n    {\n        cout &lt;&lt; arr.<span class=\"hljs-built_in\">indexof</span>(j) &lt;&lt; endl;\n    }\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">0</span>;\n}\n</code></pre>\n","children":[],"payload":{"lines":[212,231]}},{"type":"ordered_list","depth":4,"payload":{"lines":[232,233],"startIndex":5},"content":"","children":[{"type":"list_item","depth":5,"payload":{"lines":[232,233],"index":5},"content":"5. 编译并运行程序,输出结果如下:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-terminal\">0.112\n1.112\n2.112\n3.112\n4.112\n5.112\n6.112\n7.112\n8.112\n9.112\n</code></pre>\n","children":[],"payload":{"lines":[233,245]}}]}]},{"type":"heading","depth":2,"payload":{"lines":[246,247]},"content":"类模板的特化和偏特化","children":[{"type":"heading","depth":3,"payload":{"lines":[247,248]},"content":"类模板的特化","children":[{"type":"blockquote","depth":4,"payload":{"lines":[248,249]},"content":"","children":[{"type":"paragraph","depth":5,"payload":{"lines":[248,249]},"content":"类模板的特化是指在实例化类模板时,对特定类型的泛型进行特殊处理,即用户指定特定类型的类模板时,通过特化过类模板生成的类可能于其他的类由完全不同的结构。","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-keyword\">template</span> &lt;&gt;\n<span class=\"hljs-keyword\">class</span> 类名&lt;指定类型, 指定类型, ...&gt;\n{\n    类成员\n}\n</code></pre>\n","children":[],"payload":{"lines":[252,259]}},{"type":"heading","depth":4,"payload":{"lines":[259,260]},"content":"示例代码","children":[{"type":"ordered_list","depth":5,"payload":{"lines":[260,261],"startIndex":1},"content":"","children":[{"type":"list_item","depth":6,"payload":{"lines":[260,261],"index":1},"content":"1. 修改<code>MyArray.h</code>头文件,添加如下代码:","children":[]}]},{"type":"fence","depth":5,"content":"<pre><code class=\"language-h\">template &lt;&gt;\nclass MyArray&lt;double&gt;\n{\npublic:\n    MyArray();\n};\n\nMyArray&lt;double&gt;::MyArray()\n{\n    cout &lt;&lt; &quot;MyArray&lt;double&gt;::MyArray()&quot; &lt;&lt; endl;\n}\n</code></pre>\n","children":[],"payload":{"lines":[261,274]}},{"type":"ordered_list","depth":5,"payload":{"lines":[274,276],"startIndex":2},"content":"","children":[{"type":"list_item","depth":6,"payload":{"lines":[274,276],"index":2},"content":"2. 修改<code>main.cpp</code>源文件,添加如下代码:<br>\n其中的<code>main函数</code>内容如下","children":[]}]},{"type":"fence","depth":5,"content":"<pre><code class=\"language-cpp\">MyArray&lt;<span class=\"hljs-type\">double</span>&gt; arr;\n<span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">0</span>;\n</code></pre>\n","children":[],"payload":{"lines":[276,280]}},{"type":"ordered_list","depth":5,"payload":{"lines":[280,281],"startIndex":3},"content":"","children":[{"type":"list_item","depth":6,"payload":{"lines":[280,281],"index":3},"content":"3. 编译并运行程序,输出结果如下:","children":[]}]},{"type":"fence","depth":5,"content":"<pre><code class=\"language-terminal\">MyArray&lt;double&gt;::MyArray()\n</code></pre>\n","children":[],"payload":{"lines":[281,284]}}]}]},{"type":"heading","depth":3,"payload":{"lines":[285,286]},"content":"类模板的偏特化","children":[{"type":"blockquote","depth":4,"payload":{"lines":[286,288]},"content":"","children":[{"type":"paragraph","depth":5,"payload":{"lines":[286,288]},"content":"偏特化与特化相似,但是偏特化只对类模板的部分参数进行特化,而不是对整个类模板进行特化。<br>\n 偏特化模板是需要对整个类模板进行声明定义:","children":[]}]},{"type":"fence","depth":4,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-keyword\">template</span> &lt;<span class=\"hljs-keyword\">typename</span> T, 不需要特化的泛型, ...&gt;\n<span class=\"hljs-keyword\">class</span> 类名&lt;T, 不需要特化的泛型, ...&gt;\n{\n    类成员\n}\n</code></pre>\n","children":[],"payload":{"lines":[288,295]}},{"type":"heading","depth":4,"payload":{"lines":[295,296]},"content":"示例代码","children":[{"type":"ordered_list","depth":5,"payload":{"lines":[296,297],"startIndex":1},"content":"","children":[{"type":"list_item","depth":6,"payload":{"lines":[296,297],"index":1},"content":"1. 新增文件<code>pair.h</code>,内容如下:","children":[]}]},{"type":"fence","depth":5,"content":"<pre><code class=\"language-h\">#pragma once\n#ifndef _PAIR_H\n#define _PAIR_H\n\n#include &lt;iostream&gt;\nusing namespace std;\n\ntemplate&lt;typename T1, typename T2&gt;\nclass Pair\n{\nprivate:\n    T1 first;\n    T2 second;\npublic:\n    Pair();\n};\n\ntemplate &lt;typename T1, typename T2&gt;\nPair&lt;T1, T2&gt;::Pair()\n{\n    cout &lt;&lt; &quot;Pair&lt;T1, T2&gt;::Pair()&quot; &lt;&lt; endl;\n}\n\ntemplate &lt;typename T2&gt;\nclass Pair&lt;char, T2&gt;\n{\npublic:\n    Pair();\n};\n\ntemplate &lt;typename T2&gt;\nPair&lt;char, T2&gt;::Pair()\n{\n    cout &lt;&lt; &quot;Pair&lt;char, T2&gt;::Pair()&quot; &lt;&lt; endl;\n}\n#endif // !_PAIR_H\n</code></pre>\n","children":[],"payload":{"lines":[297,335]}},{"type":"ordered_list","depth":5,"payload":{"lines":[336,337],"startIndex":2},"content":"","children":[{"type":"list_item","depth":6,"payload":{"lines":[336,337],"index":2},"content":"2. <code>main.cpp</code>文件内容如下:","children":[]}]},{"type":"fence","depth":5,"content":"<pre><code class=\"language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&lt;iostream&gt;</span></span>\n<span class=\"hljs-keyword\">using</span> <span class=\"hljs-keyword\">namespace</span> std;\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include</span> <span class=\"hljs-string\">&quot;pair.h&quot;</span></span>\n\n<span class=\"hljs-function\"><span class=\"hljs-type\">int</span> <span class=\"hljs-title\">main</span><span class=\"hljs-params\">(<span class=\"hljs-type\">int</span> argc, <span class=\"hljs-type\">char</span> *argv[])</span>\n</span>{   \n    Pair&lt;<span class=\"hljs-type\">char</span>, <span class=\"hljs-type\">int</span>&gt; p1;\n    Pair&lt;<span class=\"hljs-type\">int</span>, <span class=\"hljs-type\">char</span>&gt; p2;\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">0</span>;\n}\n\n</code></pre>\n","children":[],"payload":{"lines":[337,350]}},{"type":"ordered_list","depth":5,"payload":{"lines":[350,351],"startIndex":3},"content":"","children":[{"type":"list_item","depth":6,"payload":{"lines":[350,351],"index":3},"content":"3. 编译并运行程序,输出结果如下:","children":[]}]},{"type":"fence","depth":5,"content":"<pre><code class=\"language-terminal\">Pair&lt;char, T2&gt;::Pair()\nPair&lt;T1, T2&gt;::Pair()\n</code></pre>\n","children":[],"payload":{"lines":[351,355]}}]}]}]}]}]},{})</script>
</body>
</html>