1. 程式人生 > >C++自帶string類的常用方法

C++自帶string類的常用方法

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


int main()
{
    string str1 = "hello";
    string* str2 = new string("hello");
    string str3 = "world";


    //獲取字串長度
    int length = str1.length();
    cout << "呼叫str.length()函式獲取字串長度:" << length << endl;
    cout << endl;




    //字串連線
    string str4 = str1 + str3;
    cout << "字串連線結果:" << str4 << endl;
    cout << endl;




    //字串比較
    if (str1 < str3)
        cout << "字串比較:" << "str1<str2" << endl;
    cout << endl;




    //獲取字串的第一個字元
    string::const_iterator it = str1.begin();
    cout << *it << endl;
    cout << endl;




    //獲取字串的最後一個字元
    it = str1.end();//end是指向最後一個字元後面的元素,而且不能輸出,所以cout << *it << endl;這樣輸出會報錯
    it--;
    cout << *it << endl;
    cout << endl;




    //倒置串
    reverse(str1.begin(), str1.end());
    cout << "倒置串:" << str1 << endl;
    cout << endl;


    //字串轉字元陣列
    //不推薦的用法,但是需要了解
    string a = "abc123";
    const char *b;//這裡必須為const char *,不能用char *,不然下一句會報錯
    b = a.c_str();
    cout << "a:" << a << endl;
    cout << "b:" << b << endl;
    a = "asd456";
    cout << "a:" << a << endl;
    cout << "b:" << b << endl;
    //推薦用法
    string c = "abc123";
    char *d = new char[20];
    strcpy(d, c.c_str());//因為這裡沒有直接賦值,所以指標型別可以不用const char *
    cout << "c:" << c << endl;
    cout << "d:" << d << endl;
    c = "asd456";
    cout << "c:" << c << endl;
    cout << "d:" << d << endl;
    cout << endl;




    //查詢串
    //find-從指定位置起向後查詢,直到串尾
    string st1("babbabab");
    cout << st1.find('a') << endl;//1,預設從位置0(即第1個字元)開始查詢
    cout << st1.find('a', 2) << endl;//4   在st1中,從位置2(b,包括位置2)開始,查詢a,返回首次匹配的位置
    cout << (st1.find('c', 0) == -1) << endl;//1 
    cout << (st1.find('c', 0) == 4294967295) << endl;//1   兩句均輸出1,原因是計算機中-1和4294967295都表示為32個1(二進位制)
    string st2("aabcbcabcbabcc");
    str1 = "abc";
    cout << st2.find(str1, 2) << endl;//6,從st2的位置2(b)開始匹配,返回第一次成功匹配時匹配的串(abc)的首字元在st2中的位置,失敗返回-1
    cout << st2.find("abcdefg", 2, 3) << endl;//6   取abcdefg得前3個字元(abc)參與匹配,相當於st2.find("abc", 2)


    //rfind-從指定位置起向前查詢,直到串首
    cout << st1.rfind('a', 7) << endl;//6


    //find_first_of-在源串中從位置pos起往後查詢,只要在源串中遇到一個字元,該字元與目標串中任意一個字元相同,就停止查詢,返回該字元在源串中的位置;若匹配失敗,返回-1
    string str6("bcgjhikl");
    string str7("kghlj");
    cout << str6.find_first_of(str7, 0) << endl;//2,從str1的第0個字元b開始找,g與str2中的g匹配,停止查詢,返回g在str1中的位置2
    
    //find_last_of-與find_first_of函式相似,只不過查詢順序是從指定位置向前
    string str("abcdecg");
    cout << str.find_last_of("hjlywkcipn", 6) << endl;//5,從str的位置6(g)開始向前找,g不匹配,再找c,c匹配,停止查詢,返回c在str中的位置5


    //find_first_not_of-在源串中從位置pos開始往後查詢,只要在源串遇到一個字元,與目標串中的任意字元都不相同,就停止查詢,返回該字元在源串中的位置;若遍歷完整個源串,都找不到滿足條件的字元,則返回-1
    cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3   從源串str的位置0(a)開始查詢,目標串中有a,匹配,..,找d,目標串中沒有d(不匹配),停止查詢,返回d在str中的位置3


    //find_last_not_of-與find_first_not_of相似,只不過查詢順序是從指定位置向前
    cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3


    system("pause");
    return 0;


}