C++string類常用的操作函數

#include <iostream>
#include <string>//必須包含該C++頭文件

using namespace std;

int main()
{
    //string對象的初始化,也就是構造函數與析構函數方面的知識
    /*
    string s;  //生成一個空字符串s
    string s(str); //拷貝構造函數 生成str的複製品
    string s(str, stridx); //將字符串str內“始於位置stridx”的部分當作字符串的初值
    string s(str, stridx, strlen); //將字符串str內“始於stridx且長度頂多strlen”的部分作爲字符串的初值
    string s(cstr); //將C字符串作爲s的初值
    string s(chars, chars_len); //將C字符串前chars_len個字符作爲字符串s的初值。
    string s(num, c); //生成一個字符串,包含num個c字符
    string s(beg, end); //以區間beg;end(不包含end)內的字符作爲字符串s的初值
    s.~string(); //銷燬所有字符,釋放內存
    */

    //string類的begin,end,rbegin,rend的用法,返回的是迭代器,不是簡單的數組下標,迭代器,反向迭代器
    string str;
    str = "abcdefg higklmn";
    string::iterator beginIter = str.begin();
    string::iterator endIter = str.end();
    string::reverse_iterator rbeginIter = str.rbegin();
    string::reverse_iterator rendIter = str.rend();

    cout << *beginIter << endl;
    //cout << *endIter << endl;//錯誤,end()函數指向字符串末尾的\0的後面一個位置,無法輸出
    cout << *(endIter - 1) << endl;//這就對了

    cout << *rbeginIter << endl;//同上,只不過是反過來的而已
    cout << *(rendIter - 1) << endl;

    for (string::iterator iter = beginIter; iter != endIter; ++iter)//用迭代器與反向迭代器遍歷
        cout << *iter;
    cout << endl;
    for (string::reverse_iterator rev_iter = rbeginIter; rev_iter != rendIter; ++rev_iter)
        cout << *rev_iter;
    cout << endl;

    //size()與length()函數,都是返回字符串的長度(不包括尾部的‘\0’)
    int size = str.size();
    int length = str.length();
    cout << size << " " << length << endl;
    //字符串可能的最大大小max_size()
    int maxSize1 = str.max_size();
    cout << maxSize1 << endl;
    //string s1;
    //int maxSize2 = s1.max_size();
    //cout << maxSize2 << endl;

    //字符串的容量capacity()
    cout << str.capacity() << endl;
    string s2;
    cout << s2.capacity() << endl;

    //判空empty()
    bool isEmpty = str.empty();
    if (isEmpty) cout << "爲空!" << endl;
    else cout << "不爲空!" << endl;
    string s3;
    isEmpty = s3.empty();
    if (isEmpty) cout << "爲空!" << endl;
    else cout << "不爲空!" << endl;

    //operator[]    取第幾個元素,相當於數組
    cout << str[0] << endl;
    for (int i = 0; i < str.size(); ++i)
        cout << str[i];
    cout << endl;

    //標準庫的string類提供了3個成員函數來從一個string得到c類型的字符數組:c_str()、data()、copy(p, n)。
    const char* c;
    string s4 = "1234";
    c = s4.c_str();
    cout << c << endl; //輸出:1234  
    s4 = "abcd";
    cout << c << endl; //輸出:abcd
    const char* c1;
    string s5 = "1234";
    c1 = s5.data();
    cout << c1 << endl; //輸出:1234  
    s5 = "abcd";
    cout << c1 << endl; //輸出:abcd

    //operator= 賦值操作符,字符串可以直接賦值
    string s6 = "hahahaha";
    string s7;
    s7 = s6;
    cout << s7 << endl;

    //reserve函數 函數reserve()將字符串的容量設置爲至少size. 
    //如果size指定的數值要小於當前字符串中的字符數(亦即size < this→size()), 容量將被設置爲可以恰好容納字符的數值
    string s8 = "1234";
    cout << s8.capacity() << endl;
    s8.reserve(32);//10,20,30,40,50等等
    cout << s8.capacity() << endl;

    //swap  交換函數,交換兩個字符串
    string s9 = "aaa";
    string s10 = "bbb";
    swap(s9, s10);
    cout << s9 << " " << s10 << endl;

    //reverse函數,字符串翻轉
    string s11 = "123";
    reverse(s11.begin(), s11.end());
    cout << s11 << endl;
    reverse(s11.begin(), s11.begin() + 3);
    cout << s11 << endl;

    //insert函數
    //string的成員函數insert有以下多種重載:
    //string &insert(int p0, const char *s); ——在p0位置插入字符串s
    //string &insert(int p0, const char *s, int n); ——在p0位置插入字符串s的前n個字符
    //string &insert(int p0, const string &s); ——在p0位置插入字符串s
    //string &insert(int p0, const string &s, int pos, int n); ——在p0位置插入字符串s從pos開始的連續n個字符
    //string &insert(int p0, int n, char c);//在p0處插入n個字符c
    //iterator insert(iterator it, char c);//在it處插入字符c,返回插入後迭代器的位置
    //void insert(iterator it, const_iterator first, const_iteratorlast);//在it處插入從first開始至last-1的所有字符
    //void insert(iterator it, int n, char c);//在it處插入n個字符c

    string s12 = "aaaaaaaaa";
    char* s13 = "bbb";
    s12 = s12.insert(0, s13);
    s12 = s12.insert(1, s13, 2);
    string s14 = "ccc";
    s12 = s12.insert(3, s14);
    string s15 = "dddde";
    s12 = s12.insert(5, s15, 2, 3);
    s12 = s12.insert(6, 3, 'f');
    string::iterator iter = s12.begin();
    iter++;
    string::iterator iter2 = s12.insert(iter, 'g');
    cout << *iter2 << endl;
    cout << s12 << endl;

    //append函數是向string 的後面追加字符或字符串
    //(1)向string 的後面加C - string
    string s16("Hello "); // s=”Hello ”
    const char* c3 = "Out There ";
    s16.append(c3); // s=”Hello Out There”
    cout << s16 << endl;
    //(2)向string 的後面加C-string 的一部分
    string s17("Hello "); // s=”Hello ”
    const char *c4 = "Out There ";
    s17.append(c4, 3); // s=”Hello Out”
    cout << s17 << endl;
    //(3)向string 的後面加string(有兩種方法)
    string s18("Hello "), s19("Wide "), s20("World ");
    s18.append(s19); // s1=”Hello Wide”
    s18 += s20; // s1=”Hello Wide World”
    cout << s18 << endl;
    //(4)向string 的後面加string 的一部分 ---A
    string s21("Hello "), s22("Wide World ");
    s21.append(s22, 5, 5); // s1=”Hello World”
    cout << s21 << endl;
    //(5)向string 的後面加string 的一部分 ---B
    string str1f("Hello "), str2f("Wide World");
    str1f.append(str2f.begin() + 5, str2f.end());
    cout << str1f << endl;
    //(6)向string 的後面加多個字符
    string str1e("Hello ");
    str1e.append(4, '!'); // s1=”Hello !!!!”
    cout << str1e << endl;

    //push_back,類似於vector中的在尾部添加元素
    str1e.push_back(')');
    cout << str1e << endl;

    //賦值運算符=,等於,不等於大於小於,+=,<<等都能直接使用

    //erase函數,刪除字符串中字符的函數
    /*erase函數的原型如下:
        (1)string& erase(size_t pos = 0, size_t n = npos);
        (2)iterator erase(iterator position);
        (3)iterator erase(iterator first, iterator last);
    也就是說有三種用法:
        (1)erase(pos, n); 刪除從pos開始的n個字符,比如erase(0, 1)就是刪除第一個字符
        (2)erase(position); 刪除position處的一個字符(position是個string類型的迭代器)
        (3)erase(first, last); 刪除從first到last之間的字符(first和last都是迭代器)
    */
    str1e.erase(0, 2);
    cout << str1e << endl;
    str1e.erase(str1e.begin());
    cout << str1e << endl;
    str1e.erase(str1e.begin() + 3, str1e.end() - 2);//不包括最後一個迭代器
    cout << str1e << endl;

    //clear()函數,清空字符串
    str1e.clear();
    cout << str1e.size() << endl;//爲0

    //resize()函數,分配空間大小
    str1e.resize(10);
    cout << str1e.length() << endl;//空間大小爲10

    //assign()函數,C++ string 類的成員函數,用於拷貝、賦值操作,
    //它們允許我們順次地把一個 string 對象的部分內容拷貝到另一個 string 對象上
    /*
    string &operator=(const string &s);               把字符串s賦給當前字符串
    string &assign(const char *s);                 用c類型字符串s賦值
    string &assign(const char *s, int n);              用c字符串s開始的n個字符賦值
    string &assign(const string &s);                 把字符串s賦給當前字符串
    string &assign(int n, char c);                  用n個字符c賦值給當前字符串
    string &assign(const string &s, int start, int n);       把字符串s中從start開始的n個字符賦給當前字符串
    string &assign(const_iterator first, const_itertor last);  把first和last迭代器之間的部分賦給字符串
    */
    char* c5 = "aaa";
    string s23;
    s23.assign(c5);
    cout << s23 << endl;
    char* c6 = "bbbbbb";
    s23 = s23.assign(c6, 3);
    cout << s23 << endl;
    string s24 = "ccc";
    s23 = s23.assign(s24);
    cout << s23 << endl;
    s23.assign(3, 'd');
    cout << s23 << endl;
    s23 = s23.assign("eeee", 0, 3);
    cout << s23 << endl;
    string s25 = "fff";
    s23 = s23.assign(s25.begin(), s25.end());
    cout << s23 << endl;

    //repalce函數,用於替換
    /*用法一:
    *用str替換指定字符串從起始位置pos開始長度爲len的字符
    *string& replace (size_t pos, size_t len, const string& str);
    */
    string line1 = "this@ is@ a test string!";
    line1 = line1.replace(line1.find("@"), 1, ""); //從第一個@位置替換第一個@爲空  
    cout << line1 << endl;
    /*用法二:
    *用str替換 迭代器起始位置 和 結束位置 的字符
    *string& replace (const_iterator i1, const_iterator i2, const string& str);
    */
    string line2 = "this@ is@ a test string!";
    line2 = line2.replace(line2.begin(), line2.begin() + 6, "heihei ");  //用str替換從begin位置開始的6個字符  
    cout << line2 << endl;
    /*用法三:
    *用substr的指定子串(給定起始位置和長度)替換從指定位置上的字符串
    *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
    */
    string line3 = "this@ is@ a test string!";
    string substr = "12345";
    line3 = line3.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(從1位置數共3個字符)替換從0到5位置上的line  
    cout << line3 << endl;
    /*用法四:string轉char*時編譯器可能會報出警告,不建議這樣做
    *用str替換從指定位置0開始長度爲5的字符串
    *string& replace(size_t pos, size_t len, const char* s);
    */
    string line4 = "this@ is@ a test string!";
    char* str111 = "12345";
    line4 = line4.replace(0, 5, str111); //用str替換從指定位置0開始長度爲5的字符串  
    cout << line4 << endl;
    /*用法五:string轉char*時編譯器可能會報出警告,不建議這樣做
    *用str替換從指定迭代器位置的字符串
    *string& replace (const_iterator i1, const_iterator i2, const char* s);
    */
    string line5 = "this@ is@ a test string!";
    char* str222 = "12345";
    line5 = line5.replace(line5.begin(), line5.begin() + 9, str222); //用str替換從指定迭代器位置的字符串  
    cout << line5 << endl;
    /*用法六:string轉char*時編譯器可能會報出警告,不建議這樣做
    *用s的前n個字符替換從開始位置pos長度爲len的字符串
    *string& replace(size_t pos, size_t len, const char* s, size_t n);
    */
    string line6 = "this@ is@ a test string!";
    char* str333 = "12345";
    line6 = line6.replace(0, 9, str333, 4);  //用str的前4個字符替換從0位置開始長度爲9的字符串  
    cout << line6 << endl;
    /*用法七:string轉char*時編譯器可能會報出警告,不建議這樣做
    *用s的前n個字符替換指定迭代器位置(從i1到i2)的字符串
    *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
    */
    string line7 = "this@ is@ a test string!";
    char* str444 = "12345";
    line7 = line7.replace(line7.begin(), line7.begin() + 9, str444, 4);  //用str的前4個字符替換指定迭代器位置的字符串  
    cout << line7 << endl;
    /*用法八:
    *用重複n次的c字符替換從指定位置pos長度爲len的內容
    *string& replace (size_t pos, size_t len, size_t n, char c);
    */
    string line8 = "this@ is@ a test string!";
    char c111 = '1';
    line8 = line8.replace(0, 9, 3, c111);    //用重複3次的c字符替換從指定位置0長度爲9的內容  
    cout << line8 << endl;
    /*用法九:
    *用重複n次的c字符替換從指定迭代器位置(從i1開始到結束)的內容
    *string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
    */
    string line9 = "this@ is@ a test string!";
    char c222 = '1';
    line9 = line9.replace(line9.begin(), line9.begin() + 9, 3, c222);    //用重複3次的c字符替換從指定迭代器位置的內容  
    cout << line9 << endl;

    //find函數,查找,返回目標的頭位置的索引,否則返回string::npos
    //size_type find(const string & str, size_type pos = 0) const;
    //size_type find(const char * s, size_type pos = 0) const;
    //size_type find(const char * s, size_type pos = 0, size_type n) const;
    //size_type find(const char ch, size_type pos = 0) const;
    //rfind()查找子字符串或字符最後一次出現的位置。
    //find_first_of()方法在字符串中查找參數中任何一個字符或字符串首次出現的位置
    //find_last_of()方法在字符串中查找參數中任何一個字符或字符串最後一次出現的位置
    string s111("1a2b3c4d5e6f7jkg8h9ija2b3c4d5e6f7g8ha9i");
    string::size_type position;
    position = s111.find("jk", 0);//返回第一個出現的位置,從0開始
    if (position != string::npos) cout << position << endl;
    else cout << "Can't find!" << endl;

    string flag = "c";
    position = s111.find_first_of(flag);
    printf("s.find_first_of(flag) is :%d\n", position);
    position = s111.find_last_of(flag);
    printf("s.find_last_of(flag) is :%d\n", position);
    //從字符串s 下標5開始,查找字符串b ,返回b 在s 中的下標
    position = s111.find("b", 5);
    cout << "s111.find(b,5) is : " << position << endl;
    position = s111.rfind("c");//查找最後一個出現的位置,也就是反向查找第一次出現的位置,
                             //通常我們可以這樣來使用,當正向查找與反向查找得到的位置不相同說明子串不唯一。
    if (position != string::npos) cout << position << endl;

    flag = "a";//查找所有的位置
    position = 0;
    int i = 1;
    while ((position = s111.find(flag, position)) != string::npos)
    {
        cout << "position  " << i << " : " << position << endl;
        position++;
        i++;
    }

    //substr函數,得到子串,三種用法
    string x = "Hello_World";
    cout << x.substr() << endl;//截取整個串
    cout << x.substr(5) << endl;//從位置5開始截取子串
    cout << x.substr(0, 5) << endl;//從位置0開始,截取長度爲5的子串

    //compare()函數,前面減去後面的ASCII碼,>0返回1,<0返回-1,相同返回0
    string str1("green apple");
    string str2("red apple");
    string str3("apple");
    if (str3.compare("apple") == 0)//直接整個比較
        cout << str3 << " is an apple!" << endl;
    if (str1.compare(str2) != 0)
        cout << str1 << " is not " << str2 << endl;
    if (str1.compare(6, 5, "apple") == 0)//從位置6開始比較,比較5個字符
        cout << "still, " << str1 << " is an apple!" << endl;
    if (str2.compare(str2.size() - 5, 5, "apple") == 0)
        cout << "and " << str2 << " is also an apple!" << endl;
    if (str1.compare(6, 5, str2, 4, 5) == 0)//從str1的位置6,str2的位置4開始比較
        cout << "therefore, both are apples!" << endl;
    string a("aBcdef");
    string b("AbcdEf");
    string c123("123456");
    string d("123dfg");
    //下面是各種比較方法
    //前面減去後面的ASCII碼,>0返回1,<0返回-1,相同返回0
    //完整比較a和b
    int m = a.compare(b);
    //“Bcdef”和“AbcdEf”的比較,比較a和b的從1到5位,b剩下的不要比較
    int n = a.compare(1, 5, b);
    //“Bcdef”和“Ef”的比較
    int p = a.compare(1, 5, b, 4, 2);
    //"123"和“123”的比較
    int q = c123.compare(0, 3, d, 0, 3);
    cout << "m=" << m << ",n=" << n << ",p=" << p << ",q=" << q << endl;

    //getline函數,從輸入流中讀入一行,以換行符\n結束


    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章