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

145 lines
10 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::wstring_convert&lt;Codecvt,Elem,Wide_alloc,Byte_alloc&gt;::wstring_convert</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_locale_wstring_convert_wstring_convert 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::wstring_convert&lt;Codecvt,Elem,Wide_alloc,Byte_alloc&gt;::</span>wstring_convert</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-locale.html">locale</a>&lrm; | <a href="cpp-locale-wstring_convert.html">wstring convert</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>
<tr class="t-dcl">
<td>
<div><span class="mw-geshi cpp source-cpp">wstring_convert<span class="br0">(</span><span class="br0">)</span> <span class="sy4">:</span> wstring_convert<span class="br0">(</span> new Codecvt <span class="br0">)</span> <span class="br0">{</span> <span class="br0">}</span></span></div>
</td>
<td>(1)</td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">explicit</span> wstring_convert<span class="br0">(</span> Codecvt<span class="sy2">*</span> pcvt <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(2)</td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl">
<td>
<div><span class="mw-geshi cpp source-cpp">wstring_convert<span class="br0">(</span> Codecvt<span class="sy2">*</span> pcvt, state_type state<span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(3)</td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">explicit</span> wstring_convert<span class="br0">(</span> <span class="kw4">const</span> byte_string<span class="sy3">&amp;</span> byte_err,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">const</span> wide_string<span class="sy3">&amp;</span> wide_err <span class="sy1">=</span> wide_string<span class="br0">(</span><span class="br0">)</span> <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(4)</td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl t-since-cxx14">
<td>
<div><span class="mw-geshi cpp source-cpp">wstring_convert<span class="br0">(</span><span class="kw4">const</span> <a href="cpp-locale-wstring_convert.html"><span class="kw1899">std::<span class="me2">wstring_convert</span></span></a><span class="sy3">&amp;</span><span class="br0">)</span> <span class="sy1">=</span> delete<span class="sy4">;</span></span></div>
</td>
<td>(5)</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<div class="t-li1"><span class="t-li">1)</span> &#40664;&#35748;&#26500;&#36896;&#20989;&#25968;&#12290;</div>
<div class="t-li1"><span class="t-li">2)</span> &#20197;&#25351;&#23450;&#30340;&#36716;&#25442;&#24179;&#38754;&#65292;&#29992;&#21021;&#22987;&#36801;&#31227;&#29366;&#24577;&#21644;&#38169;&#35823;&#23383;&#31526;&#20018;&#30340;&#40664;&#35748;&#26500;&#36896;&#20540;&#26500;&#36896; <code>wstring_convert</code> &#23545;&#35937;&#12290;</div>
<div class="t-li1"><span class="t-li">3)</span> &#20197;&#25351;&#23450;&#30340;&#36716;&#25442;&#24179;&#38754;&#21644;&#25351;&#23450;&#30340;&#21021;&#22987;&#36801;&#31227;&#29366;&#24577;&#65292;&#29992;&#40664;&#35748;&#26500;&#36896;&#30340;&#38169;&#35823;&#23383;&#31526;&#20018;&#20540;&#26500;&#36896; <code>wstring_convert</code> &#23545;&#35937;&#12290;</div>
<div class="t-li1"><span class="t-li">4)</span> &#20197;&#25351;&#23450;&#30340;&#38169;&#35823;&#23383;&#31526;&#20018;&#65292;&#20197; <span class="t-c"><span class="mw-geshi cpp source-cpp">new Codecvt</span></span> &#20026;&#36716;&#25442;&#24179;&#38754;&#24182;&#20197; <code>state_type</code> &#30340;&#40664;&#35748;&#26500;&#36896;&#20540;&#20026;&#21021;&#22987;&#29366;&#24577;&#26500;&#36896; <code>wstring_convert</code> &#23545;&#35937;&#12290;</div>
<div class="t-li1"><span class="t-li">5)</span> &#22797;&#21046;&#26500;&#36896;&#20989;&#25968;&#34987;&#21024;&#38500;&#65292; wstring_convert &#38750;<a href="cpp-named_req-CopyConstructible.html"><span style="font-style:italic">&#21487;&#22797;&#21046;&#26500;&#36896;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(CopyConstructible)</span></a> &#12290;</div>
<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>pcvt</td>
<td>-</td>
<td>&#25351;&#21521; <code>Codecvt</code> &#31867;&#22411;&#36716;&#25442;&#24179;&#38754;&#30340;&#25351;&#38024;&#65288;&#33509;&#27492;&#25351;&#38024;&#20026;&#31354;&#21017;&#34892;&#20026;&#26410;&#23450;&#20041;&#65289;</td>
</tr>
<tr class="t-par">
<td>state</td>
<td>-</td>
<td>&#36716;&#25442;&#36801;&#31227;&#29366;&#24577;&#30340;&#21021;&#22987;&#20540;</td>
</tr>
<tr class="t-par">
<td>byte_err</td>
<td>-</td>
<td>&#38169;&#35823;&#26102;&#26174;&#31034;&#30340;&#31364;&#23383;&#31526;&#20018;</td>
</tr>
<tr class="t-par">
<td>wide_err</td>
<td>-</td>
<td>&#38169;&#35823;&#26102;&#26174;&#31034;&#30340;&#23485;&#23383;&#31526;&#20018;</td>
</tr>
</table>
<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://wg21.link/P0935R0">P0935R0</a></td>
<td>C++11</td>
<td>&#40664;&#35748;&#26500;&#36896;&#20989;&#25968;&#26366;&#20026; explicit</td>
<td>&#20351;&#20043;&#20026;&#38544;&#24335;</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;locale&gt;</span>
<span class="co2">#include &lt;utility&gt;</span>
<span class="co2">#include &lt;codecvt&gt;</span>
<span class="co1">// &#20026; wstring/wbuffer convert &#36866;&#37197;&#32465;&#23450;&#26412;&#22320;&#29615;&#22659;&#30340;&#24179;&#38754;&#30340;&#24037;&#20855;&#21253;&#35013;&#22120;</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> Facet<span class="sy1">&gt;</span>
<span class="kw1">struct</span> deletable_facet <span class="sy4">:</span> Facet
<span class="br0">{</span>
<span class="kw1">using</span> Facet<span class="sy4">::</span><span class="me2">Facet</span><span class="sy4">;</span> <span class="co1">// &#32487;&#25215;&#26500;&#36896;&#20989;&#25968;</span>
~deletable_facet<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="co1">// UTF-16le / UCS4 &#36716;&#25442;</span>
<a href="cpp-locale-wstring_convert.html"><span class="kw1899">std::<span class="me2">wstring_convert</span></span></a><span class="sy1">&lt;</span>
<a href="cpp-locale-codecvt_utf16.html"><span class="kw1984">std::<span class="me2">codecvt_utf16</span></span></a><span class="sy1">&lt;</span><span class="kw4">char32_t</span>, <span class="nu12">0x10ffff</span>, <a href="cpp-locale-codecvt_mode.html"><span class="kw1989">std::<span class="me2">little_endian</span></span></a><span class="sy1">&gt;</span>
<span class="sy1">&gt;</span> u16to32<span class="sy4">;</span>
<span class="co1">// UTF-8 / &#23485;&#23383;&#31526;&#20018;&#36716;&#25442;&#65292;&#24102;&#23450;&#21046;&#28040;&#24687;</span>
<a href="cpp-locale-wstring_convert.html"><span class="kw1899">std::<span class="me2">wstring_convert</span></span></a><span class="sy1">&lt;</span><a href="cpp-locale-codecvt_utf8.html"><span class="kw1983">std::<span class="me2">codecvt_utf8</span></span></a><span class="sy1">&lt;</span><span class="kw4">wchar_t</span><span class="sy1">&gt;&gt;</span> u8towide<span class="br0">(</span><span class="st0">&quot;Error!&quot;</span>, L<span class="st0">&quot;Error!&quot;</span><span class="br0">)</span><span class="sy4">;</span>
<span class="co1">// GB18030 / &#23485;&#23383;&#31526;&#20018;&#36716;&#25442;&#24179;&#38754;</span>
<span class="kw1">typedef</span> deletable_facet<span class="sy1">&lt;</span><a href="cpp-locale-codecvt_byname.html"><span class="kw1976">std::<span class="me2">codecvt_byname</span></span></a><span class="sy1">&lt;</span><span class="kw4">wchar_t</span>, <span class="kw4">char</span>, <a href="cpp-string-multibyte-mbstate_t.html"><span class="kw1179">std::<span class="me2">mbstate_t</span></span></a><span class="sy1">&gt;&gt;</span> F<span class="sy4">;</span>
<a href="cpp-locale-wstring_convert.html"><span class="kw1899">std::<span class="me2">wstring_convert</span></span></a><span class="sy1">&lt;</span>F<span class="sy1">&gt;</span> gbtowide<span class="br0">(</span>new F<span class="br0">(</span><span class="st0">&quot;zh_CN.gb18030&quot;</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div>
</div>
</div>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>