鱼C论坛

 找回密码
 立即注册
查看: 1582|回复: 1

[技术交流] C++旅程第11站——string

[复制链接]
发表于 2020-6-10 21:46:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
本帖最后由 这是她 于 2020-6-10 21:46 编辑

We are most dive when we are in love.                             ——John Updike
                                                                                              STL(标准模板库)
STL六大组件:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器

                                                         String
String 本质上是一个类,类内部封装了char *,管理这个字符串,是一个char*型的容器
char*是一个指针

特点:
string 类内部封装了很多成员方法
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. void test1()
  5. {
  6.         //string();       创建一个空的字符串
  7.         //string(const char* s);      使用字符串s初始化
  8.         //string(const string& str);     使用一个string对象初始化另一个string对象
  9.         //string(int n,char c);         使用n个字符c初始化
  10.        
  11.         string s1;
  12.        
  13.         const char * str = "花甲粉";
  14.         string s2(str);
  15.         cout << "s2 = " << s2 << endl;
  16.        
  17.         string s3(s2);
  18.         cout << "s3 = " << s3 << endl;
  19.        
  20.         string s4(10,'h');
  21.         cout << "s4 = " << s4 << endl;
  22. }

  23. void test2()
  24. {
  25.         //string& opertor=(const char* s);          char*类型字符串赋值给当前的字符串
  26.         //string& opertor=(const string &s);        把字符串s赋给当前的字符串
  27.         //string& operator=(char c);                字符赋值给当前的字符串
  28.         //string& assgin(const char* s);            把字符串s赋给当前的字符串
  29.         //string& assgin(const char* s,int n);      把字符串s的前n个字符赋给当前的字符串
  30.         //string& assgin(const string &s);          把字符串s赋给当前字符串
  31.         //string& assgin(int n,char c);             用n个字符c赋给当前字符串
  32.        
  33.         string str1;
  34.         str1 = "bang";
  35.         cout << "str1 = " << str1 << endl;
  36.        
  37.         string str2;
  38.         str2 = str1;
  39.         cout << "str2 = " << str2 << endl;
  40.        
  41.         string str3;
  42.         str3 = 'h';
  43.         cout << "str3 = " << str3 << endl;
  44.        
  45.         string str4;
  46.         str4.assign("i love you!");
  47.         cout << "str4 = " << str4 << endl;
  48.        
  49.         string str5;
  50.         str5.assign("hello world!",5);
  51.         cout << "str5 = " << str5 << endl;
  52.        
  53.         string str6;
  54.         str6.assign(str5);
  55.         cout << "str6 = " << str6 << endl;
  56.        
  57.         string str7;
  58.         str7.assign(10,'i');
  59.         cout << "str7 = " << str7 << endl;
  60. }

  61. void test3()
  62. {
  63.         //string& operator+=(const char* str);              重载+=操作符
  64.         //string& operator+=(const char c);                 重载+=操作符
  65.         //string& operator+=(const string& str);           重载+=操作符
  66.         //string& append(const char *s);                   把字符串s连接到当前的字符串结尾
  67.         //string& append(const char *s,int n);             把字符串s的前n个字符串连接到当前字符串结尾         
  68.         //string& append(const string &s);                 operator+=(const string& str);
  69.         //string& append(const string &s,int pos,int n);   字符串s中从pos开始的n个字符连接到字符串结尾
  70.        
  71.         string str1;
  72.         str1 = "我";
  73.         str1 += "想去草原";
  74.         cout << "str1 = " << str1 << endl;
  75.        
  76.         str1 += '!';
  77.         cout << "str1 = " << str1 << endl;
  78.        
  79.         string str2 = "吃烤全羊";
  80.         str1 += str2;
  81.         cout << "str1 = " << str1 << endl;
  82.        
  83.         string str3 = "i";
  84.         str3.append(" love ");
  85.         cout << "str3 = " << str3 << endl;
  86.        
  87.         str3.append("fishc.too",4);
  88.         cout << "str3 = " << str3 << endl;
  89.        
  90.         str3.append(str2);
  91.         cout << "str3 = " << str3 << endl;
  92.        
  93.         string str4 = "hhhhh";
  94.         str3.append(str4,0,5);
  95.         cout << "str3 = " << str3 << endl;
  96.          
  97. }

  98. void test4()
  99. {
  100.         //int find(const string& str,int pos = 0)const;
  101.         string str1 = "Wishing you chear skies and wide eyes.";
  102.        
  103.         int pos = str1.find("you");
  104.        
  105.         if (pos == -1)
  106.         {
  107.                 cout << "未找到字符串!!!" << endl;
  108.         }
  109.         else
  110.         {
  111.                 cout << "找到字符串,pos = " << pos << endl;
  112.         }
  113.        
  114.         //rfind 和 find区别
  115.         //rfind 从右向左查找  find 从左向右查找
  116.         pos = str1.rfind("wide");
  117.        
  118.         cout << "pos = " << pos << endl;
  119.        
  120.         //从 1号位置起,7个字符替换为1111
  121.         str1.replace(1,7,"1111");
  122.        
  123.         cout << "str1 = " << str1 << endl;
  124. }

  125. void test5()
  126. {
  127.         string str1 = "fishc";
  128.         string str2 = "fisha";
  129.        
  130.         //int compare(const string &s) const;与字符串s比较
  131.         //int compare(const string *s) const;与字符串s比较
  132.         if (str1.compare(str2) == 0)
  133.         {
  134.                 cout << "str1 = str2" << endl;
  135.         }
  136.         else if (str1.compare(str2) > 0)
  137.         {
  138.                 cout << "str1 > str2" << endl;
  139.         }
  140.         else
  141.         {
  142.                 cout << "str1 < str2" << endl;
  143.         }
  144. }

  145. void test6()
  146. {
  147.         string str1 = "hello";
  148.        
  149.         //char& operator[](int n);  通过[]方式取字符
  150.         //通过[]访问单个字符
  151.         for (int i = 0;i < str1.size();i++)
  152.         {
  153.                 cout << str1[i] << " ";
  154.         }
  155.         cout << endl;
  156.        
  157.         //char& at(int n);  通过at方式获取字符
  158.         //通过at方式访问单个字符
  159.         for (int i = 0; i < str1.size(); i++)
  160.         {
  161.                 cout << str1.at(i) << " ";
  162.         }
  163.         cout << endl;
  164.        
  165.         //修改单个字符
  166.         str1[0] = 'x';
  167.        
  168.         cout << "str1 = " << str1 << endl;
  169.        
  170.         str1.at(1) = 'y';
  171.        
  172.         cout << "str1 = " << str1 << endl;
  173.        
  174. }

  175. void test7()
  176. {
  177.         string str1 = "hello";
  178.        
  179.         //插入
  180.         //string& insert(int pos,const char* s); 插入字符串
  181.         //string& insert(int pos,const string& str); 插入字符串
  182.         str1.insert(1,"6666");
  183.        
  184.         cout << "str1 = " << str1 << endl;
  185.        
  186.         //删除
  187.         //string& insert(int pos,int n,char c); 在指定位置插入n个字符c
  188.         //string& insert(int pos,int n = npos);  删除从pos开始的n个字符
  189.         str1.erase(1,3);
  190.        
  191.         cout << "str1 = " << str1 << endl;
  192. }

  193. void test8()
  194. {
  195.         string str1 = "fishc";
  196.        
  197.         //从字符串中获取想要的子串
  198.         //string substr(int pos = 0,int n = npos)const; 返回由pos开始的n个字符组成的字符串
  199.         string sub1 = str1.substr(1,3);
  200.        
  201.         cout << "sub1 =  " << sub1 << endl;
  202.        
  203. }

  204. int main()
  205. {
  206.         test1();
  207.        
  208.         test2();//string 赋值
  209.                
  210.         test3();//string 拼接
  211.        
  212.         test4();//string 查找和替换

  213.         test5();//string 比较

  214.         test6();//string 存取

  215.         test7();//string 插入和删除

  216.         test8();//string 求子串       
  217.        
  218.         return 0;
  219. }
复制代码

                                                 是谁是谁

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
小甲鱼的铁粉 + 1 + 1 无条件支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-10 23:30:31 | 显示全部楼层
这是她
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-15 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表