• STL:string容器详解


    string是C++风格的字符串,而string本质是一个类
    我们在C语言的字符串都是用char*表示,那么string与char*有什么区别?
    即:char*是一个指针,而string是一个类,类的内部维护了一个char*,同时string类内部封装了很多成员方法,例如查找find,拷贝copy,删除delete,替换replace以及插入insert

    1.string构造函数

    构造函数描述
    string()创建一个空的字符串
    string(const char* s)使用字符串s初始化
    string(const string& str)使用一个string对象初始化另一个string对象
    string(int n,char c)使用n个字符c初始化
    #include
    string s1;//默认构造函数
    const char* str="Hello World";
    string s2(str);//打印输出为Hello World
    string s3(s2);//打印输出为Hello World
    string s4(10,'a');//打印输出为 aaaaaaaaaa
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    总结:string的多种构造方式没有可比性,根据实际情况灵活使用即可。

    2.string字符串的拼接

    我们要实现string字符串吧的拼接,通常通过使用重载的+=或append函数来实现,具体代码如下所示:

    #include
    #include
    using namespace std;
    //字符串的拼接
    /*
    string字符串的拼接
    string operator+=(const char* str)
    string operator+=(const char c)
    string operator+=(const string& str)
    string& append(const char &s)
    string& append(const char *s,int n) //将字符串的前n个字符连接到当前字符串的结尾
    string& append(const string &s) 
    string& append(const string &s,int pos,int n) //字符串s中从pos开始的n个字符串连接到字符串结尾
    */
    int main() {
    	//string operator+=(const char* str)
    	string str1 = "我";
    	str1 += ("爱学习编程");
    	cout << str1 << endl;
    
    	//string operator+=(const char c)
    	str1 += ':';
    	cout << str1 << endl;
    
    	//string operator+=(const string& str)
    	string str2 = "C++ Java Python";
    	str1 += str2;
    	cout << str1 << endl;
    
    	string str3 = "I love ";
    	string str4 =str3;
    	str3.append("programmingxxxxxx", 11);
    	cout << str3 << endl;
    	str4.append("math programming", 0, 4);
    	cout << str4 << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    3.string的查找和替换

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

    函数函数描述
    int find(const string& str,int pos=0)查找str第一次出现位置 从pos开始查找 未查到则返回-1
    int rfind(const string& str,int pos=npos)/查找str最后一次出现的位置 从pos开始查找 未找到则返回-1
    string& replace(int pos,int n,const string& str)替换从pos开始的n个字符为字符串是s

    具体API的使用如下所示:

    #include
    #include
    using namespace std;
    //字符串的查找和替换
    int main() {
    	// 查找
    	string str1 = "abcdefgde";
    	//int find(const  string& str,int pos=0) //查找str第一次出现位置 从pos开始查找
    	int pos=str1.find("de");
    	if (pos != -1) {
    		cout << "找到字符串 pos=" << pos << endl;
    	}
    	else {
    		cout << "未找到该字符串" << endl;
    	}
    	/*
    	find与rfind的区别
    	find是从左往右查找
    	rfind是从左往右查找
    	*/
    	pos = str1.rfind("de");
    	cout << "找到字符串 pos=" << pos << endl;
    
    	//替换
    	string str2 = "abcdefg";
    	str2.replace(0, str2.size(),"A boy can do everything for girl");
    	cout << str2 << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    这里的STL中的replace与Python的replace函数使用方式不同,Python的replace是直接替换,而C++中的replace需要指定替换的起始位置,替换的字符数目以及替换成什么样的字符串。

    4.string字符串的比较

    字符串的比较是将字符串中的每个字符的ASCII进行对比,相等则返回0,大于则返回1,小于则返回-1;

    函数描述
    int compare(const string &s)与字符串s进行比较
    #include
    #include
    using namespace std;
    //字符串的比较
    int main() {
    	string str1 = "aello";
    	string str2 = "hello";
    	if (str1.compare(str2)==0) {
    		cout << "str1等于str2" << endl;
    	}
    	else if (str1.compare(str2) > 0) {
    		cout << "str1大于str2" << endl;
    	}
    	else {
    		cout << "str1小于str2" << endl;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    这里需要注意的是:比较过程中遇到第一个不相等的字符就会比较结束,所以后面的字符完全没有影响!

    5.string字符存取

    字符串的存取就是从字符串中读取或修改单个字符;主要有两种方式,具体如下所示:

    函数描述
    char& operator[](int n)通过[]方式取字符
    char& at(int n)通过at方法获取字符
    #include
    #include
    using namespace std;
    int main() {
    	string str = "JavaC++PythonSpringBootVue";
    	cout << str << endl;
    	//通过[]访问单个字符
    	for (int i = 0; i < str.size(); i++) {
    		cout << str[i] << " ";
    	}
    	cout << endl;
    	//通过at的方法
    	for (int i = 0; i < str.size(); i++) {
    		cout << str.at(i) << " ";
    	}
    	cout << endl;
    	//修改字符 可以使用[]/at 或者直接使用replace函数
    	str.replace(5, 2, "#");
    	cout << str << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    6.string字符串的插入与删除

    string字符串的插入主要使用insert函数,而删除主要使用的是erase函数,具体如下所示:

    函数描述
    string& insert(int pos,const char *s)插入字符串
    string& insert(int pos,const string& str)插入字符串
    string& insert(int pos,int n,char c)在指定位置插入n个字符c
    string& erase(int pos,int n=npos)删除从pos开始的n个字符
    #include
    #include
    using namespace std;
    //字符串的插入与删除
    int main() {
    	string str = "Java";
    	//string& insert(int pos,const string& str);//插入字符串
    	str.insert(1, "Python");
    	cout << str << endl;
    	//string& erase(int pos,int n=npos);//删除从pos开始的n个字符
    	str.erase(1, 6);
    	cout << str << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    7.子串获取

    我们可以使用substr函数来从字符串中截取子串;

    函数描述
    string substr(int pos=0,int n=npos) const返回由pos开始的n个字符组成的字符串
    #include
    #include
    using namespace std;
    //字符串的子串截取
    int main() {
    	string str = "abcdefg";
    	//string substr(int pos=0,int n=npos) const;返回由pos开始的n个字符组成的字符串
    	string subStr = str.substr(1,3);//bcd
    	cout << "subStr=" << subStr << endl;
    
    	//使用操作
    	string email = "zhangsan@sina.com";
    	//从邮件的地址中获取到用户名信息zhangsan
    	int pos=email.find('@');
    	string username = email.substr(0, pos);
    	cout << "用户名username为" << username << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    灵活的去运用求子串功能,可以在实际开发中获取有效的信息。

  • 相关阅读:
    Elasticsearch7 入门 & 进阶
    分布式存储系统之Ceph集群访问接口启用
    【vue3】10.跟着官网学习vue3-表单输入绑定,双向绑定,封装input组件,封装ant-design-vue的input组件
    JDBC操作BLOB类型字段
    白蛋白纳米粒|莫西沙星小鼠血清白蛋白MSA纳米粒|利多卡因大鼠血清白蛋白RSA纳米粒
    五、XML&Tomcat&Http协议
    Java零基础入门-如何代码模拟斗地主洗牌发牌动作(上)?
    通过Power Platform自定义D365 CE 业务需求 - 5. 使用Power Virtual Agent
    git上传项目至github(Linux)
    计算机网络概述
  • 原文地址:https://blog.csdn.net/qq_51447436/article/details/126665528