ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-10 11:00:46  阅读:135  来源: 互联网

标签:const string STL pos char int 字串 字符串


一.string的特性

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

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

二.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个字符串

标签:const,string,STL,pos,char,int,字串,字符串
来源: https://blog.csdn.net/weixin_44190648/article/details/122368467

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有