string整理筆記

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

int main() {
    string string1;
    string1 = "1234";
    cout << string1 << endl;

    char buf1[] = "12345";
    string string2(buf1);
    cout << string2 << endl;

    char buf2[] = "123456789";
    string string3 = buf2;
    cout << string3 << endl;

    string tmp = "abcdefghijk";
    string string4(tmp, 4);    // 從 index = 4 的位置開始複製直到完畢
    cout << string4 << endl;

    string string5(5, 'A');
    cout << string5 << endl;

    string string6(buf2, 3);    // 複製 buf2 前三個字符
    cout << string6 << endl;


    string string7(string3.begin(), string3.end());
    cout << string7 << endl;

    /*
     * 可用 ==、>、<、>=、<=、和!=比較字符串
     * 可用 + 或者 += 操作符連接兩個字符串
     * 可用[]獲取特定的字符
     */

    if(!string7.empty()) {
        cout << string7.length() << endl;
        cout << string7.size() << endl;
        cout << string7.capacity() << endl; // 返回string對象不需要擴容即可存放在字符數
        cout << string7.max_size() << endl; // 返回string對象中可存放的最大字符串的長度
    }

    /*
     * void resize(int len, char c);
     * 把字符串當前大小置爲 len,多去少補,字符c填充剩下的部分
     */
    string7.resize(12, 'w');
    cout << string7 << endl;

    string7.resize(10, 'a');
    cout << string7 << endl;

    /*
     * string 提供了豐富的查找函數
     * size_type find(const basic_string &str, size_type index);
     * //返回str在字符串中第一次出現的位置(從index開始查找),沒找到則返回string::npos
     * size_type find(const char *str, size_type index);  // 同上
     *
     * size_type find(const char *str, size_type index, size_type length);
     * //返回str在字符串中第一次出現的位置(從index開始查找,長度爲length),如果沒找到就返回string::npos
     *
     * size_type find(char ch, size_type index);  // 返回字符ch在字符串中第一次出現的位置(從index開始查找),如果沒找到就返回string::npos
     */

    string string8 = "0123456789";
    std::size_t pos1 = string8.find("34"); // 必須用 std::size_t
    if(pos1 != string::npos)
        cout << pos1 << endl; //3
    pos1 = string8.find_first_of("34"); // 找到第一個位置
    if(pos1 != string::npos)
        cout << pos1 << endl; //3
    pos1 = string8.find_last_of("34"); // 找到最後一個位置
    if(pos1 != string::npos)
        cout << pos1 << endl; //4
    pos1 = string8.rfind("34"); // 從後往前找
    if(pos1 != string::npos)
        cout << pos1 << endl; //3

    // string &insert(int p,const string &s);  //在p位置插入字符串s
    string t1 = "abc";
    string8.insert(3, t1);
    cout << string8 << endl; //012abc3456789

    // string &replace(int p, int n,const char *s); //刪除從p開始的n個字符,然後在p處插入串s
    string t2 = "def";
    string8.replace(3, 3, t2);
    cout << string8 << endl; //012def3456789

    // string &erase(int p, int n);  //刪除p開始的n個字符,返回修改後的字符串
    string8.erase(3, 3);
    cout << string8 << endl; //0123456789

    // string substr(int pos = 0,int n = npos) const;  //返回pos開始的n個字符組成的字符串
    string string9 = string8.substr(7);
    cout << string9 << endl; //789

    // void swap(string &s2);    //交換當前字符串與s2的值
    cout << string8 << endl; //0123456789
    string9.swap(string8);
    cout << string8 << endl; //789

    // string &append(const char *s);   //把字符串s連接到當前字符串結尾
    string t3 = t1.append(t2);
    cout << t3 << endl; //abcdef

    // void push_back(char c)   //當前字符串尾部加一個字符c
    t3.push_back('e');
    cout << t3 << endl; //abcdefe

    // const char *c_str()const;  //返回一個以null終止的c字符串
    // 即c_str()函數返回一個指向正規C字符串的指針, 內容與本string串相同,用於string轉const char*
    const char *buf3;
    buf3 = t3.c_str();
    cout << buf3 << endl; //abcdefe

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