前言:这个学了感觉没多大用,自己只需要了解就好,忘记了可以参考以下网站的示例

参考网站:https://github.com/AnkerLeng/Cpp-0-1-Resource/blob/master/第5阶段-C%2B%2B提高编程资料/提高编程能力资料/讲义/C%2B%2B提高编程.md

string容器:

string基本概念:

本质:

string是C++风格的字符串,而string本质上是一个类

string和char * 区别:

char * 是一个指针

string 是一个类,类内部封装了char*,管理这个字符串,是一个char * 型的容器。

特点:

string 类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete 替换replace,插入insert

string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责


string构造函数:

构造函数原型:有三种

1、string(); //创建一个空的字符串 例如: string str; string(const char* s); //使用字符串s初始化

2、string(const string& str); //使用一个string对象初始化另一个string对象

3、string(int n, char c); //使用n个字符c初始化

示例代码:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. /*
  5. 构造函数原型:
  6. string(); //创建一个空的字符串 例如: string str; string(const char* s); //使用字符串s初始化
  7. string(const string& str); //使用一个string对象初始化另一个string对象
  8. string(int n, char c); //使用n个字符c初始化
  9. */
  10. int main() {
  11. string name1; // 第一种:创建空字符串,调用无构造函数
  12. cout << name1 << endl;
  13. string name2("这是name2"); //第二种方式,初始化另一个string对象
  14. string name3(name2);
  15. cout << name3 << endl;
  16. string name4(3, '6'); //第三种方式:使用n个字符c初始化
  17. cout << name4 << endl;
  18. system("pause");
  19. return 0;
  20. }

string赋值操作:

功能描述:给string字符串进行赋值

赋值的函数原型:

1、string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串

2、string& operator=(const string &s); //把字符串s赋给当前的字符串

3、string& operator=(char c); //字符赋值给当前的字符串

4、string& assign(const char *s); //把字符串s赋给当前的字符串

5、string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串

6、string& assign(const string &s); //把字符串s赋给当前字符串

7、string& assign(int n, char c); //用n个字符c赋给当前字符串

示例代码:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. /*
  5. string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
  6. string& operator=(const string &s); //把字符串s赋给当前的字符串
  7. string& operator=(char c); //字符赋值给当前的字符串
  8. string& assign(const char *s); //把字符串s赋给当前的字符串
  9. string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
  10. string& assign(const string &s); //把字符串s赋给当前字符串
  11. string& assign(int n, char c); //用n个字符c赋给当前字符串*/
  12. int main() {
  13. string a = "a"; //第一种
  14. cout << a << endl;
  15. string b = "b"; //第二种
  16. a = b;
  17. cout << a << endl;
  18. b = 'a'; //第三种
  19. cout << b << endl;
  20. a.assign("asd"); //第四种
  21. cout << a << endl;
  22. a.assign("dnf",1); //第五种
  23. cout << a << endl;
  24. a.assign(b); //第六种
  25. cout << a << endl;
  26. a.assign(5, 'a'); //第七种
  27. cout << a << endl;
  28. system("pause");
  29. return 0;
  30. }

实现在字符串末尾拼接字符串:

好枯燥,个人感觉有用的就是能够自定义位置进行拼接的append重载函数

示例代码:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. /*
  5. string& operator+=(const char* str); //重载+=操作符
  6. string& operator+=(const char c); //重载+=操作符
  7. string& operator+=(const string& str); //重载+=操作符
  8. string& append(const char *s); //把字符串s连接到当前字符串结尾
  9. string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
  10. string& append(const string &s); //同operator+=(const string& str)
  11. string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾*/
  12. //字符串拼接
  13. void test01()
  14. {
  15. string str1 = "我";
  16. str1 += "爱玩游戏";
  17. cout << "str1 = " << str1 << endl;
  18. str1 += ':';
  19. cout << "str1 = " << str1 << endl;
  20. string str2 = "LOL DNF";
  21. str1 += str2;
  22. cout << "str1 = " << str1 << endl;
  23. string str3 = "I";
  24. str3.append(" love ");
  25. str3.append("game abcde", 4); //取前四个进行拼接
  26. //str3.append(str2);
  27. str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
  28. cout << "str3 = " << str3 << endl;
  29. }
  30. int main() {
  31. test01();
  32. system("pause");
  33. return 0;
  34. }

string查找和替换:

功能描述:

查找:查找指定字符串是否存在

替换:在指定的位置替换字符串

函数原型有很多,就不一一举例了,挑两个常用的来写

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. /*
  5. int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
  6. int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
  7. int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
  8. int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
  9. int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
  10. int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
  11. int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
  12. int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
  13. string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
  14. string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s*/
  15. int main() {
  16. string name = "thisisname";
  17. //实现查找操作
  18. int pos = name.find("is", 0); //这里注意的两个地方:1、从左往右下标从0开始 2、先找到就哪个 如果没找到返回值为-1
  19. cout << pos << endl;
  20. int poc = name.rfind("is"); // 跟上面一样 唯一不同的是 找最后一个 例如isis 会找后面的is 那么返回值为2
  21. cout << poc << endl;
  22. //替换操作
  23. name.replace(1, 2, "1111"); // 从下标一开始的两个字符替换为1111
  24. cout << name << endl;
  25. system("pause");
  26. return 0;
  27. }

string字符串比较:

示例代码:

  1. //字符串比较
  2. void test01()
  3. {
  4. string s1 = "hello";
  5. string s2 = "aello";
  6. int ret = s1.compare(s2);
  7. if (ret == 0) {
  8. cout << "s1 等于 s2" << endl;
  9. }
  10. else if (ret > 0)
  11. {
  12. cout << "s1 大于 s2" << endl;
  13. }
  14. else
  15. {
  16. cout << "s1 小于 s2" << endl;
  17. }
  18. }
  19. int main() {
  20. test01();
  21. system("pause");
  22. return 0;
  23. }

string字符存取:

string中单个字符存取方式有两种

1、char& operator[](int n); //通过[]方式取字符

2、char& at(int n); //通过at方法获取字符

示例代码:

  1. void test01()
  2. {
  3. string str = "hello world";
  4. for (int i = 0; i < str.size(); i++)
  5. {
  6. cout << str[i] << " ";
  7. }
  8. cout << endl;
  9. for (int i = 0; i < str.size(); i++)
  10. {
  11. cout << str.at(i) << " ";
  12. }
  13. cout << endl;
  14. //字符修改
  15. str[0] = 'x';
  16. str.at(1) = 'x';
  17. cout << str << endl;
  18. }
  19. int main() {
  20. test01();
  21. system("pause");
  22. return 0;
  23. }

string插入和删除:

示例代码:

  1. //字符串插入和删除
  2. void test01()
  3. {
  4. string str = "hello";
  5. str.insert(1, "111");
  6. cout << str << endl;
  7. str.erase(1, 3); //从1号位置开始3个字符
  8. cout << str << endl;
  9. }
  10. int main() {
  11. test01();
  12. system("pause");
  13. return 0;
  14. }

string子串:

示例代码:

  1. //子串
  2. void test01()
  3. {
  4. string str = "abcdefg";
  5. string subStr = str.substr(1, 3);
  6. cout << "subStr = " << subStr << endl;
  7. string email = "hello@sina.com";
  8. int pos = email.find("@");
  9. string username = email.substr(0, pos);
  10. cout << "username: " << username << endl;
  11. }
  12. int main() {
  13. test01();
  14. system("pause");
  15. return 0;
  16. }

学习:SLT_string容器的更多相关文章

  1. Spring.NET依赖注入框架学习--实例化容器常用方法

    Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...

  2. 5.docker学习之容器

    容器创建 我们已经知道,镜像是只读的,而基于镜像创建出来的容器是可读写的,所以,一般我们实际中,会经常使用对应镜像创建容器并且使用这些容器.同样,如果我们想要使用容器,那么我们必须首先需要创建容器.而 ...

  3. c++学习之容器细枝末节(2)

    从昨天到现在,还依然停留在容器的学习上,现在写例程代码顺手多了,看来写代码还是要多多练习才能有感觉. 经过一天的学习,有一下几点知识点让我觉得很有意义: (1)删除容器中的元素的时候,pop_fron ...

  4. c++学习之容器细枝末节(1)

    对照着c++primier 开始学习第九章容器,把课后习题当做练习,虽然是看过书上的讲解,但是做题编程的时候,一些需要注意的地方还是难免有遗漏. 一下是几点印象比较深刻的总结: (1)前几章只学了ve ...

  5. ###STL学习--关联容器

    点击查看Evernote原文. #@author: gr #@date: 2014-08-23 #@email: forgerui@gmail.com STL中的关联容器. ###stl学习 |--迭 ...

  6. 创建ApplicationContext与BeanFactory时的区别-Spring源码学习之容器的基本实现

    传送门 可以加载XML两种方法 使用 BeanFactory 加载 XML BeanFactory bf = new XmlBeanFactory(new ClassPathResource(&quo ...

  7. 侯捷STL学习(十)--容器hashtable探索(unordered set/map)

    layout: post title: 侯捷STL学习(十) date: 2017-07-23 tag: 侯捷STL --- 第二十三节 容器hashtable探索 hashtable冲突(碰撞)处理 ...

  8. spring源码学习之容器的基本实现

    最近想拿出一部分时间来学习一下spring的源码,还特意买了一本书结合来看,当然主要是学习并跟着作者的思路来踏上学习spring的源码的道路,特意在此记录一下,<spring源码深度解析> ...

  9. laravel学习:容器绑定与解析

    1.在服务容器中注册类(bind) $this->app->bind('sender','MailSender');//$this->app成为服务容器.   2.从服务容器生成类( ...

随机推荐

  1. 第八节:Asp.Net Core整合Log4net(官方的、微软的两种)

    一. 整合Log4net 1. 简单说明 对于log4net 官方的程序集而言,从2.0.7开始就支持.Net Core了,这里我们采用的是2.0.8,虽然好久没更新了,但不影响使用.Core版本与普 ...

  2. php redis扩展安装步骤

    因为redis不是php技术自带的技术,因此我们如果要通过php程序来操作redis,需要redis设计者提供对应的操作接口(函数类)我们使用phpredis.tar.gz文件在源码编译生成一个red ...

  3. myeclipse导入项目中文乱码怎么解决教程

    大家在Myeclipse导入项目的时候,应该都遇见过一些乱码的问题,不单单只是Myeclipse有这个问题,那么怎么解决Myeclipse导入项目乱码的问题呢,问题出现的原因是什么呢,下面来看看答案. ...

  4. Java学习:集合双列Map

    数据结构 数据结构: 数据结构_栈:先进后出 入口和出口在同一侧 数据结构_队列:先进先出 入口和出口在集合的两侧 数据结构_数组: 查询快:数组的地址是连续的,我们通过数组的首地址可以找到数组,通过 ...

  5. Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法

    当你右键编辑控件的id或者其他属性时都会卡很久,发现原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stac ...

  6. Linux学习笔记之RAID笔记

    RAID: Redundant Arrays of Inexpensive Disks Independent Berkeley: A case for Redundent Arrays of Ine ...

  7. SQL Server的唯一键和唯一索引会将空值(NULL)也算作重复值

    我们先在SQL Server数据库中,建立一张Students表: CREATE TABLE [dbo].[Students]( ,) NOT NULL, ) NULL, ) NULL, [Age] ...

  8. (转)二步实现 远程连接 阿里云SqlServer 2012 数据库服务器

    前言:在使用 阿里云 上的一些产品时,遇到不少坑. 安装IIS 时,遇到 因买的配置过低,虚拟内存不足,而导致 IIS 总是安装失败: 现在 在上面安装了个 Sql Sever 2012,远程老是 不 ...

  9. vue/react/angular开发的css架构思考

    前端开发现在已经从传统的后端web多页面开发模式转向前端单页SPA开发模式,而vuejs/react/angular则是开发SPA非常优秀的前端框架.组件化开发由react最早提出,vuejs后发优势 ...

  10. iOS - 适配 iOS 13 之工兵连扫雷

    iOS 13 支持适配的机型 目前最新 iPhone 11.iPhone 11 Pro和iPhone 11 Pro Max iPhone X.iPhone XR.iPhone XS.iPhone XS ...