【数据结构与算法】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值大小相比较),直到出现不同的字符或遇’\0’为止。


    //迭代器
    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值