C++string中有關大小和容量的函數淺析


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;

}

以上兩個輸出對應的環境同上。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM