​

重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!

 生命就像一朵花,要拼尽全力绽放!死磕自个儿,身心愉悦!

写在前面,本篇章主要介绍STL中常用容器string。

1.1 string的基本概念

本质:

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

string和char*的区别:

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

特点:

(1) string类内部封装了很多成员方法:

——查找find、拷贝copy、删除delete、替换replace、插入insert

(2) string管理了char*所分配的内存,不用担心复制或取值越界等问题,由类内部进行负责。

1.2 string的构造函数

构造函数原型:

  • string();                    创建一个空字符串,例如:string str;
  • string(const char* s);        使用字符串s初始化
  • string(const string& str)    使用一个string对象初始化另一个string对象
  • string(int n,char c)            使用n个字符c初始化string

示例:


#include <iostream>
#include <string> using namespace std;
// string的构造函数
// string(); 创建一个空字符串,例如:string str;
// string(const char* s); 使用字符串s初始化
// string(const string& str) 使用一个string对象初始化另一个string对象
// string(int n,char c) 使用n个字符c初始化string
void test() {
string s; const char* str = "sdfd";
string s1(str);
cout << "s1 = " << s1 << endl; string s2(s1);
cout << "s2 = " << s2 << endl; string s3(5, 'x');
cout << "s3 = " << s3 << endl;
} int main() {
test(); system("pause");
return 0;
}

1.3 string赋值操作

作用:

给string进行赋值

赋值函数原型:

  • string& operator=(const char* s);                   char*类型字符串        赋值给当前string
  • string& operator=(const string& s);                string类型s                 赋值给当前string
  • string& operator=(char c);                              字符                            赋值给当前string
  • string& assign(const char* s);                        字符串s                       赋值给当前string
  • string& assign(const char* s, int n);               字符串s的前n个字符    赋值给当前string
  • string& assign(const string& s);                     字符串s                        赋值给当前string
  • string& assign(int n, char c);                           用n个字符c                  赋值给当前string

示例:


#include <iostream>
#include <string> using namespace std; //string赋值操作
//string& operator=(const char* s); char*类型字符串 赋值给当前string
//string& operator=(const string& s); string类型s 赋值给当前string
//string& operator=(char c); 字符 赋值给当前string
//string& assign(const char* s); 字符串s 赋值给当前string
//string& assign(const char* s, int n); 字符串s的前n个字符 赋值给当前string
//string& assign(const string& s); 字符串s 赋值给当前string
//string& assign(int n, char c); 用n个字符c 赋值给当前string void test() {
string s1;
s1 = "hello";
cout <<"s1 = "<< s1 << endl; string s2;
s2 = s1;
cout << "s2 = " << s2 << endl; string s3;
s3 = 'a';
cout << "s3 = " << s3 << endl; string s4;
s4.assign("hi");
cout << "s4 = " << s4 << endl; string s5;
s5.assign("teststring", 4);
cout << "s5 = " << s5 << endl; string s6;
s6.assign(s5);
cout << "s6 = " << s6 << endl; string s7;
s7.assign(5, 'c');
cout << "s7 = " << s7 << endl;
} int main() {
test(); system("pause");
return 0;
}

1.4 string字符串拼接

作用:

在字符串末尾追加字符串

函数原型:

  • string& operator+=(const char* str);                //重载+=操作符
  • string& operator+=(const char c);                    //重载+=操作符
  • string& operator+=(const string& str);              //重载+=操作符
  • string& append(const char* s);                        //把字符串s连接到当前字符串结尾
  • string& append(const char* s, int n);                //把字符串s的前n个字符连接到当前字符串结尾
  • string& append(const string& s);                      //同operator+=(const string& str)
  • string& append(const string& s, int pos, int n);  //将字符串s中从pos开始的n个字符连接到当前字符串结尾

示例:


#include <iostream>
#include <string> using namespace std; //string& operator+=(const char* str); //重载+=操作符
//string& operator+=(const char c); //重载+=操作符
//string& operator+=(const string& str); //重载+=操作符
//string& append(const char* s); //把字符串s连接到当前字符串结尾
//string& append(const char* s, int n); //把字符串s的前n个字符连接到当前字符串结尾
//string& append(const string& s); //同operator+=(const string& str)
//string& append(const string& s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾 void test() {
string s1("hello");
s1 += " echo";
cout << "s1 = " << s1 << endl; string s2;
s2 += 'x';
cout << "s2 = " << s2 << endl; string s3="say ";
s3 += s1;
cout << "s3 = " << s3 << endl; string s4 = s3;
s4.append(" aaa");
cout << "s4 = " << s4 << endl; string s5 = s3;
s5.append(" 12345",3);
cout << "s5 = " << s5 << endl; string s6 = s3;
s6.append(s2);
cout << "s6 = " << s6 << endl; string s7 = s3;
s7.append(" 12345", 1,3);
cout << "s7 = " << s7 << endl;
} int main() {
test(); system("pause");
return 0;
}

1.5 string查找和替换

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串

函数原型:

  • int find(const string& str, int pos = 0) const;         //查找str第一次出现位置,从pos开始查找
  • int find(const char* s, int pos = 0) const;              //查找s第一次出现位置,从pos开始查找
  • int find(const char* s, int pos, int n) const;           //从pos位置查找s的前n个字符第一次位置
  • int find(const char c, int pos = 0) const;               //查找字符c第一次出现位置,从pos开始查找
  • int rfind(const string& str, int pos = npos) const;  //查找str最后一次位置,从pos开始查找
  • int rfind(const char* s, int pos = npos) const;       //查找s最后一次出现位置,从pos开始查找
  • int rfind(const char* s, int pos, int n) const;          //从pos查找s的前n个字符最后一次位置
  • int rfind(const char c, int pos = 0) const;              //查找字符c最后一次出现位置
  • string& replace(int pos, int n, const string& str);   //替换从pos开始n个字符为字符串str
  • string& replace(int pos, int n, const char* s);       //替换从pos开始的n个字符为字符串s

注意:

  • find查找,从左往右;rfind查找,从右往左;
  • find和rfind都返回查找到的字符串所在的第一个字符位置,找不到返回-1;
  • replace在替换时,要指定原串被替换的起始位置,被替换字符个数,用什么字符串来替换。

示例:


#include <iostream>
#include <string> using namespace std; //int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
//int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
//int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
//int find(const char c, int pos = 0) const; //查找字符c第一次出现位置,从pos开始查找
//int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
//int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
//int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
//int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
//string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
//string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s // 查找
void test() {
string s1 = "abcdefgde";
int pos = s1.find("de");
if (pos == -1) {
cout << "not find" << endl;
}
else {
cout << "pos = " << pos << endl; } // find和rfind的区别:
// find从左往右找,rfind从右往左找
pos =s1. rfind("de");
cout << "pos = " << pos << endl;
} // 替换
void test2() {
string s1 = "abcdefg";
// 从位置1起 3 个字符替换为 1234
s1.replace(1, 3, "1234");
cout << "s1 = " << s1 << endl; s1 = "abcdefg";
// 从位置1起 3 个字符替换为 12
s1.replace(1, 3, "12");
cout << "s1 = " << s1 << endl; } int main() {
//test();
test2(); system("pause");
return 0;
}

1.6 string字符串比较

作用:

字符串之间大小比较

比较方式:

字符串比较是按照字符的ASCII码进行比较的。

= 返回 0

> 返回 1

< 返回 -1

函数原型:

  • int compare(const string& s) const;        字符串与字符串比较
  • int compare(const char* s) const;           字符串与字符串比较

注意:

  字符串比较一般用于比较两个字符串是否相等。

示例:


#include <iostream>
#include <string> using namespace std; void test() {
string s1 = "hello";
string s2 = "hello";
int ret = s1.compare(s2);
if (ret == 0) {
cout << "s1 等于 s2" << endl;
}
else if (ret > 0) {
cout << "s1 大于 s2" << endl;
}
else {
cout << "s1 小于 s2" << endl;
}
} int main() {
test(); system("pause");
return 0;
}

1.7 string字符存取

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

  • char& operator[](int n);        通过[]数组下标形式取字符
  • char& at(int n);                     通过at方法获取字符

示例:



#include <iostream>
#include <string> using namespace std; void test() {
string s = "abcdefg"; // 获取单个字符
cout << s[1] << endl;
cout << s.at(1) << endl; // 修改单个字符
s[1] = 'x';
cout << s << endl;
s.at(1) = 'y';
cout << s << endl;
} int main() {
test(); system("pause");
return 0;
}

1.8 string插入和删除

作用:

对string字符串进行插入和删除字符操作

函数原型:

  • string& insert(int pos, const char* s);             //在指定位置n处,插入字符串
  • string& insert(int pos, const string& str);        //在指定位置n处,插入字符串
  • string& insert(int pos, int n, char c);               //在指定位置插入n个字符c
  • string& erase(int pos = 0, int n = npos);               //删除从Pos开始的n个字符

示例:


#include <iostream>
#include <string> using namespace std; //string& insert(int pos, const char* s); //在指定位置n处,插入字符串
//string& insert(int pos, const string& str); //在指定位置n处,插入字符串
//string& insert(int pos, int n, char c); //在指定位置插入n个字符c
//string& erase(int pos, int n = npos); //删除从Pos开始的n个字符
void test() {
// 插入
string s = "abc";
s.insert(1, "123");
cout << "s = " << s << endl; s.insert(2, 3,'x');
cout << "s = " << s << endl; // 删除
s.erase(2, 3);
cout << "s = " << s << endl; } int main() {
test(); system("pause");
return 0;
}

1.9 string子串

作用:

从字符串中获取想要的子串

函数原型:

string substr(int pos =0 ,int n=npos) const; 返回由pos开始的n个字符组成的字符串

示例:


#include <iostream>
#include <string> using namespace std; void test() {
string s = "abcdefg";
cout << s.substr(1, 3) << endl;
} void test2() {
string email = "tom@163.com";
int pos = email.find("@");
string userName = email.substr(0, pos);
cout << userName << endl;
} int main() {
test();
test2(); system("pause");
return 0;
}

​

5 STL-string的更多相关文章

  1. 深入剖析 linux GCC 4.4 的 STL string

    转自: 深入剖析 linux GCC 4.4 的 STL string 本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Wri ...

  2. 格式字符串分配stl::string

    代码非常easy,不解释,直接在代码: #include <cstdio> #include <cstdarg> #include <iostream> using ...

  3. 浅谈C++ STL string容器

    浅谈C++ STL string容器 本篇随笔简单讲解一下\(C++STL\)中\(string\)容器的使用方法及技巧. string容器的概念 其实\(string\)并不是\(STL\)的一种容 ...

  4. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  5. 转C++之stl::string写时拷贝导致的问题

    前几天在开发某些数据结构到文件的 Dump 和 Load 功能的时候, 遇到的一个 bug . [问题复现] 问题主要出在 Load 过程中,从文件读取数据的时候, 直接使用 fread 的去操作 s ...

  6. [转载] C++ STL string的Copy-On-Write技术

    原文: http://coolshell.cn/articles/12199.html stl的string是经过严格优化的, 深入理解对以后编程过程中应用string非常有益处, 感谢左耳朵耗子的精 ...

  7. stl string

    10.2.1 STL的string 1String概念 ²  string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都 ...

  8. [转] 深入剖析 linux GCC 4.4 的 STL string

    本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Write技术. 平台:x86_64-redhat-linux gcc ver ...

  9. STL - string(典型操作demo)

    1String概念  string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都可以用来表示字符串,那么二者有什么区别呢 ...

  10. STL——string

    C++之string类型详解 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个泛型类出现,他集成的操作函 ...

随机推荐

  1. Python中的super函数,你熟吗?

    摘要:经常有朋友问,学 Python 面向对象时,翻阅别人代码,会发现一个 super() 函数,那这个函数的作用到底是什么? 本文分享自华为云社区<Python中的super函数怎么学,怎么解 ...

  2. .Net7 内容汇总(1)

    .Net7 RC1发布 在9月14号,.Net7 RC1正式发布了. 按照微软的说法 This is the first of two release candidates (RC) for .NET ...

  3. Helm包管理

    Helm Kubernetes 包管理工具 Helm 可以帮助我们管理 Kubernetes 应用程序 - Helm Charts 可以定义.安装和升级复杂的 Kubernetes 应用程序,Char ...

  4. 如何在Elasticsearch中使用pipeline API来对事件进行处理

    一个processor就像是Logstash里的一个filter pipeline是一组processor

  5. useContext 解决函数父子组件传值

    1在父组件外部定义变量A创建上下文,2在父组件使用变量A<A.Provider> <子组件/> </A.Provider> ,3.在子组件中创建变量使用useCon ...

  6. Css3中自适应布局单位vh、vw

    视口单位(Viewport units) 什么是视口? 在桌面端,视口指的是在桌面端,指的是浏览器的可视区域:而在移动端,它涉及3个视口:Layout Viewport(布局视口),Visual Vi ...

  7. [CG从零开始] 3. 安装 pyassimp 库加载模型文件

    assimp 是一个开源的模型加载库,支持非常多的格式,还有许多语言的 binding,这里我们选用 assimp 的 python 的 binding 来加载模型文件.不过社区主要是在维护 assi ...

  8. 洛谷P1719 最大加权矩形 (DP/二维前缀和)

    题目描述也没啥好说的,就是给你个你n*n的矩形(带权),求其中最大权值的子矩阵. 首先比较好想的就是二维前缀和,n<=120,所以可以用暴力. 1 #include<bits/stdc++ ...

  9. Oracle字段约束

    初识约束 约束是数据库用来确保数据满足业务规则的手段,对数据做的条件限制. 约束的类型 1. 主键约束(PRIMARY KEY) 2. 唯一性约束(UNIQUE) 3. 非空约束(NOT NULL) ...

  10. 华为设备配置和使用FTP服务命令

    配置SFTP Server与Client server:aaa 进入aaa视图 local-user huawei2 password cipher huawei2 设置用户名和密码 local-us ...