﻿<?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++博客-茶博-随笔分类-STL库函数</title><link>http://www.cppblog.com/yehongly/category/8289.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 29 Oct 2008 18:32:27 GMT</lastBuildDate><pubDate>Wed, 29 Oct 2008 18:32:27 GMT</pubDate><ttl>60</ttl><item><title>C++ string类常用函数</title><link>http://www.cppblog.com/yehongly/archive/2008/09/16/61977.html</link><dc:creator>茶</dc:creator><author>茶</author><pubDate>Tue, 16 Sep 2008 06:45:00 GMT</pubDate><guid>http://www.cppblog.com/yehongly/archive/2008/09/16/61977.html</guid><wfw:comment>http://www.cppblog.com/yehongly/comments/61977.html</wfw:comment><comments>http://www.cppblog.com/yehongly/archive/2008/09/16/61977.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yehongly/comments/commentRss/61977.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yehongly/services/trackbacks/61977.html</trackback:ping><description><![CDATA[<p><font size="3"><strong>http://xiaocao000.spaces.live.com/blog/cns!F826A925CF33491A!117.entry<br></strong></font></p>
<p><font size="3"><strong>string类的构造函数：</strong></font>
</p>
<p><font size="3"><br>string(const char *s);&nbsp;&nbsp;&nbsp; //用c字符串s初始化<br>string(int n,char c);&nbsp;&nbsp;&nbsp;&nbsp; //用n个字符c初始化<br>此外，string类还支持默认构造函数和复制构造函数，如string s1；string s2="hello"；都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常</font>
</p>
<p><font size="3"><strong>string类的字符操作：</strong></font>
</p>
<p><font size="3"><br>const char &amp;operator[](int n)const;<br>const char &amp;at(int n)const;<br>char &amp;operator[](int n);<br>char &amp;at(int n);<br>operator[]和at()均返回当前字符串中第n个字符的位置，但at函数提供范围检查，当越界时会抛出out_of_range异常，下标运算符[]不提供检查访问。<br>const char *data()const;//返回一个非null终止的c字符数组<br>const char *c_str()const;//返回一个以null终止的c字符串<br>int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中，返回实际拷贝的数目</font>
</p>
<p><font size="3"><strong>string的特性描述:</strong></font>
</p>
<p><font size="3"><br>int capacity()const;&nbsp;&nbsp;&nbsp; //返回当前容量（即string中不必增加内存即可存放的元素个数）<br>int max_size()const;&nbsp;&nbsp;&nbsp; //返回string对象中可存放的最大字符串的长度<br>int size()const;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回当前字符串的大小<br>int length()const;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回当前字符串的长度<br>bool empty()const;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //当前字符串是否为空<br>void resize(int len,char c);//把字符串当前大小置为len，并用字符c填充不足的部分</font>
</p>
<p><font size="3"><strong>string类的输入输出操作:</strong></font>
</p>
<p><font size="3"><br>string类重载运算符operator&gt;&gt;用于输入，同样重载运算符operator&lt;&lt;用于输出操作。<br>函数getline(istream &amp;in,string &amp;s);用于从输入流in中读取字符串到s中，以换行符'\n'分开。<br>&nbsp;</font>
</p>
<p><font size="3"><strong>string的赋值：</strong></font>
</p>
<p><font size="3"><br>string &amp;operator=(const string &amp;s);//把字符串s赋给当前字符串<br>string &amp;assign(const char *s);//用c类型字符串s赋值<br>string &amp;assign(const char *s,int n);//用c字符串s开始的n个字符赋值<br>string &amp;assign(const string &amp;s);//把字符串s赋给当前字符串<br>string &amp;assign(int n,char c);//用n个字符c赋值给当前字符串<br>string &amp;assign(const string &amp;s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串<br>string &amp;assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串<br>&nbsp;</font>
</p>
<p><font size="3"><strong>string的连接：</strong></font>
</p>
<p><font size="3"><br><code>string &amp;operator+=(const string &amp;s);//把字符串s连接到当前字符串的结尾 </code></font><font size="3"><br>string &amp;append(const char *s);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //把c类型字符串s连接到当前字符串结尾<br><code>string &amp;append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾<br>string &amp;append(const string &amp;s);&nbsp;&nbsp;&nbsp; //同operator+=()<br>string &amp;append(const string &amp;s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾<br>string &amp;append(int n,char c);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //在当前字符串结尾添加n个字符c<br>string &amp;append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾 </code></font><font size="3"><br>&nbsp;</font>
</p>
<p><font size="3"><code><strong>string的比较：</strong></code></font>
</p>
<p><font size="3"><code><br>bool operator==(const string &amp;s1,const string &amp;s2)const;//比较两个字符串是否相等<br>运算符"&gt;","&lt;","&gt;=","&lt;=","!="均被重载用于字符串的比较；<br>int compare(const string &amp;s) const;//比较当前字符串和s的大小<br>int compare(int pos, int n,const string &amp;s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小<br>int compare(int pos, int n,const string &amp;s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小<br>int compare(const char *s) const;<br>int compare(int pos, int n,const char *s) const;<br>int compare(int pos, int n,const char *s, int pos2) const;<br>compare函数在&gt;时返回1，&lt;时返回-1，==时返回0 </code></font><font size="3">&nbsp;</font>
</p>
<p><font size="3"><code><strong>string的子串：</strong></code></font>
</p>
<p><font size="3"><code><br>string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串 </code></font><font size="3">
</font></p>
<p><font size="3"><br><strong>string的交换：</strong>
</font></p>
<p><font size="3"><br>void swap(string &amp;s2);&nbsp;&nbsp;&nbsp; //交换当前字符串与s2的值</font>
</p>
<p><font size="3">&nbsp;</font>
</p>
<p><font size="3"><strong>string类的查找函数：</strong></font>
</p>
<p><font size="3"><code>int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置<br>int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置<br>int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置<br>int find(const string &amp;s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置<br>//查找成功时返回所在位置，失败返回string::npos的值 </code></font>
</p>
<p><font size="3"><code>int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置<br>int rfind(const char *s, int pos = npos) const;<br>int rfind(const char *s, int pos, int n = npos) const;<br>int rfind(const string &amp;s,int pos = npos) const;<br>//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置，成功返回所在位置，失败时返回string::npos的值 </code></font>
</p>
<p><font size="3"><code>int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置<br>int find_first_of(const char *s, int pos = 0) const;<br>int find_first_of(const char *s, int pos, int n) const;<br>int find_first_of(const string &amp;s,int pos = 0) const;<br>//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos </code></font>
</p>
<p><font size="3"><code>int find_first_not_of(char c, int pos = 0) const;<br>int find_first_not_of(const char *s, int pos = 0) const;<br>int find_first_not_of(const char *s, int pos,int n) const;<br>int find_first_not_of(const string &amp;s,int pos = 0) const;<br>//从当前串中查找第一个不在串s中的字符出现的位置，失败返回string::npos </code></font>
</p>
<p><font size="3"><code>int find_last_of(char c, int pos = npos) const;<br>int find_last_of(const char *s, int pos = npos) const;<br>int find_last_of(const char *s, int pos, int n = npos) const;<br>int find_last_of(const string &amp;s,int pos = npos) const; </code></font>
</p>
<p><font size="3"><code>int find_last_not_of(char c, int pos = npos) const;<br>int find_last_not_of(const char *s, int pos = npos) const;<br>int find_last_not_of(const char *s, int pos,&nbsp; int n) const;<br>int find_last_not_of(const string &amp;s,int pos = npos) const;<br>//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似，只不过是从后向前查找 </code></font>
</p>
<p><font size="3">&nbsp;</font>
</p>
<p><strong><font size="3">string类的替换函数：</font> </strong>
</p>
<p><font size="3"><code>string &amp;replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符，然后在p0处插入串s<br>string &amp;replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符，然后在p0处插入字符串s的前n个字符<br>string &amp;replace(int p0, int n0,const string &amp;s);//删除从p0开始的n0个字符，然后在p0处插入串s<br>string &amp;replace(int p0, int n0,const string &amp;s, int pos, int n);//删除p0开始的n0个字符，然后在p0处插入串s中从pos开始的n个字符<br>string &amp;replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符，然后在p0处插入n个字符c<br>string &amp;replace(iterator first0, iterator last0,const char *s);//把[first0，last0）之间的部分替换为字符串s<br>string &amp;replace(iterator first0, iterator last0,const char *s, int n);//把[first0，last0）之间的部分替换为s的前n个字符<br>string &amp;replace(iterator first0, iterator last0,const string &amp;s);//把[first0，last0）之间的部分替换为串s<br>string &amp;replace(iterator first0, iterator last0,int n, char c);//把[first0，last0）之间的部分替换为n个字符c<br>string
&amp;replace(iterator first0, iterator last0,const_iterator first,
const_iterator last);//把[first0，last0）之间的部分替换成[first，last）之间的字符串 </code></font>
</p>
<p><font size="3"><strong>string类的插入函数：</strong></font>
</p>
<p><font size="3"><code>string &amp;insert(int p0, const char *s);<br>string &amp;insert(int p0, const char *s, int n);<br>string &amp;insert(int p0,const string &amp;s);<br>string &amp;insert(int p0,const string &amp;s, int pos, int n);<br>//前4个函数在p0位置插入字符串s中pos开始的前n个字符<br>string &amp;insert(int p0, int n, char c);//此函数在p0处插入n个字符c<br>iterator insert(iterator it, char c);//在it处插入字符c，返回插入后迭代器的位置<br>void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first，last）之间的字符<br>void insert(iterator it, int n, char c);//在it处插入n个字符c<br>&nbsp; </code></font>
</p>
<p><strong><font size="3">string类的删除函数</font> </strong>
</p>
<p><font size="3">iterator erase(iterator first, iterator last);//删除[first，last）之间的所有字符，返回删除后迭代器的位置<br>iterator erase(iterator it);//删除it指向的字符，返回删除后迭代器的位置<br>string &amp;erase(int pos = 0, int n = npos);//删除pos开始的n个字符，返回修改后的字符串</font>
</p>
<p><font size="3">&nbsp;</font>
</p>
<p><font size="3"><strong>string类的迭代器处理：</strong></font>
</p>
<p><font size="3">string类提供了向前和向后遍历的迭代器iterator，迭代器提供了访问各个字符的语法，类似于指针操作，迭代器不检查范围。<br>用string::iterator或string::const_iterator声明迭代器变量，const_iterator不允许改变迭代的内容。常用迭代器函数有：<br>const_iterator begin()const;<br>iterator begin();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回string的起始位置<br>const_iterator end()const;<br>iterator end();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回string的最后一个字符后面的位置<br>const_iterator rbegin()const;<br>iterator rbegin();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回string的最后一个字符的位置<br>const_iterator rend()const;<br>iterator rend();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回string第一个字符位置的前面<br>rbegin和rend用于从后向前的迭代访问，通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现</font>
</p>
<p><font size="3">&nbsp;</font>
</p>
<p><strong><font size="3">字符串流处理：</font> </strong>
</p>
<p><font size="3">通过定义ostringstream和istringstream变量实现，&lt;sstream&gt;头文件中<br>例如：<br>&nbsp;&nbsp;&nbsp; string input("hello,this is a test");<br>&nbsp;&nbsp;&nbsp; istringstream is(input);<br>&nbsp;&nbsp;&nbsp; string s1,s2,s3,s4;<br>&nbsp;&nbsp;&nbsp; is&gt;&gt;s1&gt;&gt;s2&gt;&gt;s3&gt;&gt;s4;//s1="hello,this",s2="is",s3="a",s4="test"<br>&nbsp;&nbsp;&nbsp; ostringstream os;<br>&nbsp;&nbsp;&nbsp; os&lt;&lt;s1&lt;&lt;s2&lt;&lt;s3&lt;&lt;s4;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;os.str();</font> </p><img src ="http://www.cppblog.com/yehongly/aggbug/61977.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yehongly/" target="_blank">茶</a> 2008-09-16 14:45 <a href="http://www.cppblog.com/yehongly/archive/2008/09/16/61977.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c＋＋ string详解</title><link>http://www.cppblog.com/yehongly/archive/2008/09/16/61974.html</link><dc:creator>茶</dc:creator><author>茶</author><pubDate>Tue, 16 Sep 2008 06:20:00 GMT</pubDate><guid>http://www.cppblog.com/yehongly/archive/2008/09/16/61974.html</guid><wfw:comment>http://www.cppblog.com/yehongly/comments/61974.html</wfw:comment><comments>http://www.cppblog.com/yehongly/archive/2008/09/16/61974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yehongly/comments/commentRss/61974.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yehongly/services/trackbacks/61974.html</trackback:ping><description><![CDATA[http://read.newbooks.com.cn/info/119627.html<br><br>
<p>之所以抛弃char*的字符串而选用C++标准程序库中的string类，是因为他和前者比较起来，不必担心内存是否足够、字符串长度等等，而且作
为一个类出现，他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作，== 进行比较，+
做串联（是不是很简单?）。我们尽可以把它看成是C++的基本数据类型。<br>&nbsp; &nbsp;好了，进入正题&#8230;&#8230;&#8230;<br>首先，为了在我们的程序中使用string类型，我们必须包含头文件 <string></string>。如下：<br>&nbsp; &nbsp;#include <string></string>//注意这里不是string.h string.h是C字符串头文件<br><br>1．声明一个C++字符串<br>声明一个字符串变量很简单：<br>&nbsp; &nbsp;string Str;<br>这样我们就声明了一个字符串变量，但既然是一个类，就有构造函数和析构函数。上面的声明没有传入参数，所以就直接使用了string的默认的构造函数，这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下：<br>a) &nbsp; &nbsp;string s; &nbsp;//生成一个空字符串s<br>b) &nbsp; &nbsp;string s(str) //拷贝构造函数 生成str的复制品<br>c) &nbsp; &nbsp;string s(str,stridx) //将字符串str内&#8220;始于位置stridx&#8221;的部分当作字符串的初值<br>d) &nbsp; &nbsp;string s(str,stridx,strlen) //将字符串str内&#8220;始于stridx且长度顶多strlen&#8221;的部分作为字符串的初值<br>e) &nbsp; &nbsp;string s(cstr) //将C字符串作为s的初值<br>f) &nbsp; &nbsp;string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。<br>g) &nbsp; &nbsp;string s(num,c) //生成一个字符串，包含num个c字符<br>h) &nbsp; &nbsp;string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值<br>i) &nbsp; &nbsp;s.~string() //销毁所有字符，释放内存<br>都很简单，我就不解释了。<br>2．字符串操作函数<br>&nbsp; &nbsp;这里是C++字符串的重点，我先把各种操作函数罗列出来，不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数，再到后面看他的详细解释。<br>a) =,assign() &nbsp; //赋以新值<br>b) swap() &nbsp; //交换两个字符串的内容<br>c) +=,append(),push_back() //在尾部添加字符<br>d) insert() //插入字符<br>e) erase() //删除字符<br>f) clear() //删除全部字符 <br>g) replace() //替换字符<br>h) + //串联字符串<br>i) ==,!=,&lt;,&lt;=,&gt;,&gt;=,compare() &nbsp;//比较字符串<br>j) size(),length() &nbsp;//返回字符数量<br>k) max_size() //返回字符的可能最大个数<br>l) empty() &nbsp;//判断字符串是否为空<br>m) capacity() //返回重新分配之前的字符容量<br>n) reserve() //保留一定量内存以容纳一定数量的字符<br>o) [ ], at() //存取单一字符<br>p) &gt;&gt;,getline() //从stream读取某值<br>q) &lt;&lt; &nbsp;//将谋值写入stream<br>r) copy() //将某值赋值为一个C_string<br>s) c_str() //将内容以C_string返回<br>t) data() //将内容以字符数组形式返回<br>u) substr() //返回某个子字符串<br>v)查找函数<br>w)begin() end() //提供类似STL的迭代器支持<br>x) rbegin() rend() //逆向迭代器<br>y) get_allocator() //返回配置器<br>下面详细介绍：<br>2．1 C++字符串和C字符串的转换<br>&nbsp;
&nbsp;C++提供的由C++字符串得到对应的C_string的方法是使用data()、c_str()和copy()，其中，data()以字符数组的形式
返回字符串内容，但并不添加&#8217;\0&#8217;。c_str()返回一个以&#8216;\0&#8217;结尾的字符数组，而copy()则把字符串的内容复制或写入既有的
c_string或字符数组内。C++字符串并不以&#8217;\0&#8217;结尾。我的建议是在程序中能使用C++字符串就使用，除非万不得已不选用c_string。由
于只是简单介绍，详细介绍掠过，谁想进一步了解使用中的注意事项可以给我留言(到我的收件箱)。我详细解释。<br>2．2 大小和容量函数<br>&nbsp;
&nbsp;一个C++字符串存在三种大小：a)现有的字符数，函数是size()和length()，他们等效。Empty()用来检查字符串是否为空。
b)max_size()
这个大小是指当前C++字符串最多能包含的字符数，很可能和机器本身的限制或者字符串所在位置连续内存的大小有关系。我们一般情况下不用关心他，应该大小
足够我们用的。但是不够用的话，会抛出length_error异常c)capacity()重新分配内存之前
string所能包含的最大字符数。这里另一个需要指出的是reserve()函数，这个函数为string重新分配内存。重新分配的大小由其参数决定，
默认参数为0，这时候会对string进行非强制性缩减。<br><br>还有必要再重复一下C++字符串和C字符串转换的问题，许多人会遇到这样的问
题，自己做的程序要调用别人的函数、类什么的（比如数据库连接函数Connect(char*,char*)），但别人的函数参数用的是char*形式
的，而我们知道，c_str()、data()返回的字符数组由该字符串拥有，所以是一种const
char*,要想作为上面提及的函数的参数，还必须拷贝到一个char*,而我们的原则是能不使用C字符串就不使用。那么，这时候我们的处理方式是：如果
此函数对参数(也就是char*)的内容不修改的话，我们可以这样Connect((char*)UserID.c_str(),
(char*)PassWD.c_str()),但是这时候是存在危险的，因为这样转换后的字符串其实是可以修改的（有兴趣地可以自己试一试），所以我强
调除非函数调用的时候不对参数进行修改，否则必须拷贝到一个char*上去。当然，更稳妥的办法是无论什么情况都拷贝到一个char*上去。同时我们也祈
祷现在仍然使用C字符串进行编程的高手们（说他们是高手一点儿也不为过，也许在我们还穿开裆裤的时候他们就开始编程了，哈哈&#8230;）写的函数都比较规范，那样
我们就不必进行强制转换了。<br><br>2．3元素存取<br>&nbsp;
&nbsp;我们可以使用下标操作符[]和函数at()对元素包含的字符进行访问。但是应该注意的是操作符[]并不检查索引是否有效（有效索引
0~str.length()），如果索引失效，会引起未定义的行为。而at()会检查，如果使用at()的时候索引无效，会抛出
out_of_range异常。<br>&nbsp; &nbsp;有一个例外不得不说，const string a;的操作符[]对索引值是a.length()仍然有效，其返回值是&#8217;\0&#8217;。其他的各种情况，a.length()索引都是无效的。举例如下：<br>const string Cstr(&#8220;const string&#8221;);<br>string Str(&#8220;string&#8221;);<br><br>Str[3]; &nbsp; &nbsp;//ok<br>Str.at(3); &nbsp;//ok<br><br>Str[100]; //未定义的行为<br>Str.at(100); &nbsp;//throw out_of_range<br><br>Str[Str.length()] &nbsp;//未定义行为<br>Cstr[Cstr.length()] //返回 &#8216;\0&#8217;<br>Str.at(Str.length());//throw out_of_range<br>Cstr.at(Cstr.length()) ////throw out_of_range<br><br>我不赞成类似于下面的引用或指针赋值：<br>char&amp; r=s[2];<br>char* p= &amp;s[3];<br>因为一旦发生重新分配，r,p立即失效。避免的方法就是不使用。<br><br>2．4比较函数<br>&nbsp;
&nbsp;C++字符串支持常见的比较操作符（&gt;,&gt;=,&lt;,&lt;=,==,!=），甚至支持string与C-string的比较(如
str&lt;&#8221;hello&#8221;)。在使用&gt;,&gt;=,&lt;,&lt;=这些操作符的时候是根据&#8220;当前字符特性&#8221;将字符按字典顺序进行逐一得
比较。字典排序靠前的字符小，比较的顺序是从前向后比较，遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小。同
时，string(&#8220;aaaa&#8221;) &nbsp;
&nbsp;另一个功能强大的比较函数是成员函数compare()。他支持多参数处理，支持用索引值和长度定位子串来进行比较。他返回一个整数来表示比较结果，返
回值意义如下：0-相等 〉0-大于 &lt;0-小于。举例如下：<br>&nbsp; &nbsp;string s(&#8220;abcd&#8221;);<br>&nbsp; &nbsp;<br>&nbsp; &nbsp;s.compare(&#8220;abcd&#8221;); //返回0<br>&nbsp; &nbsp;s.compare(&#8220;dcba&#8221;); //返回一个小于0的值<br>&nbsp; &nbsp;s.compare(&#8220;ab&#8221;); //返回大于0的值<br>&nbsp; &nbsp;<br>s.compare(s); //相等<br>&nbsp; &nbsp;s.compare(0,2,s,2,2); //用&#8221;ab&#8221;和&#8221;cd&#8221;进行比较 小于零<br>&nbsp; &nbsp;s.compare(1,2,&#8221;bcx&#8221;,2); //用&#8221;bc&#8221;和&#8221;bc&#8221;比较。<br>怎么样？功能够全的吧！什么？还不能满足你的胃口？好吧，那等着，后面有更个性化的比较算法。先给个提示，使用的是STL的比较算法。什么？对STL一窍不通？靠，你重修吧！<br><br>2．5 更改内容<br>这在字符串的操作中占了很大一部分。<br>首先讲赋值，第一个赋值方法当然是使用操作符=，新值可以是string(如：s=ns) 、c_string(如：s=&#8221;gaint&#8221;)甚至单一字符（如：s=&#8217;j&#8217;）。还可以使用成员函数assign()，这个成员函数可以使你更灵活的对字符串赋值。还是举例说明吧：<br>s.assign(str); //不说<br>s.assign(str,1,3);//如果str是&#8221;iamangel&#8221; 就是把&#8221;ama&#8221;赋给字符串<br>s.assign(str,2,string::npos);//把字符串str从索引值2开始到结尾赋给s<br>s.assign(&#8220;gaint&#8221;); //不说<br>s.assign(&#8220;nico&#8221;,5);//把&#8217;n&#8217; &#8216;I&#8217; &#8216;c&#8217; &#8216;o&#8217; &#8216;\0&#8217;赋给字符串<br>s.assign(5,&#8217;x&#8217;);//把五个x赋给字符串<br>把字符串清空的方法有三个：s=&#8221;&#8221;;s.clear();s.erase();(我越来越觉得举例比说话让别人容易懂！)。<br>string提供了很多函数用于插入（insert）、删除（erase）、替换（replace）、增加字符。<br>先说增加字符（这里说的增加是在尾巴上），函数有 +=、append()、push_back()。举例如下：<br>s+=str;//加个字符串<br>s+=&#8221;my name is jiayp&#8221;;//加个C字符串<br>s+=&#8217;a&#8217;;//加个字符<br><br>s.append(str);<br>s.append(str,1,3);//不解释了 同前面的函数参数assign的解释<br>s.append(str,2,string::npos)//不解释了<br><br>s.append(&#8220;my name is jiayp&#8221;);<br>s.append(&#8220;nico&#8221;,5);<br>s.append(5,&#8217;x&#8217;);</p>
<p>&nbsp;</p>
<div class="postbody">&nbsp;&nbsp;&nbsp; 字符串操作是一个不小的主题,在标准C++中,string字符串类成为一个标准,<span id="txtObj_0">之所以抛弃char*的字符串而选用C++标准程序库中的string类，是因为他和前者比较起来，不必担心内存是否足够、字符串长度等等，而且作为一个类出现，他集成的操作函数足以完成我们大多数情况下的需要.<br>&nbsp;&nbsp;&nbsp; 下面我们首先从一些示例开始学习下string类的使用.<br>1)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s("hehe");<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>2)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; char chs[] = "hehe";<br>&nbsp;&nbsp;&nbsp; string s(chs);<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>3)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; char chs[] = "hehe";<br>&nbsp;&nbsp;&nbsp; string s(chs,1,3);&nbsp;&nbsp;&nbsp; //指定从chs的索引1开始,最后复制3个字节<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>4)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s1("hehe");<br>&nbsp;&nbsp;&nbsp; string s2(s1);&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s2&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>5)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s1("hehe",2,3);<br>&nbsp;&nbsp;&nbsp; string s2(s1);&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s2&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>6)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; char chs[] = "hehe";<br>&nbsp;&nbsp;&nbsp; string s(chs,3);&nbsp;&nbsp;&nbsp; //将chs前3个字符作为初值构造<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>7)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s(10,'k');&nbsp;&nbsp;&nbsp; //分配10个字符,初值都是'k'<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>//以上是string类实例的构造手段,都很简单.<br><br>9)<br>//赋新值<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s(10,'k');&nbsp;&nbsp;&nbsp; //分配10个字符,初值都是'k'<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s = "hehehehe";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s.assign("kdje");<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s.assign("fkdhfkdfd",5);&nbsp;&nbsp;&nbsp; //重新分配指定字符串的前5的元素内容<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>10)<br>//swap方法交换<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s1 = "hehe";<br>&nbsp;&nbsp;&nbsp; string s2 = "gagaga";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"s1 : "&lt;&lt;s1&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"s2 : "&lt;&lt;s2&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s1.swap(s2);<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"s1 : "&lt;&lt;s1&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"s2 : "&lt;&lt;s2&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>11)<br>//+=,append(),push_back()在尾部添加字符<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "hehe";<br>&nbsp;&nbsp;&nbsp; s += "gaga";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s.append("嘿嘿");&nbsp;&nbsp;&nbsp; //append()方法可以添加字符串<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s.push_back('k');&nbsp;&nbsp;&nbsp; //push_back()方法只能添加一个字符...<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>12)<br>//insert() 插入字符.其实,insert运用好,与其他的插入操作是一样的.<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "hehe";<br>&nbsp;&nbsp;&nbsp; s.insert(0,"头部");&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //在头部插入<br>&nbsp;&nbsp;&nbsp; s.insert(s.size(),"尾部");&nbsp;&nbsp;&nbsp; //在尾部插入<br>&nbsp;&nbsp;&nbsp; s.insert(s.size()/2,"中间");//在中间插入<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>13)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg";<br>&nbsp;&nbsp;&nbsp; s.erase(0,1);&nbsp;&nbsp;&nbsp; //从索引0到索引1,即删除掉了'a'<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; //其实,还可以使用replace方法来执行删除操作<br>&nbsp;&nbsp;&nbsp; s.replace(2,3,"");//即将指定范围内的字符替换成"",即变相删除了<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br><br>14)<br>//clear() 删除全部字符<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.length()&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s.clear();<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.length()&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; //使用earse方法变相全删除<br>&nbsp;&nbsp;&nbsp; s = "dkjfd";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.length()&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; s.erase(0,s.length());<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.length()&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>15)<br>//replace() 替换字符<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg";<br>&nbsp;&nbsp;&nbsp; s.replace(2,3,"!!!!!");//从索引2开始3个字节的字符全替换成"!!!!!"<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>16)<br>//==,!=,&lt;,&lt;=,&gt;,&gt;=,compare()&nbsp; 比较字符串<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s1 = "abcdefg";<br>&nbsp;&nbsp;&nbsp; string s2 = "abcdefg";&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if (s1==s2)cout&lt;&lt;"s1 == s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; else cout&lt;&lt;"s1 != s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if (s1!=s2)cout&lt;&lt;"s1 != s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; else cout&lt;&lt;"s1 == s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if (s1&gt;s2)cout&lt;&lt;"s1 &gt; s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; else cout&lt;&lt;"s1 &lt;= s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if (s1&lt;=s2)cout&lt;&lt;"s1 &lt;= s2"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; else cout&lt;&lt;"s1 &gt; s2"&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>17)<br>//size(),length()&nbsp; 返回字符数量<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.size()&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.length()&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>18)<br>//max_size() 返回字符的可能最大个数<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg";<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s.max_size()&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>19)<br>//empty()&nbsp; 判断字符串是否为空<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s ;<br>&nbsp;&nbsp;&nbsp; if (s.empty())<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"s 为空."&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"s 不为空."&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; s = s + "abcdefg";<br>&nbsp;&nbsp;&nbsp; if (s.empty())<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"s 为空."&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"s 不为空."&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>20)<br>// [ ], at() 存取单一字符<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg1111";<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"use []:"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;s.length(); i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;s[i]&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"use at():"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;s.length(); i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;s.at(i)&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>21)<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg1111";<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; const char * chs1 = s.c_str();<br>&nbsp;&nbsp;&nbsp; const char * chs2 = s.data();<br><br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"use at():"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; int i;<br>&nbsp;&nbsp;&nbsp; for(i=0; i&lt;s.length(); i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"c_str() : "&lt;&lt;chs1[i]&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"data() : "&lt;&lt;chs2[i]&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"c_str() : "&lt;&lt;chs1&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"data() : "&lt;&lt;chs2&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>22)<br>// substr() 返回某个子字符串<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg1111";<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; string str = s.substr(5,3);//从索引5开始3个字节<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;str&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>23)<br>// find 查找函数<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg1111";<br>&nbsp;&nbsp;&nbsp; string pattern = "fg";<br>&nbsp;&nbsp;&nbsp; string::size_type pos;<br>&nbsp;&nbsp;&nbsp; pos = s.find(pattern,0);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //从索引0开始,查找符合字符串"f"的头索引<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;pos&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; string str = s.substr(pos,pattern.size());<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;str&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br>24)<br>// begin() end() 提供类似STL的迭代器支持<br>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void main()<br>{<br>&nbsp;&nbsp;&nbsp; string s = "abcdefg1111";<br>&nbsp;&nbsp;&nbsp; for(string::iterator iter = s.begin(); iter!=s.end(); iter++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;*iter&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp; cin.get();<br>}<br></span><span id="txtObj_0">&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; 一个C++字符串存在三种大小：a)现有的字符数，函数是size()和length()，他们等效。
Empty()用来检查字符串是否为空。b)max_size()
这个大小是指当前C++字符串最多能包含的字符数，很可能和机器本身的限制或者字符串所在位置连续内存的大小有关系。我们一般情况下不用关心他，应该大小
足够我们用的。但是不够用的话，会抛出length_error异常c)capacity()重新分配内存之前
string所能包含的最大字符数。这里另一个需要指出的是reserve()函数，这个函数为string重新分配内存。重新分配的大小由其参数决定，
默认参数为0，这时候会对string进行非强制性缩减</span><br>&nbsp;</div>
<br><br> <img src ="http://www.cppblog.com/yehongly/aggbug/61974.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yehongly/" target="_blank">茶</a> 2008-09-16 14:20 <a href="http://www.cppblog.com/yehongly/archive/2008/09/16/61974.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL vector 容器介绍</title><link>http://www.cppblog.com/yehongly/archive/2008/09/04/60925.html</link><dc:creator>茶</dc:creator><author>茶</author><pubDate>Thu, 04 Sep 2008 09:06:00 GMT</pubDate><guid>http://www.cppblog.com/yehongly/archive/2008/09/04/60925.html</guid><wfw:comment>http://www.cppblog.com/yehongly/comments/60925.html</wfw:comment><comments>http://www.cppblog.com/yehongly/archive/2008/09/04/60925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yehongly/comments/commentRss/60925.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yehongly/services/trackbacks/60925.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: http://blog.csdn.net/masterlee/archive/2004/11/09/174129.aspxSTL vector 容器介绍A Presentation of the STL Vector Container (By Nitron)翻译 masterlee介绍std::vector，并且讨论它在STL中的算法和条件函数remove_if()。&nbs...&nbsp;&nbsp;<a href='http://www.cppblog.com/yehongly/archive/2008/09/04/60925.html'>阅读全文</a><img src ="http://www.cppblog.com/yehongly/aggbug/60925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yehongly/" target="_blank">茶</a> 2008-09-04 17:06 <a href="http://www.cppblog.com/yehongly/archive/2008/09/04/60925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>