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

C++ STL-string

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

C++ STL-string

C++ string

文章目录
  • C++ string
  • 前言
  • 一、string 定义
  • 二、string 常用函数
    • 1. +=: 字符串拼接
    • 2.比较大小
    • 3.insert(): 插入字符串
      • insert(a, string):在 a 位置插入 string 字符串
      • insert(a, it_start, it_end):在 a 位置插入以 it_start 到 it_end 的字符串(均为迭代器)
    • 4.erase(): 删除元素
      • erase(it):删除单个元素,it 为元素迭代器
      • erase(it_start, it_end):删除迭代器 it_start 到 it_end 区间
      • erase(a, length): 删除从 a 开始长度为 length 的字符串
    • 5.clear():清空元素
      • 5.substr(a, length): 返回从 a 开始 长度为 length 的字符串
    • 6.find():匹配字符串
      • find(Test_Mystr) 如果 Test_Mystr 为调用者的子串,返回第一次出现位置,否则返回 string::npos
      • find(Test_Mystr, a): 从 a 位置开始匹配 Test_Mystr 并返回第一次出现位置或者 string::npos
    • 7.replace(): 替换子串
      • replace(a, length, Test_Mystr): 从调用者的 a 位置开始将长度为 length 的字符串替换为 Test_Mystr
      • replace(it_start, it_end, Test_Mystr): 将迭代器 it_start 到 it_end 区间字符串替换为 Test_Mystr


前言

为了使编程者可以更方便地对字符串进行操作,C++在 STL 中加入了 strng 类型。

一、string 定义

如果要使用 string 需要添加头文件

#include 

定义 string 与基本数据相同

string Mystr;

一般情况下, string 可以直接像字符数组一样去访问。

#include 
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "abcde";

	cout << Mystr[2] << endl;

	cout << Mystr << endl; //如果要 读入 或者 输出 整个字符串时 一般用 cin 与 cout
	printf("%sn", Mystr.c_str()); // 使用 printf 的话 得使用 c_str() 转化为字符数组
								   // 使用 c_str() 得引入头文件 #include 

	return 0;
}

结果如图:

需要使用某些函数如 insert() 或者 erase() 时,string 也可以通过迭代器访问

#include 
#include 

using namespace std;

int main()
{
	string Mystr = "qwerty";
	
	string::iterator a = Mystr.begin();  //用迭代器 a 保存 Mystr 的首地址
	string::iterator b = Mystr.end();    //用迭代器 b 保存 Mystr 末位地址的下一个地址

	for(a += 2; a != b; a ++)  // string 和 vector 一样支持迭代器加减数字获取地址
	{
		cout << *a << endl;
	}
	
	return 0;
}

结果如图:

二、string 常用函数 1. +=: 字符串拼接

string 中的 += 运算符可以直接将字符串拼接起来
代码如下(示例):

#include 
#include 

using namespace std;

int main()
{
	string Mystr_One = "qwe";
	string Mystr_Two = "asd";
	
	string Mystr_Test = Mystr_One + Mystr_Two; //用新字符串保存拼接结果
	
	Mystr_One += Mystr_Two;//在原字符串上拼接,注意拼接顺序。

	cout << Mystr_Test << endl;
	
	cout << Mystr_One << endl;
	
	return 0;
}

结果如下:

2.比较大小

两个 string 类型可以直接使用 ==、!=、>、>=、<、<=来比较大小,比较规则是字典序

代码如下(示例):

#include 
#include 

using namespace std;

int main()
{
	string Mystr_One = "abc";
	string Mystr_Two = "def";
	string Mystr_Three = "aaa";
	string Mystr_Four = "aa";
	
	if(Mystr_One != Mystr_Two) cout << "T" << endl; //字典序 abc < def 所以应该为 T
	else cout << "F" << endl;
	
	if(Mystr_One > Mystr_Three) cout << "TT" << endl; //字典序 abc > aaa 所以应该为 TT
	else cout << "FF" << endl;

	if(Mystr_Three < Mystr_Four) cout << "TTT" << endl; //字典序 aaa > aa 所以应该为 FFF
	else cout << "FFF" << endl;
	
	return 0;
	
}

结果如下:

3.insert(): 插入字符串

时间复杂度为 O(N);

insert(a, string):在 a 位置插入 string 字符串

代码如下(示例):

#include 
#include 

using namespace std;

int main()
{
	string Mystr = "abcdef";
	string TestMystr = "zzz";

	Mystr.insert(3, TestMystr);

	cout << Mystr << endl;
	
	return 0;
}

结果如下:

insert(a, it_start, it_end):在 a 位置插入以 it_start 到 it_end 的字符串(均为迭代器)
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "qweasd";
	string TestMystr = "kkkk";
	string::iterator a = Mystr.begin() + 2;
	string::iterator it_start = TestMystr.begin();
	string::iterator it_end = TestMystr.end();

	Mystr.insert(3, it_start, it_end);

	cout << Mystr << endl;

	return 0;
	
}

结果如下:

4.erase(): 删除元素 erase(it):删除单个元素,it 为元素迭代器
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "abcde";
	string::iterator it = Mystr.begin() + 2;
	
	Mystr.erase(it);

	cout << Mystr << endl;

	return 0;
}

结果如下:

erase(it_start, it_end):删除迭代器 it_start 到 it_end 区间
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "abcder";
	string::iterator it_start = Mystr.begin();
	string::iterator it_end = Mystr.end();
	
	Mystr.erase(it_start + 1, it_end - 1);  //删除区间为 [b, r), 实际删除元素为 b,c,d,e
	
	cout << Mystr << endl;

	return 0;

}

结果如下:

erase(a, length): 删除从 a 开始长度为 length 的字符串
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "qweasdzxc";
	
	Mystr.erase(3, 5);

	cout << Mystr << endl;

	return 0;
}

结果如下:

5.clear():清空元素
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "qwerty";
    cout << Mystr.length() << endl;
    
	Mystr.clear();

	if(Mystr.size() == 0) cout << "T" << endl;   // size() 与 length() 都可以获取 string 的长度。 时间复杂度为 O(1)
	else cout << "F" << endl;

	return 0;
}

结果如下:

5.substr(a, length): 返回从 a 开始 长度为 length 的字符串

时间复杂度为 O(len)

#include 
#include 

using namespace std;

int main()
{
	string Mystr = "abcdefg";

	cout << Mystr.substr(2, 3) << endl;

	return 0;
}

结果如下:

6.find():匹配字符串 find(Test_Mystr) 如果 Test_Mystr 为调用者的子串,返回第一次出现位置,否则返回 string::npos

string::npos 是一个常数,其本身值为 “-1” 但由于是 unsigned_int 类型,所以也可以认为是 unsigned_int 的最大值

#include 
#include 

using namespace std;

int main()
{
	string Mystr = "abcdef";
	string Test_Mystr_One = "def";
	string Test_Mystr_Two = "ope";
	
	cout << Mystr.find(Test_Mystr_One) << endl;

	cout << Mystr.finf(Test_Mystr_Two) << endl;
	
	return 0;
	
}

结果如下:

find(Test_Mystr, a): 从 a 位置开始匹配 Test_Mystr 并返回第一次出现位置或者 string::npos
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "This is a test!";
	string Test_Mystr = "is";

	cout << Mystr.find(Test_Mystr, 0) << endl;
	cout << Mystr.find(Test_Mystr, 4) << endl;
	cout << Mystr.find(Test_Mystr, 9) << endl;

	return 0;
}

结果如下:

7.replace(): 替换子串 replace(a, length, Test_Mystr): 从调用者的 a 位置开始将长度为 length 的字符串替换为 Test_Mystr
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "Hello!!!World!!";
	
	string Test_Mystr = "   ";
	
	cout << Mystr.replace(5, 3, Test_Mystr) << endl;

	return 0;	
}

结果如下:

replace(it_start, it_end, Test_Mystr): 将迭代器 it_start 到 it_end 区间字符串替换为 Test_Mystr
#include 
#include 

using namespace std;

int main()
{
	string Mystr = "aaaaaaa";
	string Test_Mystr = "bbb"
	string::iterator it_start = Mystr.begin() + 1;
	string::iterator it_end = Mystr.end() - 1;

	cout << Mystr.replace(it_start, it_end, Test_Mystr) << endl;

	return 0;
}

结果如下:

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

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

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