当前位置:主页 > 查看内容

C++string容器详解

发布时间:2021-06-16 00:00| 位朋友查看

简介:文章目录 1.概述 2.string的构造函数 3.string的赋值操作 4.string的拼接操作 5.string的子串获取操作 6.string的插入与删除 7.string的替换与查找字符操作 8.string的字符串比较操作与尾插操作 9.string字符存取操作 1.概述 string是C风格的字符串而string……

1.概述:

string是C++风格的字符串,而string本质上是一个
string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,不过二者之间也有差别
1.char*是一个指针
2.string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器

2.string的构造函数

构造函数原型:

1.string(); //创建一个空的字符串 例如: string str;

2.string(const char* s); //使用字符串s初始化

3.string(const string& str); //使用一个string对象初始化另一个string对象

4.string(int n, char c); //使用n个字符c初始化

示例:

#include<iostream>
#include<string>
using namespace std;
void test()
{
	//创建空字符串
	string s1;//默认构造
	//使用字符串s初始化
	const char*s = "abc";
	string s2(s);
	cout << "s2=" << s2<<endl;
	//拷贝构造
	string s3(s2);
	cout << "s3=" << s3<<endl;
	//用n个某字符来初始化
	string s4(5, 'a');
	cout << "s4=" <<s4<<endl;

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

3.string的赋值操作

赋值的函数原型:

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

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

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

示例:

#include<iostream>
#include<string>
using namespace std;
//字符串赋值
void test()
{
	string s1;//用的较多
	s1 = "abcd";
	cout <<"s1="<< s1 << endl;
	string s2 = s1;
	cout << "s2="<<s2 << endl;
	string s3;
	s3= 'a';
	cout << "s3=" << s3 << endl;
	string s4;
	s4.assign("hello c++");
	cout << "s4 = " << s4 << endl;
	string s5;
	s5.assign("hello world");
	cout << "s4=" << s5 << endl;
	string s6;
	s6.assign(s5);
	cout << "s6=" << s6 << endl;
	string s7;
	s7.assign(10,'a');
	cout << "s7=" << s7 << endl;

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

4.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 char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾

6.string& append(const string &s); //同operator+=(const string& str)

7.string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

示例:

#include<iostream>
#include<string>
using namespace std;
//string字符串的拼接
void test()
{
	string s1 = "我";
    s1 += "爱玩游戏";
	cout << "s1=" << s1 << endl;
	s1 += ':';
	cout << "s1=" << s1 << endl;
	string s2="LOL";
	s1 += s2;
	cout << "s1=" << s1 << endl;
	string s3 = "I ";
	s3.append("love ");
	cout << "s3=" << s3 << endl;
	s3.append("you LOL", 1);
	cout << "s3=" << s3 << endl;
	string s4 = "ou";
	s3.append(s4);
	cout << "s3=" << s3 << endl;
	s3.append(s3, 7, 3);
	cout << "s3=" << s3 << endl;

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

运行结果:
在这里插入图片描述

5.string的子串获取操作

s.substr(pos,n)
返回一个string,包含s中从pos开始的n个字符的拷贝,pos的默认值为0

示例:

#include<iostream>
#include<string>
using namespace std;
//子串的获取
void test()
{
	string s1 = "abcd";
	string substr = s1.substr(1, 3);//从s1的位置1开始获取3个字符
	cout << substr << endl;
	string s2 = s1.substr(1, 5);//开始位置加计数值超过string的大小,substr会调整计数值,只拷贝到string的末尾,不会报错
	//string s = s1.substr(5, 3);
    //开始位置超过了string的大小,substr函数抛出一个out_of_range 异常	
	cout << s2 << endl;
}
//实用操作
void test2()
{
	string email = "zhangsan@qq.com";
	int p = email.find('@');
	string s2 = email.substr(0, p);
	cout << s2 << endl;

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

运行结果:
在这里插入图片描述

6.string的插入与删除

插入:s.insert(pos,args)
在pos前插入args指定的字符

删除:s.erase(pos,len)
删除从位置pos开始的len个字符,如果省略len,则删除从pos开始直至s末尾的所有字符

示例:

#include<iostream>
#include<string>
using namespace std;
//字符串的插入与删除
void test()
{
	string s1 = "hel world";
	//插入
	string s2 = "hello";
	s1.insert(3, "lo");//插入起始位置,插入内容
	cout << s1 << endl;
	//在s1[0]之前插入s2中从s2[0]开始的s2.size()个字符
	s1.insert(0, s2, 0, s2.size());
	cout << s1 << endl;
	//删除
	s1.erase(0, 6);//删除起始位置,删除多少个
	cout << s1<<endl;
}

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

运行结果:
在这里插入图片描述

7.string的替换与查找字符操作

查找:s.find(args)或s.rfind(args)

s.find为从左向右查找,查找第一次args出现的位置
s.rfind为从右向左查找,查找最后一次args出现的位置

注意:查找返回的是一个unsigned int类型,用int接收并不明智

替换:s.replace(range,args)

删除s中范围range内的字符,替换为args指定的字符
replace操作是调用erase和insert的简化形式

示例:

#include<iostream>
#include<string>
using namespace std;
//字符串的查找
void test()
{
	string s1 = "abcdefgde";
	auto find = s1.find("de");//默认从0开始查找 返回整形
	if (find == -1)
	{
		cout << "没找到字符串" << endl;//没找到子串返回-1
	}
	else
	{
		cout << "find=" << find << endl;
	}
	//如果我们要搜索第一个不在参数中的字符,我们应该调用find_first_not_of
	string S = "asdasfgvc23";
	string S1 = "ads01234";
	auto pos = S.find_first_not_of(S1);//返回5(f)
	cout << pos << endl;
	//rfind从右往左查找 find 从左往右查找
	auto rfind = s1.rfind("de");
	cout << "rfind=" << rfind << endl;
}
//替换
void test2()
{
	string s1 = "123456789";
	//从第一个位置(2)替换3个字符(234)为"1111"
	s1.replace(1, 3, "1111");
	//等价于s1.erase(1,3);
	//s1.insert(1,3,"1111");
	cout << "替换后:" << s1 << endl;
}
int main()
{
	test();
	test2();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

8.string的字符串比较操作与尾插操作

字符串比较:s.compare(s1)
s与s1相等返回0,s>s1返回大于0的值,s<s1返回一个小于0的值

尾插:s.append(args)
将args追加到s,返回一个指向s的引用

示例:

#include<iostream>
#include<string>
using namespace std;
//字符串比较
//与strcmp类似
void test()
{
	//ASCII码逐个对比
	string s1 = "hello world";
	string s2 = "hello world";
	if (s1.compare(s2) == 0)
	{
		cout << "s1=s2" << endl;
	}
	else if (s1.compare(s2) > 0)
	{
		cout << "s1>s2" << endl;
	}
	else
	{
		cout << "s1<s2" << endl;
	}

}
//字符串尾插
void test2()
{
	string s = "hello ";
	string s1 = s;
	s.append("world");
	cout << s << endl;
	//等价于
	s1.insert(s1.size(), "world");
	cout << s1 << endl;
}
int main()
{
	test();
	test2();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

9.string字符存取操作

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

1.char& operator[](int n); //通过[]方式取字符
2.char& at(int n); //通过at方法获取字符

示例:

#include<iostream>
#include<string>
using namespace std;
void test()
{
	int i = 0;
	string s1 = "abcd";
	//用[]访问单个字符
	for (i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	//用at访问
	for (i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i) << " ";
	}
	cout << endl;
	//修改单个字符
	s1[0] = 'b';
	cout << s1 << endl;
	s1.at(2) = 'b';
	cout << s1 << endl;

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

运行结果:
在这里插入图片描述

;原文链接:https://blog.csdn.net/hbbfvv1h/article/details/115598447
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐