0


【C++STL】String类的常用函数用法总结

String类

一:String类的初始化方式

  1. string str:生成空字符串
  2. string s(str):生成字符串为str的复制品 (拷贝构造)(带参构造)
  3. string s(str, strbegin,strlen):将字符串str中从下标strbegin开始、长度为strlen的部分作为字符串初值
  4. string s(cstr, char_len):以string类型cstr的前char_len个字符串作为字符串s的初值
  5. string s(num ,c):生成numc字符的字符串
  6. string s(str, strindex):将字符串str中从下标strindex开始到字符串结束的位置作为字符串初值

二:String类的访问方式

  1. 一:
  2. string s1 = ("hello world"); 我们可以通过s1[i]的方式来访问
  3. s1[i] 的实际写法 s1.operator(i); 因为实际上是对[]进行了运算符重载
  4. 二:
  5. 1.通过迭代器 iterator
  6. string::iterator it1 = s1.begin();
  7. while (it1 != s1.end()) //这里的end都是字符串最后一个位置的下一个位置
  8. {
  9. cout << *it1 << " ";
  10. it1++;
  11. }
  12. 2.这里还有一种叫做反向迭代器(reverse_interator, rbegin(), rend())
  13. string s3("hello");
  14. string::reverse_iterator it2 = s3.rbegin();
  15. while (it2 != s3.rend())
  16. {
  17. cout << *it2 << " ";
  18. ++it2;
  19. }
  20. 3.auto 范围for //底层就是迭代器
  21. for (auto& e : lt1)
  22. {
  23. cout << e << " ";
  24. }
  25. s1.operator(i) iterator都有 const的情况 const _interator
  26. 例如:const string s2(s1);
  27. 注意:const _interatorinterator指向的数据不能修改 如果是 const interator的话那么就是interator本身不能修改

三:String的大小和容量

  1. 1. size()和length():返回string对象的字符个数,他们执行效果相同。
  2. 2. max_size():返回string对象最多包含的字符数,超出会抛出length_error异常
  3. 3. capacity():重新分配内存之前,string对象能包含的最大字符数
  4. 这里对capacity底层作出一些解释:如果字符串占用的空间不大,则占用的是栈空间中的buffer,默认为15个字节,如果超过,就会在堆空间上开辟空间

四:string的插入:push_back() 和 insert()

  1. string s1;
  2. // 尾插一个字符
  3. s1.push_back('a');
  4. s1.push_back('b');
  5. s1.push_back('c');
  6. cout << "s1:" << s1 << endl; // s1:abc
  7. // insert(pos,char) : 在制定的位置pos前插入字符char
  8. //insert方法 慎用效率不高 类似顺序表
  9. string ss("helloworld");
  10. ss.insert(0, "xxxx"); // 时间复杂度 O(n)

五:string拼接字符串:append() & + 操作符

  1. // 方法一:append()
  2. string s1("abc");
  3. s1.append("def");
  4. cout << "s1:" << s1 << endl; // s1:abcdef
  5. // 方法二:+ 操作符
  6. string s2 = "abc";
  7. /*s2 += "def";*/
  8. string s3 = "def";
  9. s2 += s3.c_str();
  10. cout << "s2:" << s2 << endl; // s2:abcdef

六: string的删除:erase()

  1. //删除erase 效率也很低和insert差不多
  2. 1. iterator erase(iterator p);//删除字符串中p所指的字符
  3. 2. iterator erase(iterator first, iterator last);//删除字符串中迭代器
  4. 区间[first,last)上所有字符 左闭右开
  5. 3. string& erase(size_t pos = 0, size_t len = npos);//删除字符串中从索引
  6. 位置pos开始的len个字符
  7. 4. void clear();//删除字符串中所有字符

七: string的字符替换

  1. 1. string& replace(size_t pos, size_t n, const char *s);//将当前字符串
  2. pos索引开始的n个字符,替换成字符串s
  3. 2. string& replace(size_t pos, size_t n, size_t n1, char c); //将当前字符串从pos索引开始的n个字符,替换成n1个字符c
  4. 3. string& replace(iterator i1, iterator i2, const char* s);//将当前字符串[i1,i2)区间中的字符串替换为字符串s

八:c_str()使用场景

  1. string file("stl.cpp");
  2. // c_str返回string的指针 相同与数组首元素地址
  3. FILE* fout = fopen(file.c_str(), "r"); //只能传const char* c和c++的不同之处 这个时候c_str这个时候就派上用场了
  4. char ch = fgetc(fout);
  5. while (ch != EOF)
  6. {
  7. cout << ch;
  8. ch = fgetc(fout);
  9. }

九: string的大小写转换:tolower() 和toupper() 函数 或者 STL中transform算法

方法一:

  1. #include<iostream>#include<string>usingnamespace std;intmain(){
  2. string s ="ABCDEFG";for(int i =0; i < s.size(); i++){
  3. s[i]=tolower(s[i]);}
  4. cout << s << endl;return0;}

方法二:

  1. #include<iostream>#include<algorithm>#include<string>usingnamespace std;intmain(){
  2. string s ="ABCDEFG";
  3. string result;transform(s.begin(),s.end(),s.begin(),::tolower);
  4. cout << s << endl;return0;}

十: string的查找:find() 和 substr()方法

  1. 1. size_t find (constchar* s, size_t pos = 0) const;
  2. //在当前字符串的pos索引位置开始,查找子串s,返回找到的位置索引,
  3. -1表示查找不到子串
  4. 2. size_t find (charc, size_t pos = 0) const;
  5. //在当前字符串的pos索引位置开始,查找字符c,返回找到的位置索引,
  6. -1表示查找不到字符
  7. 3. size_t rfind (constchar* s, size_t pos = npos) const;
  8. //在当前字符串的pos索引位置开始,反向查找子串s,返回找到的位置索引,
  9. -1表示查找不到子串
  10. 4. size_t rfind (charc, size_t pos = npos) const;
  11. //在当前字符串的pos索引位置开始,反向查找字符c,返回找到的位置索引,-1表示查找不到字符
  12. string file("stl.cpp");
  13. size_t pos = file.rfind("."); //从前往后倒着找. 找到返回对应的下标
  14. //string suffix = file.substr(pos, file.size() - pos);
  15. string suffix = file.substr(pos); //有多少取多少

十一:reserve() 和 resize()

reserve为容器预留足够的空间,避免不必要的重复分配,分配空间大于等于函数的参数,影响capacity。

resize调整容器中有效数据区域的尺寸,如果尺寸变小,原来数据多余的截掉。若尺寸变大,不够的数据用该函数第二个参数填充,影响size。

  1. // reserve 不一定会缩容 但会扩容 意义是直接reserve就不用一直倍增扩容了 提前开空间
  2. // reserve 不会初始化 但是resize会
  3. string ss("helloworld");
  4. cout << ss.capacity() << endl;
  5. ss.reserve(100);
  6. cout << ss.capacity() << endl;
  7. // 注意不会改变size 所以不能越界访问
  8. // 这个时候resize就出现了 就可以用下标进行访问了
  9. string str;
  10. str.resize(5, '0');
  11. str[3] = '1';
  12. cout << str[2] << endl;
  13. cout << str.size() << endl;
  14. //自己去验证一下才是最有效的
  15. ss.clear();
  16. ss = "hello world";
  17. ss.resize(15, 'x'); //如果有剩余位置 就在剩余位置填充 不会覆盖原有位置
  18. cout << ss << endl;
  19. cout << ss.size() << " " << s1.capacity() << endl;

十二:to_string 与 stoi 函数

介绍

stoi(),to_string 这两个函数都是对字符串处理的函数,前者是将字符串转化为十进制 int 类型,最后一个是将十进制类型 int、double 等转化为string。

  1. 头文件都是:#include <cstring>里的
  2. \colorbox{pink}{头文件都是:\#include <cstring>里的}
  3. 头文件都是:#include <cstring>里的​

✨to_string作用

将整数转换为字符串
功能:将数字常量(int,double,long等)转换为字符串(string),返回转换好的字符串

在这里插入图片描述

测试代码

  1. #include<iostream>#include<cstring>usingnamespace std;intmain(){int num =123456789;
  2. string s =to_string(num);// "123456789"
  3. cout << s << endl;return0;}

✨stoi作用

将 n 进制的字符串转化为十进制
stoi(字符串,起始位置,n进制(默认10进制)),将 n 进制的字符串转化为十进制
在这里插入图片描述

测试代码

  1. #include<iostream>#include<cstring>usingnamespace std;intmain(){
  2. string str ="100";int x =stoi(str,0,2);//将二进制"100"转化为十进制x
  3. cout << x << endl;return0;}

总结:

以上就是string类的常用函数,内容并不全面,详细内容可在官方文档中进行查阅,希望对大家的学习有所帮助,一起进步!


本文转载自: https://blog.csdn.net/weixin_50776420/article/details/138368623
版权归原作者 Zfox_ 所有, 如有侵权,请联系我们删除。

“【C++STL】String类的常用函数用法总结”的评论:

还没有评论