鱼C论坛

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

[学习笔记] 044-C++之STL-->string

[复制链接]
发表于 2018-9-16 21:31:34 | 显示全部楼层 |阅读模式

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

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

x
1、string概念
1. string是STL的字符串类型,通常用来表示字符串。传统的字符串通常是用字符指针char* 或 字符数组模拟字符串。
2. string是一个类,char*是指向字符的指针,  string封装了char*,并管理这个char*,是char*的一个容器。
3. string不用考虑内存释放和越界,string自动管理这块内存。
4. string提供了一系列的字符串操作函数。
2、string的构造函数
1.  默认构造
        s1 = string();     构造一个空串s1;
2.  拷贝构造函数
        string(const string &str);     构造一个与str一样的string;
3. 带参的构造函数
         string(const char *s);      用字符串s初始化string;
         string(int n, char c);        用n个字符c初始化。
  1. string s1 = "abcdef";
  2. string s2("123456");
  3. string s3 = s2;    // 通过拷贝构造函数初始化
  4. string s4(5, 'a');
  5. cout << s1 << endl << s2 << endl << s3 << endl << s4 << endl;
复制代码

3、string的存取和遍历
  1. string s1;
  2. for (int i = 0; i < s1.length(); i++)
  3.         {
  4.         cin >> s1[i] ;    // []不会抛出异常, 直接中断, s[i]可以当左值
  5.         cout << s1.at(i);  // at(i)发生异常时,可以抛出异常。
  6.         }
  7.         cout << endl;
  8.         // 2、迭代器
  9.         for (string::iterator it = s1.begin(); it != s1.end(); it++)
  10.         {
  11.                 cout << *it << " ";
  12.         }
  13.         cout << endl;
复制代码

4、字符指针和string的相互转换
  1. string s5 = "ABC123";     // char* ==>  string
  2. char * p = (char *)s5.c_str();   // string ==> char*
  3. cout << p << endl;
  4. // string字符串拷贝到字符数组
  5. char buf[128] = { 0 };
  6. //s1.copy(buf1, 3, 0);  // 从第0个字符开始复制,复制3个字符到buf1下,VS2015下不安全
  7. strcpy_s(buf, s5.c_str());
  8. s1._Copy_s(buf, 128, 3, 0);
  9. cout << buf << endl;
复制代码

5、字符串的连接
  1. string s6;
  2. s6 = s1 + s2;  // 重载了+运算符
  3. s6.append(s5);
  4. cout << s6 << endl;
复制代码

6、字符串查找和替换
  1. int index = s7.find("abc", 0);  //从位置0开始查找
  2. cout << "index:" << index << endl;
  3. // 查找ab每次出现位置的下标
  4. while (index != string::npos)
  5. {
  6.         cout << "index:" << index << " ";
  7.         index++;
  8.         index = s7.find("abc", index); //find()函数未找到返回-1,即string::npos
  9. }
  10. cout << endl;
  11. // 把abc替换为ABC
  12. index = s7.find("abc", 0);
  13. while (index != string::npos)
  14. {
  15.         s7.replace(index, 3, "ABC");  // 从index位置起删除3个字符,然后在插入“ABC”
  16.         index++;
  17.         index = s7.find("abc", index); //find()函数未找到返回-1,即string::npos
  18. }
  19. cout << s7 << endl;
复制代码

7、字符串区间删除和插入
  1. string s8 = "hello1 hello2 hello3";
  2. string::iterator it = find(s8.begin(), s8.end(), 'l');  //#include "algorithm"
  3. if (it != s8.end())
  4. {
  5.         s8.erase(it);
  6. }
  7. cout << "s8:" << s8 << endl;

  8. s8.erase(s8.begin(), s8.end()-6); //部分和全部删除
  9. cout << s8 << endl;
  10.        
  11. s8.insert(0, "AAA");  // 在零位置插入“AAA”
  12. s8.insert(s8.length(), "BBB");  // 尾部插入"BBB"
  13. cout << s8 << endl;
复制代码

8、大小写转换
包含于#include "algorithm"
  1. string s9 = "AAAbbb";
  2. transform(s9.begin(), s9.end(), s9.begin(), toupper);
  3. cout << s9 << endl;
  4. transform(s9.begin(), s9.end(), s9.begin(), tolower);
  5. cout << s9 << endl;
复制代码

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 12:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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