std string 的常用使用

來源:互聯網
上載者:User
用 string來代替char * 數組,使用sort排序演算法來排序,用unique 函數來去重
1、Define
        string s1 = "hello";
        string s2 = "world";
        string s3 = s1 + "," + s2 +"!\n";
2、append
        s1 += ",shanshan\n";
3、Compare
        if(s1 == s2)
           .....
        else if(s1 == "hello")
           .....
4、 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;
}

5、find函數
由於尋找是使用最為頻繁的功能之一,string 提供了非常豐富的尋找函數。其列表如下:

函數名 描述
find 尋找
rfind 反向尋找
find_first_of 尋找包含子串中的任何字元,返回第一個位置
find_first_not_of 尋找不包含子串中的任何字元,返回第一個位置
find_last_of 尋找包含子串中的任何字元,返回最後一個位置
find_last_not_of 尋找不包含子串中的任何字元,返回最後一個位置

以上函數都是被重載了4次,以下是以find_first_of 函數為例說明他們的參數,其他函數和其參數一樣,也就是說總共有24個函數:

size_type find_first_of(const basic_string& s, size_type pos = 0)
size_type find_first_of(const charT* s, size_type pos, size_type n)
size_type find_first_of(const charT* s, size_type pos = 0)
size_type find_first_of(charT c, size_type pos = 0)

所有的尋找函數都返回一個size_type類型,這個傳回值一般都是所找到字串的位置,如果沒有找到,則返回string::npos。
其實string::npos表示的是-1。即沒找到就返回-1。例子如下:
#include <string>
#include <iostream>
using namespace std;
int main(){
string strinfo=" //*---Hello Word!......------";
string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int first = strinfo.find_first_of(strset);
if(first == string::npos) {
cout<<"not find any characters"<<endl;
return -1;
}
int last = strinfo.find_last_of(strset);
if(last == string::npos) {
cout<<"not find any characters"<<endl;
return -1;
}
cout << strinfo.substr(first, last - first + 1)<<endl;//string.substr是子串
return 0;
}
6、insert函數, replace函數和erase函數

string只是提供了按照位置和區間的replace函數,而不能用一個string字串來替換指定string中的另一個字串。
例子:
#include <string>
#include <iostream>
using namespace std;
int main() {
string strinfo="This is Winter, Winter is a programmer. Do you know Winter?";
cout<<"Orign string is :\n"<<strinfo<<endl;
string_replace(strinfo, "Winter", "wende");
cout<<"After replace Winter with wende, the string is :\n"<<strinfo<<endl;
return 0;
}

string.erase(pos,srclen);//srclen是刪除的長度
string.insert(pos,strdst); //pos是定位,strdst是插入的函數
void string_replace(string & strBig, const string & strsrc, const string &strdst) {
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos){
strBig.erase(pos, srclen);
strBig.insert(pos, strdst);
pos += dstlen;
}
}

相關連結:http://www.stlchina.org/twiki/bin/view.pl/Main/STLDetailString
 
7、切割字串
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string text = "big|dog|china|sonic|free";
stringstream ss(text);
string sub_str;
while(getline(ss,sub_str,'|')) //以|為間隔分割test的內容
cout << sub_str << endl;

return 0;
}
輸出如下:
big
dog
china
sonic
free

8、建構函式和解構函式
string s 產生一個Null 字元串S
string s(str) Copy建構函式,產生字串Str的一個複製品
string s(str,stridx) 將字串Str內始於位置Stridx的部分,當作字串S的初值
string s(str,stridx,strlen) 將字串Str內始於位置Stridx且長度為strlen的部分,當作字串S的初值

string s(cstr)            以C-String cstr作為S的初值
string s(num,c)           產生一個字串,包含Num個C字元
string s(beg,end)         以區間[beg,end]內的字元作為s初值
s.~string()               銷毀所有字元,釋放記憶體
注意:
std::string s('x');//error
std::string s(1,'x'); //ok,create a string that has one charactor 'x'

9、substr(string.substr的方法)

(1)參數 start:Number -- 一個整數,指示 my_str 中用於建立子字串的第一個字元的位置。如果 start 為一個負數,則起始位置從字串的結尾開始確定,其中 -1 表示最後一個字元。 length:Number -- 要建立的子字串中的字元數。如果沒有指定 length,則子字串包括從字串開頭到字串結尾的所有字元。(2)返回String -- 指定字串的子字串。(3)樣本下面的樣本建立一個新字串 my_str,並使用 substr() 返回該字串中的第二個單詞;首先,使用正的 start 參數,然後使用負的 start 參數: var my_str:String = new String("Hello world");
var mySubstring:String = new String();
mySubstring = my_str.substr(6,5);
trace(mySubstring); // 輸出:world

mySubstring = my_str.substr(-5,5);
trace(mySubstring); // 輸出:world

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.