﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-SilverStar</title><link>http://www.cppblog.com/lethe555/</link><description>专注于C++技术</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:08:08 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:08:08 GMT</pubDate><ttl>60</ttl><item><title>string与wstring相互转换</title><link>http://www.cppblog.com/lethe555/archive/2012/10/15/193328.html</link><dc:creator>lethe555</dc:creator><author>lethe555</author><pubDate>Mon, 15 Oct 2012 10:15:00 GMT</pubDate><guid>http://www.cppblog.com/lethe555/archive/2012/10/15/193328.html</guid><wfw:comment>http://www.cppblog.com/lethe555/comments/193328.html</wfw:comment><comments>http://www.cppblog.com/lethe555/archive/2012/10/15/193328.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lethe555/comments/commentRss/193328.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lethe555/services/trackbacks/193328.html</trackback:ping><description><![CDATA[<p><font face="DejaVu Sans Mono">// VS11</font></p><pre class="code"><span style="background: white; color: blue"><font face="DejaVu Sans Mono">#include </font></span><font face="DejaVu Sans Mono"><span style="background: white; color: #a31515">&lt;codecvt&gt;
</span><span style="background: white; color: blue">#include </span></font><font face="DejaVu Sans Mono"><span style="background: white; color: #a31515">&lt;string&gt;
</span><span style="background: white; color: blue">#include </span></font><font face="DejaVu Sans Mono"><span style="background: white; color: #a31515">&lt;locale&gt;
</span><span style="background: white; color: blue">#include </span></font><font face="DejaVu Sans Mono"><span style="background: white; color: #a31515">&lt;iostream&gt;
</span><span style="background: white; color: blue">#include </span></font><font face="DejaVu Sans Mono"><span style="background: white; color: #a31515">&lt;fstream&gt;
</span><span style="background: white; color: blue">using namespace </span><span style="background: white; color: #002251">std</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;

</span><span style="background: white; color: blue">typedef </span><span style="background: white; color: #2b91af">codecvt_byname</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: blue">wchar_t</span><span style="background: white; color: black">,</span><span style="background: white; color: blue">char</span><span style="background: white; color: black">,</span><span style="background: white; color: #002251">std</span><span style="background: white; color: black">::</span><span style="background: white; color: #2b91af">mbstate_t</span><span style="background: white; color: black">&gt; </span><span style="background: white; color: #2b91af">F</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;
</span><span style="background: white; color: blue">int </span><span style="background: white; color: #6f008a">_tmain</span><span style="background: white; color: black">(</span><span style="background: white; color: blue">int </span><span style="background: white; color: #505050">argc</span><span style="background: white; color: black">, </span><span style="background: white; color: #2b91af">_TCHAR</span><span style="background: white; color: black">* </span><span style="background: white; color: #505050">argv</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">[])
{
    </span><span style="background: white; color: #2b91af">locale</span><span style="background: white; color: black">::</span><span style="background: white; color: maroon">global</span><span style="background: white; color: black">(</span><span style="background: white; color: #2b91af">locale</span><span style="background: white; color: black">(</span><span style="background: white; color: #2b91af">locale</span><span style="background: white; color: black">(),</span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">codecvt_utf8</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: blue">wchar_t</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">&gt;()));

    </span><span style="background: white; color: #2b91af">wstring_convert</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: #2b91af">F</span><span style="background: white; color: black">,</span><span style="background: white; color: blue">wchar_t</span><span style="background: white; color: black">&gt; </span><span style="background: white; color: navy">stows</span><span style="background: white; color: black">(</span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">F</span><span style="background: white; color: black">(</span><span style="background: white; color: #a31515">"chs"</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">)); 
    
    </span><span style="background: white; color: #2b91af">string </span><span style="background: white; color: navy">str</span><span style="background: white; color: black">=</span><span style="background: white; color: #a31515">"Hello,世界!"</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;
    </span><span style="background: white; color: #2b91af">wstring </span><span style="background: white; color: navy">wstr</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;

    </span><span style="background: white; color: navy">wcout</span><span style="background: white; color: black">.</span><span style="background: white; color: maroon">imbue</span><span style="background: white; color: black">(</span><span style="background: white; color: #2b91af">locale</span><span style="background: white; color: black">(</span><span style="background: white; color: #a31515">"chs"</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">));
    </span><span style="background: white; color: navy">wstr</span><span style="background: white; color: black">=</span><span style="background: white; color: navy">stows</span><span style="background: white; color: black">.</span><span style="background: white; color: maroon">from_bytes</span><span style="background: white; color: black">(</span><span style="background: white; color: navy">str</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">);
    </span><span style="background: white; color: navy">wcout</span><span style="background: white; color: black">&lt;&lt;</span><span style="background: white; color: navy">wstr</span><span style="background: white; color: black">&lt;&lt;</span><span style="background: white; color: maroon">endl</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;
    </span><span style="background: white; color: #2b91af">wofstream</span><span style="background: white; color: black">(</span><span style="background: white; color: #a31515">"E:\\Wout.txt"</span><span style="background: white; color: black">)&lt;&lt;</span><span style="background: white; color: navy">wstr</span><span style="background: white; color: black">;</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: green">// 以UTF-8 编码保存
    
    </span><span style="background: white; color: navy">str</span><span style="background: white; color: black">=</span><span style="background: white; color: navy">stows</span><span style="background: white; color: black">.</span><span style="background: white; color: maroon">to_bytes</span><span style="background: white; color: black">(</span><span style="background: white; color: navy">wstr</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">);
    </span><span style="background: white; color: navy">cout</span><span style="background: white; color: black">&lt;&lt;</span><span style="background: white; color: navy">str</span><span style="background: white; color: black">&lt;&lt;</span><span style="background: white; color: maroon">endl</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;
    </span><span style="background: white; color: #2b91af">ofstream</span><span style="background: white; color: black">(</span><span style="background: white; color: #a31515">"E:\\out.txt"</span><span style="background: white; color: black">)&lt;&lt;</span><span style="background: white; color: navy">str</span></font><font face="DejaVu Sans Mono"><span style="background: white; color: black">;

    </span><span style="background: white; color: blue">return </span></font><span style="background: white; color: black"><font face="DejaVu Sans Mono">0;
}

</font></span></pre><img src ="http://www.cppblog.com/lethe555/aggbug/193328.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lethe555/" target="_blank">lethe555</a> 2012-10-15 18:15 <a href="http://www.cppblog.com/lethe555/archive/2012/10/15/193328.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Test</title><link>http://www.cppblog.com/lethe555/archive/2012/09/25/191992.html</link><dc:creator>lethe555</dc:creator><author>lethe555</author><pubDate>Tue, 25 Sep 2012 12:52:00 GMT</pubDate><guid>http://www.cppblog.com/lethe555/archive/2012/09/25/191992.html</guid><wfw:comment>http://www.cppblog.com/lethe555/comments/191992.html</wfw:comment><comments>http://www.cppblog.com/lethe555/archive/2012/09/25/191992.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lethe555/comments/commentRss/191992.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lethe555/services/trackbacks/191992.html</trackback:ping><description><![CDATA[<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffff80; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include <span style="color: #0000ff">&lt;</span><span style="color: #800000">ctime</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #include <span style="color: #0000ff">&lt;</span><span style="color: #800000">iostream</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: #include <span style="color: #0000ff">&lt;</span><span style="color: #800000">chrono</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: #include <span style="color: #0000ff">&lt;</span><span style="color: #800000">thread</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: #include <span style="color: #0000ff">&lt;</span><span style="color: #800000">iomanip</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: using namespace std;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: // the function f() does some time-consuming work
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: void f()
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: {
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: 	volatile double d=0;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: 	for(int n=0; n<span style="color: #0000ff">&lt;</span>1000; ++n)
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: 		printf("%d ",n);
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: 	printf("\n");
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: }
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: int main()
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: {
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: 	std::clock_t c_start = std::clock();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: 	auto t_start = std::chrono::high_resolution_clock::now();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: 	std::thread t1(f);
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: 	std::thread t2(f); // f() is called on two threads
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: 	t1.join();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: 	t2.join();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: 
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: 
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25: 	std::clock_t c_end = std::clock();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: 	auto t_end = std::chrono::high_resolution_clock::now();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: 	std::cout <span style="color: #0000ff">&lt;</span><span style="color: #0000ff">&lt;</span> "CPU time used: "
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: 		<span style="color: #0000ff">&lt;</span><span style="color: #0000ff">&lt;</span> 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: 		<span style="color: #0000ff">&lt;</span><span style="color: #0000ff">&lt;</span> " ms\n";
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30: 	std::cout <span style="color: #0000ff">&lt;</span><span style="color: #0000ff">&lt;</span> "Wall clock time passed: "
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31: 		<span style="color: #0000ff">&lt;</span><span style="color: #0000ff">&lt;</span> std::chrono::duration_cast<span style="color: #0000ff">&lt;</span><span style="color: #c71585">std</span>::chrono::milliseconds<span style="color: #0000ff">&gt;</span>(t_end - t_start).count()
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32: 		<span style="color: #0000ff">&lt;</span><span style="color: #0000ff">&lt;</span> " ms\n";
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33: 
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34: 
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: 
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36: }
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37: </pre></pre><img src ="http://www.cppblog.com/lethe555/aggbug/191992.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lethe555/" target="_blank">lethe555</a> 2012-09-25 20:52 <a href="http://www.cppblog.com/lethe555/archive/2012/09/25/191992.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>