String 用法 简单API

明天天梯赛选拔,怕不是STl忘到姥姥家去了,赶紧复习一下

构造str

    string s = "fuckyouRMA";
    string s1(s, 2); de(s1);
    string s2(s, 2, 5); de(s2);
    char s3 []= "fuck";
    string s33(s3); de(s33);
    string s4(4, 'c'); de(s4);
    string s5(s.begin(), s.end() - 2); de(s5);
    //这样就可以灵活切割string了
    /*
    s1 ckyouRMA
    s2 ckyou
    s33 fuck
    s4 cccc
    s5 fuckyouR
    */

操作函数

swap
+=
clear
== <= != 可用来比较字符串
size
max_size//s.max_size() 2147483647
empty
[]取单一字符

strstr

    char s1[] = "fuck you leather man";
    char s2[] = "you";
    //前母串,后子串
    char*ptr = strstr(s1, s2);
    de(ptr);//ptr you leather man

str插入与遍历

    string s = "fck you smmy";
    s.insert(s.begin() + 1, 'u');
    de(s);//s fuck you smmy
    //+,+=足以应付一般插入
    each(i, 0, s.size() - 1)
    {
        de(s[i]);
    }
    /*
    s[i] f
    s[i] u
    s[i] c
    s[i] k
    s[i]
    s[i] y
    s[i] o
    s[i] u
    s[i]
    s[i] s
    s[i] m
    s[i] m
    s[i] y
    */

str删除

    string s = "01234567";
    s.erase(s.begin());
    de(s); //s 1234567
    s.erase(s.begin(), s.end ()- 5);
    de(s);//34567
    s.erase(0, 2);
    de(s);//567 左闭右开

str替换

    //replace 将从i开始的n个字符替换成s2
    string s = "fuckyouplease!";
    s.replace(4, 3, "me");
    de(s);
    s.replace(s.size()-1, 1, 6, '.');
    de(s);
    s.replace(s.begin(), s.begin() + 4, "rape");
    de(s);
    /*
    s fuckmeplease!
    s fuckmeplease......
    s rapemeplease......
    */

str查找&比较

    string s = "i wanna fuck you rather than your sister";
    de(s.find("you"));
    de(s.find("you", 15));
    de((int)s.find("mother"));
    /*
    s.find("you") 13
    s.find("you", 15) 29
    (int)s.find("mother") -1
    */
    string s1 = "abbb";
    string s2 = "bcccc";
    string s3 = "cdddd";
    de(s1 > s2); //s1 > s2 0
    de(s3 > s2); //s3 > s2 1

你可能感兴趣的:(String 用法 简单API)