【数据结构与算法】C++String常用备忘

int main(){
    string s1 = "123";
    string s2 = s1;//深拷贝,基本类型也是深拷贝,对象类型浅拷贝
    cout << &s1 << " " << &s2  <<endl;//00DFFA30 00DFFA0C 

    s1.size();//获取长度3

    string s3 = "abc";
    string s4 = "abc";
    cout << (s3 == s4) << endl;//true,不去要equal函数

    s4[0] = 'd';//下标访问取出char,可以修改来改变原string
    cout << s4 << endl;//dbc
    cout << (s4[2]) << endl;//c
    cout << (s4[3]) << endl;//空的
    //cout << (s4[4]) << endl;//程序终止

    int a[] = { 1,2,3 };
    cout << (a[2]) << endl;//3
    cout << (a[3]) << endl;//随机
    cout << (a[4]) << endl;//随机。。。以后也是,int数组可以越界访问,结果随机,string不行。

    string s5 = "a";
    string s6 = "b";
    cout << (s5 > s6) << endl;//两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’’为止。


    //迭代器
    string s7 = "abcdefg";
    for (string::iterator it = s7.begin(); it < s7.end(); it++) {
        cout << *it << endl;//遍历输出    iter->mem  等价于 (*iter).mem   const_interator只能读不能写     reverse_iterator搭配 rbegin~rend 反向遍历  it--往前移动
    }

}
原文地址:https://www.cnblogs.com/ulyssescat/p/10521960.html