C++初阶学习第六弹------标准库中的string类

[复制链接]
发表于 2026-1-21 08:18:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×


目次


一.标准库中的string类
二.string的常用接口函数
2.1string类对象的构造
 2.2 string的容量操纵
 2.3 string类的访问与遍历
 2.4 string类对象的修改
2.5 string类常用的非成员函数
 三、总结


一.标准库中的string类


可以简单明确成把string类明确为变长的字符数组,我们可以对它举行增删查改等一系列操纵,同时有一些列封装的接口函数提供给我们可以让我们直接利用。
二.string的常用接口函数

2.1string类对象的构造


  1.         string s2("hello bit"); // 用C格式字符串构造string类对象s2
  2.         cout << s2 << endl;
  3.         cout << endl;
  4.         string s3(s2); // 拷贝构造s3string s3(s2); // 拷贝构造s3
  5.         cout << s3 << endl;
复制代码
 我们普便利用以上的两种方法来构造string。


 2.2 string的容量操纵


  1. int main()
  2. {
  3.         string s1("abcdef");
  4.         cout << "s1:" << s1 << endl;
  5.         cout << "size:" << s1.size() << endl;        //有效字符的个数
  6.         cout << "length:" << s1.length() << endl;    //有效字符的个数
  7.         //上面这两个功能上差别不大,一般我们用size()用的多一点
  8.         cout << "capacity:" << s1.capacity() << endl;
  9.         //开辟的空间大小(当空间不够时会自动扩容,扩容空间为原空间的1.5倍(与环境有关))
  10.         cout << "empty:" << s1.empty() << endl;     //检查字符串是否为空,0表示非空,1表示空
  11.         s1.clear();                                 //清空字符串
  12.         cout << "s1:" << s1 << endl;
  13.         s1.reserve(50);                            //开辟指定大小空间(一般会多一点)
  14.         cout << "capacity:" << s1.capacity() << endl;
  15.         s1.resize(5, 'a');
  16.         cout << "size:" << s1.size() << endl;
  17.         cout << "s1:" << s1 << endl;
  18.         return 0;
  19. }
复制代码
 

 2.3 string类的访问与遍历



  1. int main()
  2. {
  3.         string s1("abcdef");
  4.         //访问方法:下标访问法
  5.         cout << s1[1] << endl;
  6.         cout << s1[2] << endl;
  7.         s1[0] = 'h';
  8.        
  9.        
  10.         cout << "下标遍历法:";
  11.         for (int i = 0; i < s1.size(); i++)
  12.         {
  13.                 cout << s1[i] << " ";
  14.         }
  15.         cout << endl;
  16.        
  17.         cout << "迭代器法(正向):";
  18.         string::iterator it = s1.begin();
  19.         for (; it != s1.end(); it++)
  20.         {
  21.                 cout << *it << " ";
  22.         }
  23.         cout << endl;
  24.        
  25.         cout << "迭代器(反向):";
  26.         string::reverse_iterator rit = s1.rbegin();
  27.         while (rit != s1.rend())
  28.         {
  29.                 cout << *rit << " ";
  30.                 rit++;
  31.         }
  32.         cout << endl;
  33.        
  34.         cout << "范围for法:";
  35.         for (auto e : s1)
  36.         {
  37.                 cout << e << " ";
  38.         }
  39.         cout << endl;
  40.         return 0;
  41. }
复制代码
 

 2.4 string类对象的修改


  1. int main()
  2. {
  3.         string s1("sssssssss");
  4.         cout << s1 << endl;
  5.         //push_back  在末尾加入字符
  6.         cout << "push_back后:";
  7.         s1.push_back('w');
  8.         cout << s1 << endl;
  9.         //append     在末端加入字符串
  10.         cout << "append后:" ;
  11.         s1.append(" www");
  12.         cout << s1 << endl;
  13.         //operator+= 在末端随意添加
  14.         cout << "+=后:";
  15.         s1 += " yy";
  16.         cout << s1 << endl;
  17.         //c_str    返回C格式字符串
  18.         cout << "c_str:";
  19.         const char* m = s1.c_str();
  20.         cout << m << endl;
  21.         //find  从pos位置开始查找字符并返回其位置
  22.         cout << "find:";
  23.         int npos1 = s1.find('y');
  24.         cout << npos1 << endl;
  25.         //rfind  从pos位置开始往前查找字符并返回其位置
  26.         cout << "rfind:";
  27.         int npos2 = s1.rfind('y');
  28.         cout << npos2 << endl;
  29.         //substr  从pos位置开始截取n个字符并返回
  30.         cout << "substr后:";
  31.         string tmp = s1.substr(npos1, npos2 - npos1);
  32.         cout << tmp << endl;
  33.         return 0;
  34. }
复制代码
 

2.5 string类常用的非成员函数



  1. int main()
  2. {
  3.         string s1("hello ");
  4.         string s2("bit");
  5.         //operator+    涉及深层拷贝,不建议多用
  6.         cout << "operator+后:";
  7.         cout << operator+(s1, s2) << endl;
  8.         //operator>>   输入运算符重载
  9.         cout << "operator>>:";
  10.         string s3;
  11.         operator>>(cin,s3);
  12.         cout << s3 << endl;
  13.         //operator<<    输出运算符重载
  14.         cout << "operator<<:";
  15.         operator<<(cout, s1) << endl;
  16.         return 0;
  17. }
复制代码
 

 三、总结

以上就是常见的string封装的接口函数,下期将解说关于怎样实现这些函数。
请大佬们一键三连~

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表