1.length()与size() length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。 string类的size()/length()方法返回的是字节数,不管是否有汉字。两者原型如下: size_type __CLR_OR_THIS_CALL length() const { // return length of sequence return (_Mysize); } size_type __CLR_OR_THIS_CALL size() const { // return length of sequence return (_Mysize); } 可见两者没有区别。2.capacity()对这个函数的理解为: 当我们定义了一个string变量,如string str("abcdefg");或string str1="abcdefg";那么编译器就会为它分配空间,而capacity()返回的就是这个空间的大小(按字节算)。通常实际分配的空间比字符串的实际长度要大。这是一种优化,因为当我们再向原串加入一些字符(不超过原来的capacity()值)的话,就不用再次分配空间了。从下面的例子可以看出,当string变得比较大时,空间分配并不再遵循n*16-1这样的规律,空间分配变得不是那么大方了。另外,并不是所有的编译器都会为string多分配空间,比如CodeBlocks12.11上(GCC编译器),string str1 = "ab";cout << str1.capacity() << endl;的结果就是2.以下结果来自VS2013#include<iostream>#include<string>#include<fstream>using namespace std;int main(){ string str1 = "ab"; cout << str1.capacity() << endl;//15 str1 += "c"; cout << str1.capacity() << endl; //15 str1 += "defghi"; cout << str1.capacity() << endl;//15 str1 += "haohao";//等于15个 cout << str1.capacity() << endl;//15 str1 += "x";//超过15个 cout << str1.capacity() << endl;//31 ifstream readfile("zpc2.txt", ios::in); if (!readfile){ cout << "程序出现异常,自动退出!" << endl; return 0; } string str, str2; while (!readfile.eof()) { getline(readfile, str2); str += str2; str += " "; } readfile.close(); cout << str.length() << endl;//913 cout << str.capacity() << endl;//1126 return 0;}3.reserve()原型: void reserve( size_type _Count = 0 );功能:函数reserve()将字符串的容量设置为至少size. 如果size指定的数值要小于当前字符串中的字符数(亦即size < this→size()), 容量将被设置为可以恰好容纳字符的数值。它最大的 用处是为了避免反复重新分配缓冲区内存而导致效率降低,或者在使用某些STL操作(例如std::copy)之前保证缓冲区够大。但在有些编译器上,reserve()并不怎么起作用。#include<iostream>#include<string>using namespace std;struct Mystr{ string str; Mystr() { str = "abcdefiunyhiluyntv5eco8unmomusb nbjhg bj kkiubhno"; str.reserve(20); }};int main(){ string str1 = "abcd"; str1.reserve(6); cout << str1.length() << endl;//4 4 cout << str1.capacity() << endl;//15 8 string str2 = "abcd"; str1.reserve(50); cout << str2.length() << endl;//4 4 cout << str2.capacity() << endl;//15 4 string str3; str3.reserve(6); cout << str3.length() << endl;//0 0 cout << str3.capacity() << endl;//15 6 Mystr mystr; cout << sizeof(mystr) << endl;//28 4 return 0;}上面的输出结果中,前一个来自于VS2013,后一个来自于CodeBlocks12.11。从输出结果来看,reserve()的结果毫无规律可循,并且似乎并没有起到它应有的效果。所以,根据以上情况,对于capacity()和reserve(),我们的态度是:能不用就不用。即使要用,也要实现确定它们在当前环境下的表现。4.resize()原型:void resize( size_type size, char val = char() );功能: 改变原有字符串的长度,size指定新长度,当size大于原长度时,多出的部分用val来填充,如果为指定val,则val默认为空格;当size小于原长度时,从开 始起截取size个字符,即相当于把后面的部分删除。#include<iostream>#include<string>using namespace std;struct Mystr{ string str; Mystr() { str = "abc"; str.resize(9); }};int main(){ string str1 = "ab"; str1.resize(6); cout << str1 << endl;//ab+4个空格 ab+4个空格 cout << str1.length() << endl;//6 6 cout << str1.capacity() << endl;//15 6 string str2 = "abcdefg"; str2.resize(5); cout << str2 << endl;//abcde abcde cout << str2.length() << endl;//5 5 cout << str2.capacity() << endl;//15 7 string str3 = "abc"; str3.resize(5, "a"); cout << str3 << endl;//abcaa abcaa cout << str3.length() << endl;//5 5 cout << str3.capacity() << endl;//15 6 string str4 = "abcdefg"; str4.resize(5, "a");//此时"a"将不起作用 cout << str4 << endl;//abcde abcde cout << str4.length() << endl;//5 5 cout << str4.capacity() << endl;//15 7 Mystr mystr; cout << sizeof(mystr) << endl;//28 4 return 0;}以上两个输出对应的环境同上。5.max_size()返回string对象最多可包含的字符数。当程序执行了长度超过max_size()的string操作,编译器会抛出length_error异常。max_size()的值与编译器有关,对于不同的编译器,max_size()的值不一定相同。#include<iostream>#include<string>using namespace std;int main(){ string str1 = "abcdefg"; cout << str1.max_size() << endl;//4294967294 1073741820 str1.resize(4294967300);//出现警告 无警告无错误 return 0;}以上两个输出对应的环境同上。------------------------------分割线------------------------------
C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码 http://www.linuxidc.com/Linux/2014-05/101227.htm读C++ Primer 之构造函数陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm读C++ Primer 之智能指针 http://www.linuxidc.com/Linux/2011-08/40177.htm读C++ Primer 之句柄类 http://www.linuxidc.com/Linux/2011-08/40175.htm
将C语言梳理一下,分布在以下10个章节中:- Linux-C成长之路(一):Linux下C编程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
- Linux-C成长之路(二):基本数据类型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
- Linux-C成长之路(三):基本IO函数操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
- Linux-C成长之路(四):运算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
- Linux-C成长之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
- Linux-C成长之路(六):函数要义 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
- Linux-C成长之路(七):数组与指针 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
- Linux-C成长之路(八):存储类,动态内存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
- Linux-C成长之路(九):复合数据类型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
- Linux-C成长之路(十):其他高级议题
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-05/116873.htm