basketball  

 

 

String 簡介

string 是以 char 作為模板參數的模板類例項,把字串的記憶體管理責任由 string 負責而不是由編程者負責,

大大減輕了 C 語言風格的字串的麻煩,使用前預先 #include "string" 即可。

 

 

String 特色

* 提供了大量的字串操作函式,如比較、連線、搜尋、替換、獲得子串等。

* 可與 C 語言風格字串雙向轉換。

* 屬於 C++ STL 容器類,使用者自訂的類也可以作為它的模板參數,因此也適用 C++ STL Algorithm 庫。

 

 

成員函式概觀

string 重載(overload)許多操作符(operator),包括 +、+=、<、=、,、[]、<<、>> 等,這些操作符對字符串操作非常方便。

有了操作符以後,assign()、append()、compare()、at() 等函數,除非有一些特殊的需求時,一般是用不上。

 

 

常用方法

* 賦值 operator = : 將字串指定給另一個字串 str1 = str2;

* 相等比較 operator == : 使用==來比較兩個字串的字元內容是否相同。

* 串接字串 operator + : 直接使用 + 運算子來串接字串。

* 存取字符 []、str.at() : 下標運算子[ ]如字元陣列的操作一般 或 str.at(),at 帶邊界檢查。

* 字串長度 size() : 測試字串長度。

* 字串為空 empty() : 測試字串是否為空。

* 字串長度 length() : 傳回字串的長度。

 

 

串接操作符 +

只要你的等式裏面有一個 string 對象,你就可以一直連續 "+",但有一點需要保證的是,在開始的兩項中,

必須有一項是 string 對象(不然 compiler 在解語法樹的時候會出錯)。

 

賦值 operator =

可以將一個 C- Style 的字串指定給 string,但是您不能將一個 string 實例指定給字元陣列。

 

存取字符 []、str.at()

如果你希望效率高,還是使用 [] 來訪問,如果你希望穩定性好,最好使用 at() 來訪問。

 

 

進階用法

assign(string, start, num) : 從 string 的第 start 個字元取出 num 個字元來指定給另一字串物件。

append(string, start, num) : 從 string 的第 start 個字元取出 num 個字元來附加至另一字串物件之後。

find(string, 0) : 從引發 find 的字串物件第 0 個字元尋找是否有符合 string 的子字串。

insert(start, string) : 將 string 插入引發 insert 的字串物件第 start 個字元之後。

 

 

常用的 string 程式寫法

 

範例碼(一):操作符對字符串操作。

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string strinfo="Please input your name:";
    cout << strinfo ;
    cin >> strinfo;
    if( strinfo == "winter" )
        cout << "you are winter!"<<endl;
    else if( strinfo != "wende" )
        cout << "you are not wende!"<<endl;
    else if( strinfo < "winter")
        cout << "your name should be ahead of winter"<<endl;
    else
        cout << "your name should be after of winter"<<endl;
    strinfo += " , Welcome to China!";
    cout << strinfo<<endl;
    cout <<"Your name is :"<<endl;
    string strtmp = "How are you? " + strinfo;
    for(int i = 0 ; i < strtmp.size(); i ++)
        cout<<strtmp[i];
    return 0;
}

執行結果:

Please input your name:Hero

you are not wende!

Hero , Welcome to China!

How are you? Hero , Welcome to China!

 

範例碼(二):進階用法簡易例子。

#include <iostream> 
#include <string> 
using namespace std; 

int main() { 
    string str1; 
    string str2("caterpillar"); 
    string str3(str2); 

    // assign: 指定字串 
    str1 = str1.assign(str2, 0, 5); 
    cout << "str1: " << str1 << endl; 
    str1 = str1.assign("caterpillar", 5, 6); 
    cout << "str1: " << str1 << endl; 

    str1 = ""; 

    // append: 字串串接 
    str1 = str1.append(str2, 0, 5); 
    str1 = str1.append(str3, 5, 6); 
    cout << "str1: " << str1 << endl; 

    // find: 尋找字串位置 
    cout << "尋找str1中的第一個pill: " 
         << str1.find("pill", 0) << endl; 

    // insert: 插入字串 
    cout << "在str1插入字串***: " 
         << str1.insert(5, "***") << endl; 

    cout << "str1長度: " << str1.length() << endl; 
 
    return 0; 
}

執行結果:

str1: cater

str1: pillar

str1: caterpillar

尋找str1中的第一個pill: 5

在str1插入字串***: cater***pillar

str1長度: 14

 

 

string 建議

使用 string 的方便性就不用再說了,這裏要重點強調的是 string 的安全性。

 

string 並不是萬能的,如果你在一個大工程中需要頻繁處理字符串,而且有可能是多線程,那麼你一定要慎重(當然,

在多線程下你使用任何 STL 容器都要慎重)。

 

string 的實現和效率並不一定是你想象的那樣,如果你對大量的字符串操作,而且特別關心其效率,那麼你有兩個選擇,

首先,你可以看看你使用的 STL 版本中 string 實現的源碼;另一選擇是你自己寫一個只提供你需要的功能的類。

 

 

string 的 c_str() 函數是用來得到 C 語言風格的字符串,其返回的指針不能修改其空間。

而且在下一次使用時重新調用獲得新的指針。

string 的 data() 函數返回的字符串指針不會以 '\0' 結束,千萬不可忽視。儘量去使用操作符,這樣可以讓程序更加易懂。

 

難怪有人說:string 使用方便功能強,我們一直用它!

 

 

Reference:http://mropengate.blogspot.tw/2015/07/cc-string-stl.html

Reference:http://openhome.cc/Gossip/CppGossip/string2.html

Reference:https://www.byvoid.com/zht/blog/cpp-string

Reference:https://zh.wikipedia.org/wiki/String_(C%2B%2B%E6%A0%87%E5%87%86%E5%BA%93)

arrow
arrow
    文章標籤
    [String]
    全站熱搜

    mitblog 發表在 痞客邦 留言(0) 人氣()