• 【STL】string类(中)


    目录

    1,rbegin 和 rend

    2,reserve & capacity

    3,max_size ( )

    4,size()& resize

    1,void resize (size_t,char c)

    5,push_back & append

    1,追加字符串范围

    2,直接追加


    1,rbegin 和 rend

    具体详情:cplusplus.com/reference/string/string/rbegin/

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. string s1("hello world");
    7. string::reverse_iterator it = s1.rbegin();
    8. while (it != s1.rend())
    9. {
    10. cout << *it << " ";
    11. it++;
    12. }
    13. return 0;
    14. }

    其实也就是逆置打印字符串;

    而且缺点也很明显,类型太长了不便于书写;

    所以 auto 的好用之处就体现出来了,自动帮我们推算类型;

    1. int main()
    2. {
    3. string s1("hello world");
    4. //string::reverse_iterator it = s1.rbegin();
    5. auto it = s1.rbegin();
    6. while (it != s1.rend())
    7. {
    8. cout << *it << " ";
    9. it++;
    10. }
    11. return 0;
    12. }

    这样就更 OK 了;

    还有一个冷门不常用的,就是 crengin 代替 rengin ,crend 代替 crend ;

    1. int main()
    2. {
    3. string s1("hello world");
    4. //string::reverse_iterator it = s1.crbegin();
    5. auto it = s1.crbegin();
    6. while (it != s1.crend())
    7. {
    8. cout << *it << " ";
    9. it++;
    10. }
    11. return 0;
    12. }

    2,reserve & capacity

    reserve:更改容量,需要多少空间,提前开好即可

    capacity:返回空间总大小

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. //初始容量大小
    6. cout << s1.capacity() << endl;
    7. cout << s2.capacity() << endl;
    8. cout << endl;
    9. s1.reserve(20);
    10. s2.reserve(30);
    11. //当 n>容量大小
    12. cout << s1.capacity() << endl;
    13. cout << s2.capacity() << endl;
    14. cout << endl;
    15. s1.reserve(5);
    16. s2.reserve(5);
    17. //当 n<容量大小
    18. cout << s1.capacity() << endl;
    19. cout << s2.capacity() << endl;
    20. return 0;
    21. }

    小伙伴们会好奇,为什么扩容的容量不跟我们指定的容量相同;

    那是因为编译器有自己的一套扩容机制;

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. int doll = s1.capacity();
    6. cout << s1.capacity() << endl;
    7. int i = 1000;
    8. while (i--)
    9. {
    10. s1 += ' ';
    11. if (doll != s1.capacity())
    12. {
    13. cout << s1.capacity() << endl;
    14. doll = s1.capacity();
    15. }
    16. }
    17. return 0;
    18. }

    基本上是按 1.5 倍扩增的,除了刚开始的;

    所以空间容量只会在这些值里面,就算是 16 也直接扩容至下一阶段 31,其实是 32,因为还有一个 ' \0 ' ;

    当扩容之后的量大于当前的容量则扩大;

    当扩容之后的量小于当前真实容量则不变,否则缩小;

    string s2("hello world") 里面的 " hello world " 真实容量就是11;

    3,max_size ( )

    算出字符串所能开辟的最大空间

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. cout << s1.max_size() << endl;
    6. cout << s2.max_size() << endl;
    7. return 0;
    8. }

    由上可得 string类所能开辟的最大空间都是一样的;

    但是所能开辟的最大空间并不是真的能开辟,我们来看一段代码;

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. cout << s2.capacity() << endl;
    6. cout << s2.max_size() << endl;
    7. s2.reserve(s2.max_size());
    8. cout << s2.capacity() << endl;
    9. return 0;
    10. }

    上面开最大空间的容量的时候,运行直接崩溃的,根本开不出来; 

    真实的话是开不出来的,这仅供参考,不必当真;

    4,size()& resize

    size():返回字符串有效长度

    resize():将有效字符的个数该成n个,多出的空间用字符 c 填充

    我们直接来看一段代码

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. //打印有效字符长度和容量大小
    6. cout << s1.size() << " " << s1.capacity() << endl;
    7. cout << s2.size() << " " << s2.capacity() << endl;
    8. cout << endl;
    9. s1.resize(20);
    10. s2.resize(30);
    11. //有效字符对容量的影响
    12. cout << s1.size() << " " << s1.capacity() << endl;
    13. cout << s2.size() << " " << s2.capacity() << endl;
    14. cout << endl;
    15. s1.resize(5);
    16. s2.resize(8);
    17. //
    18. cout << s1.size() << " " << s1.capacity() << endl;
    19. cout << s2.size() << " " << s2.capacity() << endl;
    20. cout << endl;
    21. s1.reserve(100);
    22. s2.reserve(200);
    23. //容量对有效字符的影响
    24. cout << s1.size() << " " << s1.capacity() << endl;
    25. cout << s2.size() << " " << s2.capacity() << endl;
    26. return 0;
    27. }

    由上可得,有效字符 size 的长度会影响 capacity 容量,但是 capacity 容量的大小不会影响 有效字符 size ;

    而且 字符串有效字符长度会随着 size 的变化而变化即使是缩小,但是容量不会改变,以后可以用于【删除数据,保留前 n 个】

    1,void resize (size_t,char c)

    扩增加尾插

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. s1.resize(10, 'y');
    6. s2.resize(20,'x');
    7. cout << s2 << endl;
    8. cout<< s1 << endl;
    9. return 0;
    10. }

    这个我们以后可以用作给字符串赋值和初始化;

    5,push_back & append

    push_back :在字符串后尾插字符c

    append:在字符串后追加一个字符串

    直接上代码:

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. s1.push_back('x');
    6. s2.push_back('y');
    7. cout << s1 << endl;
    8. cout << s2 << endl;
    9. cout << endl;
    10. s2.append("hello world");
    11. s2.append("hello wprld");
    12. cout << s1 << endl;
    13. cout << s2 << endl;
    14. return 0;
    15. }

    1,追加字符串范围

    string& append(inputiterator first,inputiterator last);

    1. int main()
    2. {
    3. string s1("abcdefg");
    4. string s2("hello world");
    5. s1.append(s2.begin(), s2.end());
    6. cout << s1 << endl;
    7. cout << endl;
    8. string s3("abcdefg");
    9. s2.append(++s3.begin(), --s3.end());
    10. cout << s2 << endl;
    11. return 0;
    12. }

    直接范围也是可以的,还可以 ++,--;

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. s1.append(s2,2,7);
    6. s2.append(10, 'x');
    7. cout << s1 << endl;
    8. cout << s2 << endl;
    9. return 0;
    10. }

     指定也是可以的,用法有很多更 string类的用法类似,大家可以去查查文档的各种用法;

    2,直接追加

    1. int main()
    2. {
    3. string s1;
    4. string s2("hello world");
    5. s1 += 'x';
    6. s2 += " abcdefg";
    7. cout << s1 << endl;
    8. cout << s2 << endl;
    9. return 0;
    10. }

    直接 追加也可以,更简便;

  • 相关阅读:
    智能家居涉及到的12个物联网传感器!
    list(链表)
    网络安全(黑客技术)——自学思路
    【Redis】Redis持久化策略
    【C++】拷贝构造函数调用时机 ① ( 使用一个对象初始化另外一个对象 | 将一个对象赋值给另外一个对象 )
    Mysql 流程控制
    又新增4地PMP考试延期!速看!
    git操作将本地的代码推送到远程仓库
    linux安装maven
    华为计算开源总经理堵俊平:AI领域开源新趋势与思考
  • 原文地址:https://blog.csdn.net/m0_71676870/article/details/134497006