初识STL之string容器

1.string基本概念

本质:

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

string和char*区别:

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

2.string构造函数

构造函数原型

string();              //创建一个空的字符串
string(const char*s);  //使用字符串s初始化

string(const string& str); //使用一个string对象初始化另一个string对象
string(int n,char c);  //使用n个字符c初始化
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string s1; //默认构造
	
	const char* str = "hello world"; // string str = "hello world";
	
	string s2(str);  
	cout<<"s2= "<<s2<<endl;
	
	string s3(s2);
	cout<<"s3= "<<s3<<endl;
	
	string s4(10,'a');
	cout<<"s4= "<<s4<<endl;
}

int main()
{
	test1();
}

3.string赋值操作

赋值的函数原型:

	string &operator = (const char* s);   //char*类型字符串赋值给当前的字符串 
	string &operator = (const string &s); //把字符串s赋给当前的字符串 
	string &operator = (char c);          //字符赋值给当前的字符串 
	string &assign(const char* s);        //把字符串s给当前的字符串 
	string &assign(const char* s,int n);  //把字符串s的前n个字符赋给当前的字符串 
	string &assign(const string* s);      //把字符串s赋给当前的字符串 
	string &assign(int n,char c);         //用n个字符c赋给当前字符串 
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1;
	str1 = "hello world";
	cout<<"str1 = "<<str1<<endl; 
	
	string str2;
	str2 = str1;
	cout<<"str2 = "<<str2<<endl;
	
	string str3;
	str3 = "a";
	cout<<"str3 = "<<str3<<endl;  
	
	string str4;
	str4.assign("hello c++");
	cout<<"str4 = "<<str4<<endl; 
	
	string str5;
	str5.assign("hello c++",5);
	//hello
	cout<<"str5 = "<<str5<<endl; 
	
	string str6;
	str6.assign(str4);
	cout<<"str6 = "<<str6<<endl; 
	
	string str7;
	str7.assign(10,'a');
	cout<<"str7 = "<<str7<<endl; 
	
}

int main()
{
	test1();
}

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);
	//把字符串s从pos开始的n个字符连结到当前字符串结尾 
	string& append(const string &s,int pos,int n); 
	
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "I";
	str1.append(" love ");
	cout<<"str1 = "<<str1<<endl;
	

	str1.append("game asdafa",4);
	cout<<"str1 = "<<str1<<endl;
	
	string str2 = " LOL DNF";
	str1.append(str2);
	cout<<"str2 = "<<str1<<endl;
	
	str1.append(str2,0,4);
	cout<<"str3 = "<<str1<<endl;
	
}

int main()
{
	test1();
}

5.string查找跟替换

查找跟替换的函数原型

	//查找str第一次出现的位置,从pos开始查找 
	int find(const string& str,int pos = 0)const;  
	//查找s第一次出现的位置,从pos开始查找 
	int find(const char* s,int pos = 0)const;
	//从pos位置查找s的前n个字符第一次出现的位置 
	int find(const char* s,int pos = 0,int n)const;
	//从pos位置查找c的前n个字符第一次出现的位置
	int find(const char c,int pos = 0)const;
	//rfind和find用法一样,不同的是rfind是从后往前查找 
	int rfind(const string& str,int pos = 0)const;	
	int rfind(const char* s,int pos = 0)const;	
	int rfind(const char* s,int pos = 0,int n)const;
	int rfind(const char c,int pos = 0)const;
	
	//替换pos开始n个字符的字符串str 
	string& replace(int pos,int n,const string& str);
	//替换从pos开始的n个字符为字符串s 
	string& replace(int pos,int n,const char* s);
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!" ;
	int pos = str1.find("l");
	int r_pos = str1.rfind("l"); 
	cout<<"pos = "<<pos<<" r_pos= "<<r_pos<<endl;
	
	str1.replace(2,2,"222");
	//he222o world!
	cout<<"str1 = "<<str1<<endl;

	
}

int main()
{
	test1();
}

6.string字符串比较

比较方式:
字符串比较是按字符ASCII码经行对比

=返回 0
>返回 1
<返回 -1

函数原型:

	int compare(const string& s)const;//与字符串s比较 
	int compare(const char* s)const;
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!";
	string str2 = "hello world!";
	
	if(str1.compare(str2)==0)  cout<<"str1=str2"<<endl;
	
	string str3 = "hello world!";
	string str4 = "xello world!";
	
	if(str3.compare(str4)<0)  cout<<"str1<str2"<<endl;
	
	string str5 = "hello world!";
	string str6 = "aello world!";
	
	if(str5.compare(str6)>0)  cout<<"str1>str2"<<endl;
	
}

int main()
{
	test1();
}

7.string字符存取

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

	char& operator[](int n);
	char& at(int n);
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!";
	for(int i=0;i<str1.size();i++)
		cout<<str1[i]<<" ";
	cout<<endl;
	for(int i=0;i<str1.size();i++)
		cout<<str1.at(i)<<" ";

	
}

int main()
{
	test1();
}

8.string字符的插入和删除

函数原型

	string& insert(int pos,const char* s);
	string& insert(int pos,const string& str);	
	string& insert(int pos,int n,char c); //从指定位置插入字符串 

	string& erase(int pos,int n = npos);//删除从pos开始的n个字符 

#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!";
	str1.insert(2,"222");
	cout<<"str1 = "<<str1<<endl;
	
	string str2 = "good";
	str1.insert(2,str2);
	cout<<"str1 = "<<str1<<endl;
	
	str1.insert(2,4,'a');
	cout<<"str1 = "<<str1<<endl;
	
	str1.erase(2,11);
	cout<<"str1 = "<<str1<<endl;
}

int main()
{
	test1();
}

9.string子串

函数原型

string substr(int pos = 0,int n = npos) const //返回由pos开始的n个字符组成的字符串
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "abcdefg";
	string str2 = str1.substr(0,3);
	cout<<str2<<endl;
}

int main()
{
	test1();
}

10.String字符串转换成整数函数

	string s1= "1234567";
	int a = stoi(s1);
	cout<<a<<endl;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值