STL string容器

12 篇文章 0 订阅
9 篇文章 0 订阅
本文详细介绍了C++ STL中的string容器,包括其与C语言风格字符串的区别,以及string的构造、赋值、拼接、查找、替换、插入、删除和获取子串等常用操作。此外,还展示了在实际开发中的应用,如从邮箱地址中提取用户名。通过这些内容,读者可以更好地理解和掌握C++中string的使用。
摘要由CSDN通过智能技术生成

STL中的容器:

string容器的常用接口及用法:


stringstringC++ 风格的字符串,但是 string 本质是一个类;


C语言风格字符串和 C++ 风格字符串和区别:

1.C语言字符串:char * str = "Hello world";char str[] = "Hello world";

数组=指针,C语言字符串本质是一个指针;

2.C++ 风格字符串:string str;

string 本质是一个类,这个类的内部封装了 char *,管理着这个字符串,是一个 char * 的容器;


string 特点:

1.string 类内部封装了很多的成员函数(例如:查找find、拷贝 copy、删除delete、插入insert、替换replace…)

2.string 管理 char *分配的内存,不用担心数组越界的问题,由类内部进行解决;


各种函数接口具体如何使用,下面的代码块中会有详细的使用方法


string 构造函数:

1.string(); 无参构造,创建一个空的字符串;

2.string(const char * s); 有参构造,传入 s 字符串初始化我们新创建的 string 对象;

3.string(const string & s); 拷贝构造,根据一个 string 对象初始化我们新创建的 string 对象;

4.string(int n,char c); 使用n个c字符来初始化我们新创建的 string 对象;

//string的构造函数

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()
{
	string s1;  //无参构造,创建一个空的字符串
	
	string s2("Yauge");  //有参构造,传入s字符串初始化我们新创建的string对象
	
	string s3(s2);  //拷贝构造,根据一个string对象初始化我们新创建的string对象
	
	string s4(5,'a');  //使用n个a字符来初始化我们新创建的string对象
	
	cout << "s1=" << s1 << endl;
	cout << "s2=" << s2 << endl;
	cout << "s3=" << s3 << endl;
	cout << "s4=" << s4 << endl;
}

int main()
{
	test_1();
	
	system("pause");
	return 0;
}

string 赋值:

1.string& operator=(const char * s); 把字符串 s 赋值给当前的字符串;

2.string& operator=(const char & 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 char & s); 把字符串 s 赋值给当前的字符串;

7.string& assign(int n,char c); 把n个字符赋值给当前的字符串;

注意:前三种是通过重载运算符的方式来给字符串赋值,后四种是通过成员函数对字符串赋值的;

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //通过重载运算符的方式给字符串赋值 
{
	string s1 = "Yauge";
	string s2 = s1;
	//string s3 = 'a';  //这种方法编译器会报错,但把单引号换成双引号就可以,不知道换成双引号之后,调用的是第一种方式还是第三种方式 
	
	cout << "s1=" << s1 << endl;
	cout << "s2=" << s2 << endl;
	//cout << "s3=" << s3 << endl;
}

void test_2()  //通过成员函数给字符串赋值 
{
	string s1;
	s1.assign("Yauge");
	
	string s2;
	s2.assign("Yauge",3);
	
	string s3;
	s3.assign(s1);
	
	string s4;
	s4.assign(5,'a');
	
	cout << "s1=" << s1 << endl;
	cout << "s2=" << s2 << endl;
	cout << "s3=" << s3 << endl;
	cout << "s4=" << s4 << endl;
}

int main()
{
	test_1();
	test_2();
	
	system("pause");
	return 0;
} 

string 拼接:

1.string& operator+(const char * s); 把字符串 s 拼接到当前的字符串后面;

2.string& operator+(const string & s); 把字符串 s 拼接到当前的字符串后面;

3.string& operator(const char c); 把字符拼接到当前的字符串后面;

4.string& append(const char * s); 把字符串 s 拼接到当前的字符串后面;

5.string& append(const char * s,int n); 把字符串 s 的前n个字符拼接到当前的字符串后面;

6.string& append(const string & s); 把字符串 s 拼接到当前的字符串后面;

7.string& append (const string & s,int pos,int n);

把字符串 spos 位置开始的 n 个字符拼接到当前的字符串后面;

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //通过重载运算符的方式给字符串拼接 
{
	string s1 = "Yauge";
	s1 += " Hello world!";  //把字符串s拼接到当前的字符串后面
	
	string s2 = " Hello world!";
	s1 += s2;  // 把字符串s2拼接到当前的字符串后面
	
	s1 += 'a';   //把字符a拼接到当前的字符串后面
	
	cout << "s1=" << s1 << endl; 
}

void test_2()  //通过成员函数给字符串拼接 
{
	string s3 = "Yauge";
	s3.append(" Hello world!");  //把字符串s拼接到当前的字符串后面
	
	s3.append(" Hello world",6);  //把字符串s的前n个字符拼接到当前的字符串后面
	
	string s4 = " Hello world!";
	s3.append(s4);  //把字符串s4拼接到当前的字符串后面
	
	s3.append(s4,7,6);  //把字符串s4从pos位置开始的n个字符拼接到当前的字符串后面
	
	cout << "s3=" << s3 << endl;
}

int main()
{
	test_1();
	test_2();
	
	system("pause");
	return 0;
} 

string 查找和替换:

查找:在一个字符串中查找另一个字符串是否存在;


find 成员函数:从左往右查找,若原来的字符串中存在目标字符串,则返回目标字符串第一次出现的位置,若不存在,返回 -1

1.int find(const char * s,int pos = 0) const; 从左往右查找字符串 s 第一次出现的位置,从 pos 位置开始查找;

2.int find(const string & s,int pos = 0) const; 从左往右查找字符串 s 第一次出现的位置,从 pos 位置开始查找;

3.int find(const char * s,int pos,int n) const; 从左往右查找字符串 s 前n个字符第一次出现的位置,从 pos 开始查找;

4.int find (char c,int pos = 0) const; 从左往右查找某个字符第一次出现的位置,从 pos 位置开始查找;


rfind 成员函数:从右往左查找,若原来的字符串中存在目标字符串,则返回目标字符串第一次出现的位置,若不存在,返回 -1

1.int rfind(const char * s,int pos = npos) const; 从右往左查找字符串 s 第一次出现的位置,从 pos 位置开始查找;

2.int rfind(const string & s,int pos = npos) const; 从右往左查找字符串 s 第一次出现的位置,从 pos 位置开始查找;

3.int rfind(const char * s,int pos,int n) const; 从右往左查找字符串 s 前n个字符第一次出现的位置,从 pos 开始查找;

4.int rfind (char c,int pos = 0) const; 从右往左查找某个字符第一次出现的位置,从 pos 位置开始查找;


注意:位置永远是固定的,不会因为你从左边数还是右边数而改变;左边数还是右边数,只决定开始数的位置;

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //find函数 (a-0  b-1  c-2 ... 最后出现的e-8)
{
	string s1 = "abcdefgde";
	
	int pos = s1.find("de");  //从左往右开始查找字符串 "de" 的位置 
	
	pos = s1.find("cdefg",0,3);  //从左往右开始查找字符串 "cdefg" 前三个字符("cde")的位置 
	
	pos = s1.find('e');  //从左往右开始查找字符 'e' 的位置 
	
	if(pos==-1)
	{
		cout << "目标字符串不存在!" << endl; 
	}
	else
	{
		cout << "目标字符串存在,位置为 " << pos << endl;
	}
}

void test_2()  //rfind函数 (a-0  b-1  c-2 ... 最后出现的e-8)
{
	string s1 = "abcdefgde";
	
	int pos = s1.rfind("de");  //从右往左开始查找字符串 "de" 的位置
	
	pos = s1.rfind("fgde",8,3);  //从右往左开始查找字符串 "fgde" 前三个字符("fgd")的位置
	
	pos = s1.find('e');  //从左往右开始查找字符 'e' 的位置
	
	if(pos==-1)
	{
		cout << "目标字符串不存在!" << endl; 
	}
	else
	{
		cout << "目标字符串存在,位置为 " << pos << endl;
	}
}

int main()
{
	test_1();
	test_2();
	
	system("pause");
	return 0;
} 

替换:在一个字符串中的指定位置替换成另一个字符串;

1.string& replace(int pos,int n,const char * s);pos 位置开始,n个字符替换为字符串 s

2.string& replace (int pos,int n,const string & s);pos 位置开始,n个字符替换为字符串 s

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //替换 
{
	string s1 = "Yauge";
	
	s1.replace(0,5,"Hello world!");  //从0号位置开始,紧跟着的5个字符,替换为Hello world!
	
	cout << "s1=" << s1 << endl;
	
}

int main()
{
	test_1();  //输出:s1=Hello world!
	
	system("pause");
	return 0;
} 

string 比较:每个字符都根据 ASCII 码表逐一进行比较;

1.int compare(const char * s) const; 比较某一个字符串与字符串 s 的大小;

2.int compare(const string & s) const; 比较某一个字符串与字符串 s 的大小;

返回值:

-1:某字符串小于字符串 s

0:某字符串与字符串 s 相等;

1:某字符串大于字符串 s


string 单个字符的存取:

1.char& operator[](int n); 通过重载[]的方式,存取字符串的每一个字符;

(可以联系到C语言中的字符串,C语言中的字符串即为字符数组或者指针)

2.char& at(int n); 通过成员函数 at() 访问字符串中的没一个字符;

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //替换 
{
	int i = 0;
	string s1 = "Yauge";
	
	//通过[]的方式访问字符串中的每一个字符 
	for(i=0;i<5;i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	
	//通过成员函数at()访问字符串中的每一个字符
	for(i=0;i<5;i++)
	{
		cout << s1.at(i) << " ";
	}
	cout << endl;
	
	//通过[]的方式修改字符
	s1[0] = 'H';
	cout << "s1=" << s1 << endl;
	
	//通过at()函数修改字符
	s1.at(0) = 'H';
	cout << "s1=" << s1 << endl; 
	
}

int main()
{
	test_1();
	
	system("pause");
	return 0;
} 

string 插入和删除:

插入:

1.string& insert(int pos,const char * s);pos 位置开始,插入字符串 s

2.string& insert(int pos,const string & s);pos 位置开始,插入字符串 s

3.string& insert(int pos,int n,char c);pos 位置开始,插入n个字符;

删除:

1.string& erase(int pos,int n = npos);pos 位置开始,删除n个字符;

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //替换 
{
	string s1 = "Yauge";
	
	s1.insert(2,"hello");  //插入
	
	s1.erase(2,5);  //删除 
	
	cout << "s1=" << s1 << endl;	
}

int main()
{
	test_1();
	
	system("pause");
	return 0;
} 

string 中获取子串:

1.string substr(int pos = 0,int n = npos) const;pos 位置开始,截取n个字符;

在实际开发中,灵活的运用子串的功能,可以获取到有用的信息;

#include <iostream>
#include <string>  //使用STL中的容器,得包含它的头文件 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

void test_1()  //字符串中获取子串 
{
	string s1 = "Hello world!";
	
	string SubStr = s1.substr(0,5);  //从0位置开始,获取连续的5个字符 
	
	cout << "SubStr=" << SubStr << endl;
}

//实际应用
void test_2()
{
	string s1 = "zhangsan@sina.com";  //在邮箱里,往往@前面的是用户名,若我们想得到某个人的用户名,可以利用获取子串的方法 
	string s2 = "lisi@sina.com";
	
	int pos_1 = s1.find("@");  //先定位@的位置
	int pos_2 = s2.find("@");
	
	string Sub_s1 = s1.substr(0,pos_1);
	string Sub_s2 = s2.substr(0,pos_2);
	
	cout << "Sub_s1=" << Sub_s1 << endl;
	cout << "Sub_s2=" << Sub_s2 << endl;
} 

int main()
{
	test_1();
	test_2();
	
	system("pause");
	return 0;
} 

以上就是STL中string容器的一些常用接口和用法啦O(∩_∩)O。笔记中有错误的地方,欢迎指出,欢迎大家讨论!

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值