1. 程式人生 > >C++ string的最常用的用法總結

C++ string的最常用的用法總結

1.string的構造方法

string s(cp);
string s(cp, n);
string s(s1);
string s(s1, pos2);
string s(s1, pos2, len2);

2.substr()

s.substr(pos, n):返回一個string,包含s中從pos開始的n個字元的拷貝。pos的預設值為0,n的預設值為
                  s.size()-pos.即拷貝從pos開始的所有字元。

3.insert()

basic_string& insert(size_type __pos, const basic_string& __s)
功能:向string中pos位置插入__s
用法:
    string str1("hello");
    string str2("world");
    str2.insert(str2.size(), str1);    //str2="worldhello"  
    
basic_string& insert(size_type __pos, const basic_string& __s,size_type __beg, size_type __n)
功能:向string中的pos位置插入__s中從__beg位置開始的__n個字元
用法:
    str2.insert(0, str1, 2, 3);  //向str2中的0位置之前插入str1中從位置2開始的3個字元
​
basic_string& insert(size_type __pos, const _CharT* __s, size_type __n)
功能:向string中的__pos位置插入__s開始的__n個字元
用法:
    str2.insert(0, cp, 1);      //向str2中的0號位置之前插入從地址cp開始的1個字元
​
basic_string& insert(size_type __pos, const _CharT* __s)
功能:向string中的__pos位置插入__s
用法:str2.insert(0, cp);    
​
basic_string& insert(size_type __pos, size_type __n, _CharT __c)
功能:向string中的__pos位置,插入__n個__c字元
用法:str2.insert(0, 10, '*');
​
void insert(iterator __p, _InputIter __first, _InputIter __last)
功能:向string中從迭代器__p開始的位置插入[__first, __last)之間的位置
用法:str2.insert(str2.begin(), str1.begin(), str1.end());

4.erase()

s.erase(pos, len);   刪除從位置pos開始的len個字元。如果len被省略,則刪除從pos開始直至s末尾的所有
                     字元。返回一個指向s的引用
s.erase(beg, end);   刪除迭代器範圍[beg, end)內的元素

5.append()

s.append(args)   將args追加到s。返回一個指向s的引用

args的形式如下:
str
str, pos, len
cp, len
cp
n, c
b, e

6.replace()

s.replace(range, args)    刪除s中範圍range內的字元,替換為args指定的字元。range或者是一個下標和
                          一個長度,或者是一個一對指向s的迭代器。返回一個指向s的引用
​
s.replace(11, 3, "3th");  //刪除s中從位置11開始的3個字元並插入字串"3th"

7.find()

s.find(args)              查詢s中args第一次出現的位置
s.rfind(args)             查詢s中args最後一次出現的位置
s.find_first_of(args)     在s中查詢args中任何一個字元第一次出現的位置
s.find_last_of(args)      在s中查詢args中任意一個字元最後一次出現的位置
s.find_first_not_of(args) 在s中查詢第一個不在args中的字元
s.find_last_not_of(args)  在s中查詢最後一個不在args中的字元
​
args必須是一下形式之一:
c, pos     從s中位置pos開始查詢字元c。pos預設為0
s2, pos    從s中位置pos開始查詢字串s2.pos預設為0
cp, pos    從s中位置pos開始查詢指標cp指向的以空字元結尾的C風格字串。pos預設為0
cp, pos, n 從s中位置pos開始查詢指標cp指向的陣列的前n個字元。pos和n無預設值
​
注意:若找到,則返回第一個匹配位置的下標,否則返回string::npos。

8.常用函式

to_string(val)  :一組過載函式,返回陣列val的string表示。val可以算是任何算術型別。對每個浮點類 
                  型和int或更大的整型都有相應版本的to_string()。
stoi(s, p, b)   :返回s的起始子串(表示整數內容)的數值,返回值型別為int、long、unsigned 
                  long、long long、unsigned long long.
stol(s, p, b)     b表示轉換所用的基數,預設值為10.p是size_t的指標,用來儲存s stoul(s, p, b)
                  中第一個非數字字元的下標
stoll(s, p, b)
stoull(s, p, b)
stof(s, p)
stod(s, p)
stold(s, p)
​


排序  sort(str.begn(), str.end());   

●編寫一個函式,接收三個string引數s、oldValue、newValue。實現將s中所有oldValue替換為newValue

void replace_string(string &src, const string &oldValue, const string &newValue)
{
    int index = 0;
    while( (index = src.find(oldValue)) != string::npos)
    {
        src.replace(index, oldValue.size(), newValue);
        index += newValue;   //防止oldValue和newValue相等的情況
    }
}

●編寫一個函式,以特定字元分割字串

char str[] = "I,am a student;hello world";
const char *delimiter = ",; !";
split(str, delimiter);

void split(char *str, const char *delimiter)
{
    char *p2 = strtok(str, delimiter);
    while (p2 != NULL)
    {
        cout << p2 << endl;
        p2 = strtok(NULL, delimiter);
    }
}

●編寫一個函式分離文字中的每一個單詞

string line, word;
while (getline(cin, line))
{
    istringstream record(line);
    while (record>>word)
    {
        cout << word << endl;
    }
}