﻿<?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++博客-【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】-文章分类-C/C++ STL 学习</title><link>http://www.cppblog.com/xiongnanbin/category/9993.html</link><description>竞赛决不是捷径，它只是另一种艰辛的生活方式。得到与失去，只有时间会去评判；成功与失败，只有历史能去仲裁。我不会永远成功，正如我不会永远失败一样</description><language>zh-cn</language><lastBuildDate>Mon, 10 Aug 2009 21:48:39 GMT</lastBuildDate><pubDate>Mon, 10 Aug 2009 21:48:39 GMT</pubDate><ttl>60</ttl><item><title>C/C++ 字符串排序</title><link>http://www.cppblog.com/xiongnanbin/articles/78169.html</link><dc:creator>开拓者</dc:creator><author>开拓者</author><pubDate>Sat, 28 Mar 2009 12:44:00 GMT</pubDate><guid>http://www.cppblog.com/xiongnanbin/articles/78169.html</guid><wfw:comment>http://www.cppblog.com/xiongnanbin/comments/78169.html</wfw:comment><comments>http://www.cppblog.com/xiongnanbin/articles/78169.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiongnanbin/comments/commentRss/78169.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiongnanbin/services/trackbacks/78169.html</trackback:ping><description><![CDATA[题目描述:<br>上次唐伯虎点秋香那是秋香自己跑出来滴,这次他可要真的自己点了,下面由我来描述一下点的规则.当然事先要列举所有姑娘的匿名咯,我要对他进行1次提问,我问他对第几个姑娘感兴趣,他选的当然是他的意中人咯。就是对于N个人进行字符串排序，再找K小。
<p style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体">input：<br>第一行为一个整数小于1001的整数n,k.从第二行到第n+1行是对每个姑娘的名字.</p>
<p style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体">output：<br>输出文件有且仅有一行,为他点中的&#8220;秋香&#8221;即第k个名字</p>
<p style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体">input：<br>5 3<br>faff<br>adf<br>beig<br>plaie<br>zfdeegeetulae</p>
<p>output：<br>faff</p>
<h4><span style="COLOR: #ff00ff">【参考程序】:<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;a[</span><span style="COLOR: #000000">1005</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">1005</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">},tt;<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;n,k;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cmp(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">t)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)s,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;strcmp(i,j);<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">k);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,a[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;qsort(a,n,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">1005</span><span style="COLOR: #000000">,cmp);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,a[k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
</span></h4>
<img src ="http://www.cppblog.com/xiongnanbin/aggbug/78169.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiongnanbin/" target="_blank">开拓者</a> 2009-03-28 20:44 <a href="http://www.cppblog.com/xiongnanbin/articles/78169.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++  vector 容 器</title><link>http://www.cppblog.com/xiongnanbin/articles/78168.html</link><dc:creator>开拓者</dc:creator><author>开拓者</author><pubDate>Sat, 28 Mar 2009 12:42:00 GMT</pubDate><guid>http://www.cppblog.com/xiongnanbin/articles/78168.html</guid><wfw:comment>http://www.cppblog.com/xiongnanbin/comments/78168.html</wfw:comment><comments>http://www.cppblog.com/xiongnanbin/articles/78168.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiongnanbin/comments/commentRss/78168.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiongnanbin/services/trackbacks/78168.html</trackback:ping><description><![CDATA[<p style="FONT-FAMILY: 宋体"></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><strong><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">Vector容器</span><span style="COLOR: #008000">*/</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">numeric</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><strong><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span></strong><strong><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span></strong><strong><span style="COLOR: #000000">&nbsp;vec;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;i,n;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span></strong><strong><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span></strong><strong><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span></strong><strong><span style="COLOR: #000000">&nbsp;x;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span></strong><strong><span style="COLOR: #000000">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vec.push_back(x);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span></strong><strong><span style="COLOR: #000000">::iterator&nbsp;j;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">vec.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vec.end();j</span><span style="COLOR: #000000">++</span></strong><strong><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">accumulate(vec.begin(),vec.end(),</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求容器所有数值的综合</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span></strong><span style="COLOR: #000000"><strong>;<br>}&nbsp;</strong></span></div>
<p style="FONT-FAMILY: 宋体"><font size=3><font color=#33cccc></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><strong><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">删除函数erase的使用</span><span style="COLOR: #008000">*/</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><strong><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span></strong><strong><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;v(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">vector&nbsp;数组</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;v[i]</span><span style="COLOR: #000000">=</span></strong><strong><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;v.erase(v.begin()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">删除从开始加2的地方的元素，vector默认开始为0.</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span></strong><strong><span style="COLOR: #000000">::iterator&nbsp;j;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">v.end();j</span><span style="COLOR: #000000">++</span></strong><strong><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;v.clear();</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">清楚所有元素</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">v.size()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span></strong><span style="COLOR: #000000"><strong>;<br>}</strong></span></div>
<p style="FONT-FAMILY: 宋体"></font></font>&nbsp;</p>
<div style="BORDER-RIGHT: #00ccff 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #00ccff 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #00ccff 1px solid; WIDTH: 98%; COLOR: #0000ff; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #00ccff 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><strong><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">区间的反向排列即颠倒顺序</span><span style="COLOR: #008000">*/</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><strong><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span></strong><strong><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;v(</span><span style="COLOR: #000000">10</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;v[i]</span><span style="COLOR: #000000">=</span></strong><strong><span style="COLOR: #000000">i;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span></strong><strong><span style="COLOR: #000000">::iterator&nbsp;j;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">v.end();j</span><span style="COLOR: #000000">++</span></strong><strong><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span></strong><strong><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;reverse(v.begin(),v.end());<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">v.end();j</span><span style="COLOR: #000000">++</span></strong><strong><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span></strong><strong><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">reverse除了反一串以外还可以反指定的一个范围,example:</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;reverse(v.begin()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,v.end()</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">v.end();j</span><span style="COLOR: #000000">++</span></strong><strong><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span></strong><strong><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span></strong><span style="COLOR: #000000"><strong>;<br>}<br></strong></span></div>
<p style="FONT-FAMILY: 宋体"><strong><font color=#33cccc></font></strong><br>&nbsp;</p>
<img src ="http://www.cppblog.com/xiongnanbin/aggbug/78168.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiongnanbin/" target="_blank">开拓者</a> 2009-03-28 20:42 <a href="http://www.cppblog.com/xiongnanbin/articles/78168.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++ sort和基本属性 </title><link>http://www.cppblog.com/xiongnanbin/articles/78160.html</link><dc:creator>开拓者</dc:creator><author>开拓者</author><pubDate>Sat, 28 Mar 2009 10:31:00 GMT</pubDate><guid>http://www.cppblog.com/xiongnanbin/articles/78160.html</guid><wfw:comment>http://www.cppblog.com/xiongnanbin/comments/78160.html</wfw:comment><comments>http://www.cppblog.com/xiongnanbin/articles/78160.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiongnanbin/comments/commentRss/78160.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiongnanbin/services/trackbacks/78160.html</trackback:ping><description><![CDATA[<p><font color=#008080><font size=3><font face=Arial><strong></p>
</strong></font></font></font>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee">
<h4><span style="COLOR: #008000">//</span><span style="COLOR: #008000">泛函数排序sort的使用</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;cmp(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">y)<br>{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">ture不交换&nbsp;false交换，sort默认是升序。此程序为降序</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(x</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">y)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;v(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;v[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator&nbsp;j;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">v.end();j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;sort(v.begin(),v.end(),cmp);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v.begin();j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">v.end();j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}&nbsp;</span></h4>
</div>
<p>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><strong><span style="COLOR: #008000">//</span><span style="COLOR: #008000">C++容器向量大小的相关函数</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><strong><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span></strong><strong><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;v(</span><span style="COLOR: #000000">10</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;v[i]</span><span style="COLOR: #000000">=</span></strong><strong><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">容器大小：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">v.size()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">容器的空与不空：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">v.empty()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;v.clear();&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">容器大小：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">v.size()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">容器的空与不空：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">v.empty()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span></strong><span style="COLOR: #000000"><strong>;<br>}&nbsp;<br></strong></span></div>
<p><font color=#008080><font face=Arial color=#ff00ff></font></font><font face=Arial><font color=#008080><font color=#ff00ff>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000"><strong>/*</strong></span><strong><span style="COLOR: #008000">C++&nbsp;string&nbsp;的基本操作<br>+运算,length(),比较函数<br>连接函数</span><span style="COLOR: #008000">*/</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><strong><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span></strong><strong><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abcd</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1234</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s1.length()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;s1.append(s2);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">等价=&gt;&nbsp;s1=s1+s2;</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s1</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s1.length()</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span></strong><span style="COLOR: #000000"><strong>;<br>}&nbsp;<br></strong></span></div>
</font></font></font>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #eeeeee"><strong><span style="COLOR: #008000">//</span><span style="COLOR: #008000">string&nbsp;的替换和插入函数(replace)</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br><strong>#include</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><strong><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span></strong><strong><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span></strong><strong><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span></strong><strong><span style="COLOR: #000000">&nbsp;s,s2;<br>&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abcd123456</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;s2</span><span style="COLOR: #000000">=</span></strong><strong><span style="COLOR: #000000">s;<br>&nbsp;&nbsp;&nbsp;&nbsp;s.replace(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">good</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">/*</span></strong><strong><span style="COLOR: #008000">如果replace的第一个数值是从哪个位置的前面开始插入一个字符或字符串<br>&nbsp;&nbsp;&nbsp;&nbsp;第二个参数是替换几个，1就替换开始插入位置把替换的个数字符换成要替换的字符的前几个如上例就是把d换成了g，再插入ood再后面，第三的参数就是插入的字符啦</span><span style="COLOR: #008000">*/</span></strong><span style="COLOR: #000000"><br><strong>&nbsp;&nbsp;&nbsp;&nbsp;cout</strong></span><strong><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">再了个例子：</span></strong><span style="COLOR: #008000"><br></span><strong><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;s2.replace(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">good</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s2</span><span style="COLOR: #000000">&lt;&lt;</span></strong><strong><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span></strong><strong><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span></strong><span style="COLOR: #000000"><strong>;<br>}&nbsp;<br><br></strong></span></div>
<img src ="http://www.cppblog.com/xiongnanbin/aggbug/78160.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiongnanbin/" target="_blank">开拓者</a> 2009-03-28 18:31 <a href="http://www.cppblog.com/xiongnanbin/articles/78160.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>