Files
hustoj/web/cppreference-zh/chmhelp/cpp-utility-pair-swap.html
2025-08-01 12:57:54 +08:00

137 lines
8.2 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>std::pair&lt;T1,T2&gt;::swap</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_utility_pair_swap skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::pair&lt;T1,T2&gt;::</span>swap</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-utility.html">utility</a>&lrm; | <a href="cpp-utility-pair.html">pair</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody></tbody>
<tbody class="t-dcl-rev">
<tr class="t-dcl t-since-cxx11 t-until-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span> swap<span class="br0">(</span>pair<span class="sy3">&amp;</span> other<span class="br0">)</span> <span class="kw1">noexcept</span><span class="br0">(</span><span class="coMULTI">/* see below */</span><span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span><br />
<span class="t-mark-rev t-until-cxx20">(C++20 &#21069;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span> <span class="kw4">void</span> swap<span class="br0">(</span>pair<span class="sy3">&amp;</span> other<span class="br0">)</span> <span class="kw1">noexcept</span><span class="br0">(</span><span class="coMULTI">/* see below */</span><span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
</tbody>
<tbody>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#20132;&#25442; <code>first</code> &#19982; <code>other.first</code> &#21450; <code>second</code> &#19982; <code>other.second</code> &#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>other</td>
<td>-</td>
<td>&#35201;&#20132;&#25442;&#20540;&#30340; <code>pair</code></td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#65288;&#26080;&#65289;</p>
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">&#24322;&#24120;</span></h3>
<table class="t-rev-begin">
<tr class="t-rev t-until-cxx17">
<td>
<div class="t-noexcept-full"><a href="cpp-language-noexcept_spec.html"><tt>noexcept</tt></a> &#35828;&#26126;&#65306;&nbsp;&nbsp;
<div class="t-noexcept-box"><span class="t-cc"><span class="mw-geshi cpp source-cpp"><span class="kw1">noexcept</span><span class="br0">(</span><br /></span></span>
<p>&nbsp; &nbsp; &nbsp;<span class="kw1">noexcept</span><span class="br0">(</span>swap<span class="br0">(</span>first, other.<span class="me1">first</span><span class="br0">)</span><span class="br0">)</span> <span class="sy3">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">noexcept</span><span class="br0">(</span>swap<span class="br0">(</span>second, other.<span class="me1">second</span><span class="br0">)</span><span class="br0">)</span><br /></p>
<span class="br0">)</span></div>
</div>
<p>&#22312;&#19978;&#36848;&#34920;&#36798;&#24335;&#20013;&#65292;&#25353;&#29031;&#19982; C++17 <span class="t-lc"><a href="cpp-types-is_swappable.html">std::is_nothrow_swappable</a></span> &#29305;&#24615;&#25152;&#29992;&#30340;&#30456;&#21516;&#26041;&#24335;&#26597;&#25214;&#26631;&#35782;&#31526; <code>swap</code> &#12290;</p>
</td>
<td><span class="t-mark-rev t-until-cxx17">(C++17 &#21069;)</span></td>
</tr>
<tr class="t-rev t-since-cxx17">
<td>
<div class="t-noexcept-full"><a href="cpp-language-noexcept_spec.html"><tt>noexcept</tt></a> &#35828;&#26126;&#65306;&nbsp;&nbsp;
<div class="t-noexcept-box"><span class="t-cc"><span class="mw-geshi cpp source-cpp"><span class="kw1">noexcept</span><span class="br0">(</span><br /></span></span>
<p>&nbsp; &nbsp; &nbsp;<a href="cpp-types-is_swappable.html"><span class="kw577">std::<span class="me2">is_nothrow_swappable_v</span></span></a><span class="sy1">&lt;</span>first_type<span class="sy1">&gt;</span> <span class="sy3">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp;<a href="cpp-types-is_swappable.html"><span class="kw577">std::<span class="me2">is_nothrow_swappable_v</span></span></a><span class="sy1">&lt;</span>second_type<span class="sy1">&gt;</span><br /></p>
<span class="br0">)</span></div>
</div>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 &#36215;)</span></td>
</tr>
</table>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</span></h3>
<div class="t-example">
<div class="t-example-live-link"></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="cpp source-cpp">
<pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;utility&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="cpp-utility-pair.html"><span class="kw1096">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, <a href="cpp-string-basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span> p1, p2<span class="sy4">;</span>
p1 <span class="sy1">=</span> <a href="cpp-utility-pair-make_pair.html"><span class="kw1097">std::<span class="me2">make_pair</span></span></a><span class="br0">(</span><span class="nu0">10</span>, <span class="st0">&quot;test&quot;</span><span class="br0">)</span><span class="sy4">;</span>
p2.<span class="me1">swap</span><span class="br0">(</span>p1<span class="br0">)</span><span class="sy4">;</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;(&quot;</span> <span class="sy1">&lt;&lt;</span> p2.<span class="me1">first</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;, &quot;</span> <span class="sy1">&lt;&lt;</span> p2.<span class="me1">second</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;)<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">(10, test)</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">&#32570;&#38519;&#25253;&#21578;</span></h3>
<p>&#19979;&#21015;&#26356;&#25913;&#34892;&#20026;&#30340;&#32570;&#38519;&#25253;&#21578;&#36861;&#28335;&#22320;&#24212;&#29992;&#20110;&#20197;&#21069;&#20986;&#29256;&#30340; C++ &#26631;&#20934;&#12290;</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th>DR</th>
<th>&#24212;&#29992;&#20110;</th>
<th>&#20986;&#29256;&#26102;&#30340;&#34892;&#20026;</th>
<th>&#27491;&#30830;&#34892;&#20026;</th>
</tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://cplusplus.github.io/LWG/issue2456">LWG 2456</a></td>
<td>C++11</td>
<td><code>noexcept</code> &#25351;&#23450;&#26366;&#20026;&#30149;&#24335;</td>
<td>&#20196;&#20043;&#26377;&#25928;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-algorithm-swap.html"><span class="t-lines"><span>swap</span></span></a></div>
</div>
</td>
<td>&#20132;&#25442;&#20004;&#20010;&#23545;&#35937;&#30340;&#20540;<br />
<span class="t-mark">(&#20989;&#25968;&#27169;&#26495;)</span></td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>