STL:string容器特性、定义、初始化、等号、取值、拼接、查找、替换、比较、字串、插入、删除

一.string的特性

string和char*类型字符串的对比:

  • char是一个指针,string是一个类,string封装了char,管理这个字符串,是一个char*型的容器。
  • string封装了很多实用的成员方法,查找find,拷贝copy,删除delete,替换replace,插入insert
  • 不用考虑内存的释放和越界,string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
    String输入,遇到空格就会停止
	string str1;
	cin >> str1;
	cout << str1;

在这里插入图片描述

二.string定义、初始化和assign函数

//初始化,assign成员函数
void test01() {
	string s1;//调用无参构造
	string s2(10, 'a');//10个a
	string s3("abcdefg");
	string s4(s3);// 调用拷贝构造
	cout << s1 << endl;
	s1.assign("hij");//string类提供成员方法赋值
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
}

在这里插入图片描述

三.等号操作符操作

string s1;
string s2("abcd");
s1 = "efg";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1 = 'a';
cout << s1 << endl;

在这里插入图片描述

四.取值操作—[ ]操作符重载、at函数

string s1 = "abcdefg";
//重载[]号
for (int i = 0; i < s1.size(); i++) {
	cout << s1[i] << " ";
}
cout << endl;
//at函数
for (int i = 0; i < s1.size(); i++) {
	cout << s1.at(i) << " ";
}
cout << endl;

结果:
在这里插入图片描述
区别:

  • [ ]方式:如果是访问越界,直接挂掉。
  • at方式:访问越界,抛出out_of_range异常。
    测试:
try {
	cout << s1[100] << endl;
}
catch (...) {
	cout << "越界!" << endl;
}

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

try {
	cout << s1.at(100) << endl;
}
catch (...) {
	cout << "越界!" << endl;
}

在这里插入图片描述

五.拼接操作

看完就懂,很简单!

string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char* s);//把字符串s连接到当前字符串结尾
string& append(const char* s, int n);//把字符串前n个字符连接到当前字符串结尾
string& append(const string & s);//同operator+=()
string& append(const string& s, int pos, int n);//把字符串s中从pos位置开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c

六.查找和替换

看完就懂,很简单!

//查找第一次出现位置
	int find(const string & str, int pos = 0)const;//查找str第一次出现位置,从pos开始查找
	int find(const char* s, int pos = 0) const;//查找str第一次出现位置,从pos开始查找
	int find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第一次位置
	int find(const char c, int pos = 0) const;//查找字符c第一次出现位置
//查找最后一次出现位置
	int rfind(const string & str, int pos = npos)const;//查找str最后一次出现位置,从pos开始查找
	int rfind(const char* s, int pos = npos) const;//查找str最后一次出现位置,从pos开始查找
	int rfind(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符最后一次位置
	int rfind(const char c, int pos = 0) const;//查找字符c最后一次出现位置
//替换
	string& replace(int pos, int n, const string & str);//替换从pos位置开始n个字符为字符串str
	string& replace(int pos, int n, const char* s);//替换从pos位置开始n个字符为字符串s

七.比较操作

  • compare函数在>时,返回1,<时返回-1,=时返回0;
  • 比较区分大小写,比较时参考字典顺序,排序前面的越小;
  • 大写A比小写a小;
int compare(const string & s)const;//与字符串s比较
int comapre(const char* s)const;//与字符串s比较

八.获取字串

string substr(int pos = 0; int n = npos)const;//返回由pos开始的n个字符组成的字符串

九.插入

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

十.删除

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

十一.其他类型转string

需要添加:

#include <string>

类型转换:

string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值