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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章