栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

string容器的基本用法 STL

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

string容器的基本用法 STL

目录:

1.string容器的基本概念

2.string赋值操作

3.string字符串拼接

4.string字符串查找和替换

5.string字符串比较

6.string字符存取

7.string字符串插入和删除

8.string字串获取

1.string容器的基本概念

string是c++风格的字符串,string本质上是一个类
char*是一个指针
stirng是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
string内部封装了很多成员方法
find 查找,copy拷贝,delete删除,replace替换,insert 插入
构造函数原型

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

代码如下:

#include
#include
using namespace std;
void test01()
{
	string s1;//默认构造
	const char* str = "hello world";
	//string(const char*s) 使用字符串s初始化
	string s2(str);
	cout << "s2=" << s2 << endl;
	//string(const string&str) 使用string对象初始化另一个string对象
	string s3(s2);//拷贝
	cout << "s3=" << s3 << endl;
	//string(int n,char c)使用n个字符c初始化 
	string s4(10, 'a');
	cout << "s4=" << s4 << endl;
}


int main() {
	test01();
}

2.string赋值操作

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

代码如下:

#include
#include
using namespace std;

void test01() {
	string str1;//定义一个字符串
		str1 = "hello world";//将char类型字符串赋值给当前的字符串
		cout << "str1=" << str1 << endl;

		string str2;
		str2 = str1;//把字符串1的值赋值给str2
		cout << "str2=" << str2 << endl;

		string str3;
		str3 = 'a';//把字符赋值给字符串 
		cout << "str3=" << str3 << endl;

		string str4;//利用assign函数
		str4.assign("hello c++");
		cout << "str4=" << str4 << endl;

		string str5;
		str5.assign("hello nihao", 5);//将字符串s的前5个字符赋值给当前的字符串
		cout << "str5=" << str5 << endl;

		string str6;
		str6.assign(str5);//将字符串str5赋值给字符串str6
		cout << "str6=" << str6 << endl;
		string str7;
		str7.assign(5, 'w');//用5个字符w给字符串赋值
		cout << "str7=" << str7 << endl;
}


int main() {
	test01();




}

3.string字符串拼接

string字符串拼接
1.string& operator+=(const char* str);重载+=操作符 相当于加一串
2.string& operator+=(const char c);重载+=操作符 单追加一个字符
3.string& operator+=(const string& str); 重载+=操作符
4.string& append(const char*s); 把字符串s连接到当前字符串尾
5.string& append(const *s,int n);把字符串s的前n个字符连接到当前字符串结尾 字符串s可以自己写
6.string& append(const string &s); 同operator+=(const string& str)
7.string& append(const string &s,int pos,int n)字符串s中从pos开始的n个字符连接到字符串结尾

代码如下:

#include
#include
using namespace std;

void test01()
{
	string str1 = "我";
	str1 += "爱玩游戏";//加了一串字符
	cout << "str1=" << str1 << endl;
	str1 += ':';//加了单个字符
	cout << "str1=" << str1 << endl;
	string str2 = "LOL DNF";
	str1 += str2;//加了一个字符串
	cout << "str1=" << str1 << endl;
	string str3 = "I";
	str3.append(" Love ");
	cout << "str3=" << str3<< endl;
	//string str4 = "game abcde";
	str3.append("game abcde", 4);
	cout << "str3=" << str3 << endl;
	string str4 = "LOL DNF";
	str3.append(str4);
	cout << "str3=" << str3 << endl;
	str3.append(str4,4,6);//截取字符串str4的第4到6个字符加到字符串后面
	cout << "str3=" << str3 << endl;
}



int main()
{
	test01();





	return 0;
}

4.string字符串查找和替换

函数原型 
1.int find(const string& str,int pos=0)const;//查找str第一次出现位置,从pos开始查找
2.int find(const*s,int pos=0)const;//查找s第一次出现位置,从pos开始查找
3.int find(const char * s,int pos,int n)const;//从pos位置查找s的前n个字符第一次位置
4.int find(const char s,int pos=0)const;//查找字符s第一次出现的位置
5.int rfind(const string& str,int pos=npos)const;//查找str最后一次位置,从pos开始查找
6.int rfind(const char *s,int pos=npos)const;//查找s最后一次位置,从pos开始查找
7.int rfind(const char* s,int pos,int n)const;//从pos查找s的前n个字符最后一次位置
8.int rfind(const char c,int pos=0)const;//查找字符c最后一次出现位置
9.string& replace(int pos,int n,const string& str);//替换pos开始n个字符为字符串str
10.string& replace(int pos,int n,const char* s);替换从pos开始的n个字符为字符串s

代码如下:

#include
#include
#include
using namespace std;

void test01()
{
	string str1 = "abcdefgde";
	int pos = str1.find("de");//查找d
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else {
		cout << "找到字符串,pos=" << pos << endl;//pos=3  0.1.2.3 从0开始
	}
	//rfind和find的区别,rfind从右往左查找,find从左往右查找
	 pos=str1.rfind("de");
	cout << "pos=" << pos << endl;
	int s=str1.find("cdefg", 1, 5);
	cout << "s=" << s << endl;
	string str5 = "cd";
	int result= str1.find(str5, 0);
	cout << "result=" << result<< endl;
}


//2.替换
void test02()
{
	string str1 = "abcdefg";
	//从1号位置起3个字符,替换为“1111”;
	str1.replace(1, 3, "1111");
	cout << "str1=" << str1 << endl;
}
int main()
{
	test01();
	test02();


}

5.string字符串比较

两个字符串比较时每个字符ASCLL码进行对比,相等时返回0,大于返回1,小于返回-1

#include
#include
using namespace std;
//string字符串比较  比较ASCLL码进行对比
//= 返回0
//> 返回1  >0
//<返回-1 <0
void test01()
{
	string str1 = "xello";//一个一个字符进行比较 比较ascll码
	string str2 = "zello";
	if (str1.compare(str2) == 0) {//compare比较
		cout << "str1等于str2" << endl;
	}
	else if (str1.compare(str2) > 0) {
		cout << "str1大于str2" << endl;
	}
	else if (str1.compare(str2) < 0) {
		cout << "str1小于str2" << endl;
	}
}

int main()
{
	test01();




}

6.string字符存取
方法:1.通过中括号访问单个字符

           2.通过at方式进行访问

代码如下:

#include
#include
using namespace std;
//string字符存取操作
void test01()
{
	string str = "hello";
	//cout << "str=" << str << endl;
	//1.通过中括号访问单个字符  str.size()表示字符串的长度  根据i进行一个一个输出
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";//输出单个字符  类似数组
	}
	cout << endl;
	//2.通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";//通过at方式
	}
	cout << endl;
	//修改单个字符
	str[0] = 'x';
	cout << "str=" << str << endl;
	str.at(1) = 'o';//必须是单引号来引用单个字符
	cout << "str=" << str << endl;
}


int main()
{
	test01();




	return 0;
}

7.string字符串插入和删除

插入使用:insert  删除使用:erase

代码如下:

#include
#include
using namespace std;
//对字符串进行插入和删除操作
//1.insert  插入  str.insert()
void test01()
{
	string str = "hello";
	str.insert(1, "111");//在第一个位置插入字符串111  h111ello
	cout << "str=" << str << endl;
	str.erase(1, 3);//从第一个位置起删除三个
	cout << "str=" << str << endl;
}
//2.erase  删除
void test02()
{

	string str2 = "hello";
	str2.erase(1, 3);
	cout << str2 << endl;
}



int main() {

	test01();
	test02();



}

8.string字串获取

使用函数:substr

代码如下:

#include
#include
using namespace std;
//string 子串获取
//string substr(int pos=0,int n=npos)const; string s=str.substr(初始,结束)
void test01()
{

	string str = "abcdef";
	string s = str.substr(1, 3);
	cout << "s=" << s<< endl;
}
//实用操作
void test02()
{
	string email = "lisi@sina.com";
	//从邮件地址中获取到用户名的信息
	int s=email.find("@");
	cout << s << endl;//查找找到@需要几个字符
	string s_e = email.substr(0, s);
	cout << "s_e=" << s_e << endl;
}


int main()
{
	test01();
	test02();




	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号