﻿<?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++博客-C++</title><link>http://www.cppblog.com/liuliu/</link><description /><language>zh-cn</language><lastBuildDate>Sat, 04 Apr 2026 20:05:35 GMT</lastBuildDate><pubDate>Sat, 04 Apr 2026 20:05:35 GMT</pubDate><ttl>60</ttl><item><title>点到点的最大连接个数</title><link>http://www.cppblog.com/liuliu/archive/2009/06/18/88004.html</link><dc:creator>Liu</dc:creator><author>Liu</author><pubDate>Thu, 18 Jun 2009 09:37:00 GMT</pubDate><guid>http://www.cppblog.com/liuliu/archive/2009/06/18/88004.html</guid><wfw:comment>http://www.cppblog.com/liuliu/comments/88004.html</wfw:comment><comments>http://www.cppblog.com/liuliu/archive/2009/06/18/88004.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liuliu/comments/commentRss/88004.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liuliu/services/trackbacks/88004.html</trackback:ping><description><![CDATA[假定坐标系中，从点(0,0) 到 点 (m,n)，只能上行或者右行，则可以到达的路径总数是多少？<br>每次只能移动一个单位<br><br>解法一：<br><br>组合数学的多重集合排列问题，设上行一个单位为 x，右行一个单位为y，则到达(m,n)点<br>的一条路径，对应于{m*x, n*y}的一个全排列。根据全排列公式，可有<br><br>&nbsp;&nbsp;&nbsp; (m+n)!<br>__________ = C(m+n, m)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m! n!<br><br><br>解法二：<br><br>将问题转化为DP问题，即 设 F(x,y)表示到达(x,y)的路径总数，则有<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F(x,y) = F(x-1,y) + F(x, y-1);<br>其中有F(0,j) = 1, F(i, 0) = 1, i = 1...x;&nbsp; j = 1.....y<br><br>代码如下：以 （9，9）点为列子<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000"></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: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;F(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y)&nbsp;{<br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;x&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;y&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;F(x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,y)&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;F(x,y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()&nbsp;{<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">F(</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000"></span></div>
<img src ="http://www.cppblog.com/liuliu/aggbug/88004.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liuliu/" target="_blank">Liu</a> 2009-06-18 17:37 <a href="http://www.cppblog.com/liuliu/archive/2009/06/18/88004.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>求递增的最长子序列</title><link>http://www.cppblog.com/liuliu/archive/2009/06/18/88001.html</link><dc:creator>Liu</dc:creator><author>Liu</author><pubDate>Thu, 18 Jun 2009 09:13:00 GMT</pubDate><guid>http://www.cppblog.com/liuliu/archive/2009/06/18/88001.html</guid><wfw:comment>http://www.cppblog.com/liuliu/comments/88001.html</wfw:comment><comments>http://www.cppblog.com/liuliu/archive/2009/06/18/88001.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liuliu/comments/commentRss/88001.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liuliu/services/trackbacks/88001.html</trackback:ping><description><![CDATA[<p>给定一个序列<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a1, a2, a3,&nbsp;...., aN<br>找出其中的递增的最长子序列<br><br><br>基本思想：DP的方法，对数字 ai 来说，假设前(1,2,....,i-1)个数字中的最长子序列长度为m，如果 ai 比这个子序列中的最大的一个数大，则 ai 加入到这个最长子序列，否则 goto a(i+1)，代码如下<br><br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#&nbsp;include&nbsp;</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: #008080">&nbsp;4</span><span style="COLOR: #000000"><img id=Codehighlighter1_73_415_Open_Image onclick="this.style.display='none'; Codehighlighter1_73_415_Open_Text.style.display='none'; Codehighlighter1_73_415_Closed_Image.style.display='inline'; Codehighlighter1_73_415_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_73_415_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_73_415_Closed_Text.style.display='none'; Codehighlighter1_73_415_Open_Image.style.display='inline'; Codehighlighter1_73_415_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()&nbsp;</span><span id=Codehighlighter1_73_415_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_73_415_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;length&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_109_129_Open_Image onclick="this.style.display='none'; Codehighlighter1_109_129_Open_Text.style.display='none'; Codehighlighter1_109_129_Closed_Image.style.display='inline'; Codehighlighter1_109_129_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_109_129_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_109_129_Closed_Text.style.display='none'; Codehighlighter1_109_129_Open_Image.style.display='inline'; Codehighlighter1_109_129_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;number[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span id=Codehighlighter1_109_129_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_109_129_Open_Text><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">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;std::vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;Lis(length);<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_200_338_Open_Image onclick="this.style.display='none'; Codehighlighter1_200_338_Open_Text.style.display='none'; Codehighlighter1_200_338_Closed_Image.style.display='inline'; Codehighlighter1_200_338_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_200_338_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_200_338_Closed_Text.style.display='none'; Codehighlighter1_200_338_Open_Image.style.display='inline'; Codehighlighter1_200_338_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;length;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;</span><span id=Codehighlighter1_200_338_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_200_338_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lis[i]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_246_335_Open_Image onclick="this.style.display='none'; Codehighlighter1_246_335_Open_Text.style.display='none'; Codehighlighter1_246_335_Closed_Image.style.display='inline'; Codehighlighter1_246_335_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_246_335_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_246_335_Closed_Text.style.display='none'; Codehighlighter1_246_335_Open_Image.style.display='inline'; Codehighlighter1_246_335_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&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;j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;j&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;i;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;</span><span id=Codehighlighter1_246_335_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_246_335_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_301_331_Open_Image onclick="this.style.display='none'; Codehighlighter1_301_331_Open_Text.style.display='none'; Codehighlighter1_301_331_Closed_Image.style.display='inline'; Codehighlighter1_301_331_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_301_331_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_301_331_Closed_Text.style.display='none'; Codehighlighter1_301_331_Open_Image.style.display='inline'; Codehighlighter1_301_331_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(number[j]&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;number[i]&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;Lis[j]&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;Lis[i])&nbsp;</span><span id=Codehighlighter1_301_331_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_301_331_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lis[i]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Lis[j]&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">max_element(Lis.begin(),&nbsp;Lis.end())</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&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><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/liuliu/aggbug/88001.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liuliu/" target="_blank">Liu</a> 2009-06-18 17:13 <a href="http://www.cppblog.com/liuliu/archive/2009/06/18/88001.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>三种过滤空格方法，已经过滤特定字符</title><link>http://www.cppblog.com/liuliu/archive/2009/06/16/87818.html</link><dc:creator>Liu</dc:creator><author>Liu</author><pubDate>Tue, 16 Jun 2009 11:27:00 GMT</pubDate><guid>http://www.cppblog.com/liuliu/archive/2009/06/16/87818.html</guid><wfw:comment>http://www.cppblog.com/liuliu/comments/87818.html</wfw:comment><comments>http://www.cppblog.com/liuliu/archive/2009/06/16/87818.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liuliu/comments/commentRss/87818.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liuliu/services/trackbacks/87818.html</trackback:ping><description><![CDATA[<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iterator</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">vector</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</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: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">sstream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()&nbsp;{<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;line1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">We&nbsp;were&nbsp;her&nbsp;pride&nbsp;of&nbsp;10&nbsp;she&nbsp;named&nbsp;us&nbsp;--</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;line2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Benjamin,&nbsp;Pheonix,&nbsp;the&nbsp;Prodigal</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;line3&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">and&nbsp;perspicacious&nbsp;pacific&nbsp;Suzanne</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;sentence&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;line1&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;line2&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;line3;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">::size_type&nbsp;pos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">::size_type&nbsp;prepos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::vector</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;words;<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">*&nbsp;Method&nbsp;1<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;filter&nbsp;the&nbsp;space&nbsp;using&nbsp;find_first_of<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;*</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">while&nbsp;((pos&nbsp;=&nbsp;sentence.find_first_of("&nbsp;",pos))<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;!=&nbsp;std::string::npos)&nbsp;{<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;words.push_back(sentence.substr(prepos,(pos-prepos)));<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos++;<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prepos&nbsp;=&nbsp;pos;<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">}</span><span style="color: #008000;"><br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">*&nbsp;Method&nbsp;2<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;filter&nbsp;the&nbsp;space&nbsp;using&nbsp;getline<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;*</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">std::stringstream&nbsp;ss(sentence);<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">std::string&nbsp;temp;<br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">while(std::getline(ss,temp,'&nbsp;')){<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;words.push_back(temp);<br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">}</span><span style="color: #008000;"><br></span><span style="color: #008080;">39</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">40</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">*&nbsp;Method&nbsp;3<br></span><span style="color: #008080;">42</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;filter&nbsp;the&nbsp;space&nbsp;using&nbsp;&lt;&lt;<br></span><span style="color: #008080;">43</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;*</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">44</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::stringstream&nbsp;ss(sentence);<br></span><span style="color: #008080;">45</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;temp;<br></span><span style="color: #008080;">46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(ss</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">temp){<br></span><span style="color: #008080;">47</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;words.push_back(temp);<br></span><span style="color: #008080;">48</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">49</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">50</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">51</span>&nbsp;<span style="color: #000000;"></span><span style="color: #808080;">/////////</span><span style="color: #008000;">//&nbsp;filter&nbsp;the&nbsp;punctuations&nbsp;</span><span style="color: #808080;">/////////////////////////////////<br></span><span style="color: #008080;">52</span>&nbsp;<span style="color: #808080;"></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;punctuations(</span><span style="color: #000000;">"</span><span style="color: #000000;">,--10</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">53</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::vector</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::iterator&nbsp;iter;<br></span><span style="color: #008080;">54</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;filter&nbsp;the&nbsp;punctuations</span><span style="color: #008000;"><br></span><span style="color: #008080;">55</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(iter&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;words.begin();&nbsp;iter&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;words.end();&nbsp;</span><span style="color: #000000;">++</span><span style="color: #000000;">iter)&nbsp;{<br></span><span style="color: #008080;">56</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">57</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">((pos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">*</span><span style="color: #000000;">iter).find_first_of(punctuations,pos))<br></span><span style="color: #008080;">58</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">::npos)&nbsp;{<br></span><span style="color: #008080;">59</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #000000;">*</span><span style="color: #000000;">iter).erase(pos,</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">60</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">61</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">((</span><span style="color: #000000;">*</span><span style="color: #000000;">iter).empty())&nbsp;{<br></span><span style="color: #008080;">62</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iter&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;words.erase(iter);<br></span><span style="color: #008080;">63</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iter</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br></span><span style="color: #008080;">64</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">65</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">66</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">67</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;output&nbsp;the&nbsp;filtered&nbsp;vector</span><span style="color: #008000;"><br></span><span style="color: #008080;">68</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::ostream_iterator</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">std::</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;output(std::cout,</span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">69</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;std::copy(words.begin(),words.end(),output);<br></span><span style="color: #008080;">70</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">71</span>&nbsp;<span style="color: #000000;">&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><span style="color: #008080;">72</span>&nbsp;<span style="color: #000000;">}</span></div>
<br> <img src ="http://www.cppblog.com/liuliu/aggbug/87818.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liuliu/" target="_blank">Liu</a> 2009-06-16 19:27 <a href="http://www.cppblog.com/liuliu/archive/2009/06/16/87818.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>getline 的用法</title><link>http://www.cppblog.com/liuliu/archive/2009/06/16/87817.html</link><dc:creator>Liu</dc:creator><author>Liu</author><pubDate>Tue, 16 Jun 2009 11:18:00 GMT</pubDate><guid>http://www.cppblog.com/liuliu/archive/2009/06/16/87817.html</guid><wfw:comment>http://www.cppblog.com/liuliu/comments/87817.html</wfw:comment><comments>http://www.cppblog.com/liuliu/archive/2009/06/16/87817.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liuliu/comments/commentRss/87817.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liuliu/services/trackbacks/87817.html</trackback:ping><description><![CDATA[<table style="width: 100%;" border="0">
    <tbody>
        <tr>
            <td>
            <p><span style="font-size: xx-large;"><strong><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/reference/iostream/istream"><span style="font-size: x-large;">istream</span></a><span style="font-size: x-large;">::getline</span></strong></span></p>
            </td>
            <td align="right" valign="bottom">public member function</td>
        </tr>
    </tbody>
</table>
<table style="width: 100%;" border="0">
    <tbody>
        <tr>
            <td><tt><span style="color: #008000;">
            <pre>istream&amp; getline (char* s, streamsize n );<br>istream&amp; getline (char* s, streamsize n, char delim );</pre>
            </span></tt></td>
            <td align="right"><br></td>
        </tr>
    </tbody>
</table>
<p><strong>Get line from stream</strong></p>
<p>Extracts characters from the input sequence and stores them as a c-string into the array beginning at <em>s</em>. </p>
<p>Characters are extracted until either <em>(n - 1)</em> characters have been extracted or the delimiting character is found (which is <em>delim</em> if this parameter is specified, or <tt>'\n'</tt>
otherwise). The extraction also stops if the end of file is reached in
the input sequence or if an error occurs during the input operation. </p>
<p>If the delimiter is found, it is extracted and discarded, i.e. it is
not stored and the next input operation will begin after it. If you
don't want this character to be extracted, you can use member <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/istream::get">get</a> instead. </p>
<p>The ending null character that signals the end of a c-string is automatically appended to <em>s</em> after the data extracted. </p>
<p>The number of characters read by this function can be obtained by calling to the member function <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/istream::gcount">gcount</a>. </p>
<p>A global function with the same name exists in header &lt;<a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/string">string</a>&gt;. This global function provides a similar behavior, but with standard C++ <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/string">string</a> objects instead of c-strings: see <a  href="string:getline">getline (string)</a>. </p>
<h3>Parameters</h3>
<dl><dt>s</dt><dd>A pointer to an array of characters where the string is stored as a c-string.</dd><dt>n</dt><dd>Maximum number of characters to store (including the terminating null character).<br>This is an integer value of type <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/streamsize">streamsize</a>.<br>If the function stops reading because this size is reached, the <tt>failbit</tt> internal flag is set.</dd><dt>delim</dt><dd>The
delimiting character. The operation of extracting succesive characters
is stopped when this character is read. This parameter is optional, if
not specified the function considers <tt>'\n'</tt> (a newline character) to be the delimiting character.</dd></dl>
<h3>Return Value</h3>
<p>The function returns <tt>*this</tt>. </p>
<p>Errors are signaled by modifying the internal state flags: </p>
<table class="boxed" border="0">
    <tbody>
        <tr>
            <th>flag</th><th>error</th>
        </tr>
        <tr>
            <td><tt>eofbit</tt></td>
            <td>The end of the source of characters is reached during its operations.</td>
        </tr>
        <tr>
            <td><tt>failbit</tt></td>
            <td>No characters were extracted because the end was prematurely found.<br>This is also set if the function stops extracting because <em>n-1</em> characters were extracted (<em>n</em> including the terminating null-character).<br>Notice that some <tt>eofbit</tt> cases will also set <tt>failbit</tt>.</td>
        </tr>
        <tr>
            <td><tt>badbit</tt></td>
            <td>An error other than the above happened.</td>
        </tr>
    </tbody>
</table>
<p>Additionaly, in any of these cases, if the appropriate flag has been set with member function <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/ios::exceptions">ios::exceptions</a>, an exception of type <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/ios_base::failure">ios_base::failure</a> is thrown. </p>
<h3>Example</h3>
<table class="snippet" border="0">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">// istream getline</span><br><span class="prep">#include &lt;iostream&gt;</span><br><span class="kw">using</span> <span class="kw">namespace</span> std;<br><br><span class="kw">int</span> main () {<br>  <span class="kw">char</span> name[256], title[256];<br><br>  cout &lt;&lt; <span class="str">"Enter your name: "</span>;<br>  cin.getline (name,256);<br><br>  cout &lt;&lt; <span class="str">"Enter your favourite movie: "</span>;<br>  cin.getline (title,256);<br><br>  cout &lt;&lt; name &lt;&lt; <span class="str">"'s favourite movie is "</span> &lt;&lt; title;<br><br>  <span class="kw">return</span> 0;<br>}</pre>
            </td>
        </tr>
    </tbody>
</table>
<p>This example ilustrates how to get lines from the standard input stream ( <tt>cin</tt> ). </p>
<h3>Basic template member declarations</h3>
<p>( basic_istream&lt;charT,traits&gt; )</p>
<table class="snippet" border="0">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">typedef</span> charT char_type;<br>basic_istream&amp; getline (char_type* s, streamsize n );<br>basic_istream&amp; getline (char_type* s, streamsize n, char_type delim );</pre>
            </td>
        </tr>
    </tbody>
</table>
<h3>See also</h3>
<table class="list" border="0">
    <tbody>
        <tr>
            <td class="tit"><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/reference/iostream/istream/get.html"><strong>istream::get</strong></a></td>
            <td class="des">Get unformatted data from stream <small><span style="color: #008000; font-size: x-small;">(public member function)</span></small></td>
        </tr>
    </tbody>
</table>
<table class="list" border="0">
    <tbody>
        <tr>
            <td class="tit"><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/reference/iostream/istream/ignore.html"><strong>istream::ignore</strong></a></td>
            <td class="des">Extract and discard characters <small><span style="color: #008000; font-size: x-small;">(public member functions)</span></small></td>
        </tr>
    </tbody>
</table>
<table class="list" border="0">
    <tbody>
        <tr>
            <td class="tit"><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/reference/iostream/istream/gcount.html"><strong>istream::gcount</strong></a></td>
            <td class="des">Get number of characters extracted by last unformatted input operation <small><span style="color: #008000; font-size: x-small;">(public member function)</span></small></td>
        </tr>
    </tbody>
</table>
<p>//////////////////////////////////////////////////////////////////////////////////////////////////////////////////</p>
<p>
&nbsp;
&nbsp; &nbsp; </p>
<p>getline为全局方法</p>
<p>
</p>
<table style="width: 100%;" border="0">
    <tbody>
        <tr>
            <td><span style="font-size: xx-large;"><strong><span style="font-size: x-large;">getline</span></strong></span></td>
            <td align="right" valign="bottom">function</td>
        </tr>
    </tbody>
</table>
<p>
</p>
<table style="width: 100%;" border="0">
    <tbody>
        <tr>
            <td><tt><span style="color: #008000;">
            <pre>istream&amp; getline ( istream&amp; is, string&amp; str, char delim );<br>istream&amp; getline ( istream&amp; is, string&amp; str );</pre>
            </span></tt></td>
            <td align="right"><tt><span style="color: #000080;">
            <pre>&lt;string&gt;</pre>
            </span></tt></td>
        </tr>
    </tbody>
</table>
<p><strong>Get line from stream</strong></p>
<p>Extracts characters from <em>is</em> and stores them into <em>str</em> until a delimitation character is found. </p>
<p>The delimiter character is <em>delim</em> for the first function version, and <tt>'\n'</tt> (newline character) for the second. The extraction also stops if the end of file is reached in <em>is</em> or if some other error occurs during the input operation. </p>
<p>If the delimiter is found, it is extracted and discarded, i.e. it is
not stored and the next input operation will begin after it. </p>
<p>Notice that unlike the c-string versions of <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/istream::getline">istream::getline</a>, these <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/string">string</a> versions are implemented as global functions instead of members of the stream class. </p>
<h3>Parameters</h3>
<dl><dt>is</dt><dd><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/istream">istream</a> object on which the extraction operation is performed.</dd><dt>str</dt><dd><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/string">string</a> object where the extracted content is stored.</dd><dt>delim</dt><dd>The delimiting character. The operation of extracting succesive characters is stopped when this character is read.</dd></dl>
<h3>Return Value</h3>
<p>The same as parameter <em>is</em>. </p>
<p>Errors are signaled by modifying the internal state flags: </p>
<table class="boxed" border="0">
    <tbody>
        <tr>
            <th>flag</th><th>error</th>
        </tr>
        <tr>
            <td><tt>eofbit</tt></td>
            <td>The end of the source of characters is reached during its operations.</td>
        </tr>
        <tr>
            <td><tt>failbit</tt></td>
            <td>No characters were extracted because the end was prematurely found.Notice that some <tt>eofbit</tt> cases will also set <tt>failbit</tt>.</td>
        </tr>
        <tr>
            <td><tt>badbit</tt></td>
            <td>An error other than the above happened.</td>
        </tr>
    </tbody>
</table>
<p>Additionaly, in any of these cases, if the appropriate flag has been set with <em>is</em>'s member function <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/ios::exceptions">ios::exceptions</a>, an exception of type <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/ios_base::failure">ios_base::failure</a> is thrown. </p>
<h3>Example</h3>
<table style="width: 321px; height: 181px;" class="snippet" border="0">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">
            <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;getline&nbsp;with&nbsp;strings</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></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: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()&nbsp;{<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str;<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Please&nbsp;enter&nbsp;full&nbsp;name:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;getline&nbsp;(cin,str);<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Thank&nbsp;you,&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">.\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">}</span></div>
            <br></span></pre>
            </td>
        </tr>
    </tbody>
</table>
<p>This example ilustrates how to get lines from the standard input stream ( <a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/cin">cin</a> ). </p>
<h3>Basic template member declarations</h3>
<p>( basic_istream&lt;charT,traits&gt; )</p>
<table class="snippet" border="0">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">template</span>&lt;<span class="kw">class</span> charT, <span class="kw">class</span> traits, <span class="kw">class</span> Allocator&gt;<br>  basic_istream&lt;charT,traits&gt;&amp;<br>    getline (basic_istream&lt;charT,traits&gt;&amp; is,<br>             basic_string&lt;charT,traits,Allocator&gt;&amp; str,<br>             charT delim );<br><span class="kw">template</span>&lt;<span class="kw">class</span> charT, <span class="kw">class</span> traits, <span class="kw">class</span> Allocator&gt;<br>  basic_istream&lt;charT,traits&gt;&amp;<br>    getline (basic_istream&lt;charT,traits&gt;&amp; is,<br>             basic_string&lt;charT,traits,Allocator&gt;&amp; str );</pre>
            </td>
        </tr>
    </tbody>
</table>
<h3>See also</h3>
<table class="list" border="0">
    <tbody>
        <tr>
            <td class="tit"><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/reference/string/operator%3E%3E.html"><strong>operator&gt;&gt;</strong></a></td>
            <td class="des">Extract string from istream <small><span style="color: #008000; font-size: x-small;">(function)</span></small></td>
        </tr>
    </tbody>
</table>
<table class="list" border="0">
    <tbody>
        <tr>
            <td class="tit"><a  href="http://blog.csdn.net/yingxunren/archive/2009/03/08/reference/iostream/istream/getline.html"><strong>istream::getline</strong></a></td>
            <td class="des">Get line from stream <small><span style="color: #008000; font-size: x-small;">(public member function)</span></small></td>
        </tr>
    </tbody>
</table>
<p>
///////////////////////////////////////////////////////////////////////////////////////////////////////////// </p><img src ="http://www.cppblog.com/liuliu/aggbug/87817.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liuliu/" target="_blank">Liu</a> 2009-06-16 19:18 <a href="http://www.cppblog.com/liuliu/archive/2009/06/16/87817.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>