鱼C论坛

 找回密码
 立即注册
查看: 2423|回复: 0

[技术交流] 万行代码计划 Day05

[复制链接]
发表于 2021-7-30 23:44:07 | 显示全部楼层 |阅读模式

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

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

x
万行代码计划
Day05,50行
进度300/10000
有兴趣的伙伴可以一起来,互相监督

  1. /*
  2. 0730
  3. list 双向串列
  4. 不支持随之访问
  5. 插入删除元素很快,不会造成迭代器失效
  6. */
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #include<iostream>
  9. using namespace std;
  10. #include <string>
  11. #include <stdexcept>

  12. /*
  13. string和c-style字符串转换
  14. */

  15. void func(string s) {
  16.     cout << s << endl;
  17. }

  18. void func2(const char *s){
  19.     cout << s << endl;   
  20. }

  21. void test07(){
  22.     string s = "abc";

  23.     const char *p = s.c_str();
  24.     func(p);//const char * 隐式类型转换为 string

  25.     string s2(p);
  26.     //func2(s2);//不存在从 "std::string" 到 "const char *" 的适当转换函数
  27.     func(s2);
  28. }

  29. void test08()
  30. {
  31.         string s = "abcdefg";
  32.         char& a = s[2];
  33.         char& b = s[3];

  34.         a = '1';//会改变s的值
  35.         b = '2';

  36.         cout << s << endl;
  37.         cout << (int*)s.c_str() << endl;

  38.         s = "pppppppppppppp";

  39.         //a = '1';
  40.         //b = '2';

  41.         cout << s << endl;
  42.         cout << (int*)s.c_str() << endl;//地址指向“ppppppppp”

  43. }

  44. /*
  45. string插入和删除操作
  46. string& insert(int pos, const char* s); //插入字符串
  47. string& insert(int pos, const string& str); //插入字符串
  48. string& insert(int pos, int n, char c);//在指定位置插入n个字符c
  49. string& erase(int pos, int n = npos);//删除从Pos开始的n个字符

  50. */

  51. void test06(){
  52.     string s1("hello");
  53.     s1.insert(1,"111");
  54.     cout << s1 << endl;
  55.     s1.insert(1,s1);
  56.     cout << s1 << endl;
  57.     //删除
  58.     s1.erase(1,3);
  59.     cout << s1 << endl;
  60. }

  61. /*
  62. string子串
  63. string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
  64. */
  65. //需求  查找一个右键的 用户名
  66. void test05()
  67. {
  68.     string email = "zhangsan@qq.com";
  69.    
  70.     auto pos = email.find('@');
  71.     cout << "pos @ = " << pos << endl;

  72.     auto usrName = email.substr(0,pos);
  73.         cout << "用户名为:" << usrName << endl;
  74. }

  75. /*
  76. string存取字符操作
  77. char& operator[](int n);//通过[]方式取字符
  78. char& at(int n);//通过at方法获取字符

  79. */
  80. void test02()
  81. {
  82.         string s = "hello world";

  83.         for (int i = 0; i < s.size();i++)
  84.         {
  85.                 //cout << s[i] << endl;
  86.                 cout << s.at(i) << endl;
  87.         }
  88.         //[] 和at区别?[]访问越界  直接挂掉 at会抛出异常

  89.         try
  90.         {
  91.                 cout << s[100] << endl;
  92.         cout << "s[100]" << endl;
  93.                 cout << s.at(100) << endl;
  94.         }
  95.         catch (out_of_range & e)
  96.         {
  97.                 cout << e.what() << endl;
  98.         cout << "s.at(100) error" << endl;
  99.         }
  100.         catch (...)
  101.         {
  102.                 cout << "越界异常" << endl;
  103.         }
  104. }

  105. int main()
  106. {
  107.     test07();
  108.     test08();
  109.     test06();
  110.     test05();
  111.     test02();
  112. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 05:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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