[STL]string常见用法详解

目录

引入

常见用法介绍

1. string的定义

2. string中内容的访问

1)通过下标访问

2)通过迭代器访问

3. string常用函数实例解析

1)operator+=

2)compare operator

3)length()/size()

4)insert()

5)erase()

6)clear()

7)substr()

8)string::npos

9)find()

10)replace


引入

在C语言中,一般使用字符数组char str[]来存放字符串,但是使用字符数组有时会显得操作麻烦,而且容易因为经验不足产生一些错误。为了方便操作,C++在STL中加入了string类型,以方便字符串操作。

使用string前,需要添加头文件#include<string>(注意,string和string.h不一样)。此外还需要加上一句using namespace std;

常见用法介绍

1. string的定义

定义string的方式跟基本数据类型相同,只需要在string后面跟上变量名即可:

string str;
//如果要初始化,可直接给string类型变量赋值
string str = "ayay";

2. string中内容的访问

1)通过下标访问

可以像字符数组那样直接访问string:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcd";
	for(int i = 0; i < str.length(); i++){
		printf("%c", str[i]);
	}
	return 0;
}
/*
result: abcd
*/

如果要读入和输出整个字符串,则只能用cin和cout

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str;
	cin>>str;
	cout<<str;
	return 0;
}
/*
input: ayvay
output: ayvay
输入与输出相同 
*/

那么,怎么用printf来输出string呢?方法是用c_str()将string类型转为字符数组进行输出:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "ayvay";
	printf("%s\n", str.c_str());
	//将string型str使用c_str()变为字符数组 
	return 0;
}
/*
result: ayvay 
*/

2)通过迭代器访问

一般第一种方法已经可以满足访问的要求,但是有些函数比如insert()和erase()需要以迭代器为参数,所以依然需要学习迭代器用法。

因为string不像vector,set需要参数类型,所以可以直接定义:

string::iterator it;

这样就得到了一个迭代器it,并且可以通过*it访问string里的每一位:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "ayvay";
	for(string::iterator it = str.begin(); it != str.end(); it++)
		printf("%c", *it);
		
	return 0;
}
/*
result: ayvay
*/

另外指出,string和vector一样,支持直接对迭代器进行加减某个数字,如str.begin() + 3的写法是可行的。

3. string常用函数实例解析

1)operator+=

这是string的加法,可以直接将两个string拼接起来

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1 = "I ", str2 = "love ", str3 = "you!";
	string str4;
	str4 = str1 + str2 + str3;
	str1 += str2 + str3;
	cout<<str1<<endl;
	cout<<str4<<endl;
		
	return 0;
}
/*
result: 
I love you!
I love you!
*/

2)compare operator

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

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1 = "aa", str2 = "aaa", str3 = "abc", str4 = "xyz";
	if(str1 < str2) printf("ok1\n");
	if(str1 != str3) printf("ok2\n");
	if(str4 >= str3) printf("ok3\n");	
	return 0;
}
/*
result:
ok1
ok2
ok3
*/

3)length()/size()

返回string的长度,即存放的字符数,时间复杂度为O(1)。size()和length()基本相同。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1 = "hhhhh";
	cout<<str1.length()<<' '<<str1.size()<<endl;
	return 0;
}
/*
result: 5 5
*/

4)insert()

这里给出string的insert()函数几个常见用法,时间复杂度为O(N)。

insert(pos, string),在pos号位置插入字符串string

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1 = "hhhhh", str2 = "www";
	str1.insert(3, str2);
	//往str[3]插入www。这里str2的位置直接写www也可以
	cout<<str1<<endl; 
	return 0;
}
/*
result: hhhwwwhh
*/

insert(it, it2, it3),it为原字符串的欲插入位置,it2和it3为待插字符串的首尾迭代器,用来表示串[it2, it3)将被插在it的位置上。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1 = "hhhhh", str2 = "www";
	str1.insert(str1.begin() + 3, str2.begin(),  str2.end());
	//往str[3]插入www。这里str2的位置直接写www也可以
	cout<<str1<<endl; 
	return 0;
}
/*
result: hhhwwwhh
*/

5)erase()

erase有两种用法:删除单个元素、删除一个区间内的所有元素。时间复杂度均为O(N)。

删除单个元素

str.erase(it)用于删除单个元素,it为需要删除的元素的迭代器。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "ayvay";
	str.erase(str.begin() + 2);
	cout<<str<<endl; 
	return 0;
}
/*
result: ayay
*/

删除一个区间内的所有元素

->string.erase(first, last),其中first为需要删除的区间的起始迭代器,而last则为需要删除的区间的末尾迭代器的下一个地址,也即为删除[first, last)。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(str.begin(), str.end() - 1);
	cout<<str<<endl; 
	return 0;
}
/*
result: g
*/

->str.erase(pos, length),其中pos为需要删除的起始位置,length为删除的字符个数。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(3, 2);
	cout<<str<<endl; 
	return 0;
}
/*
result: abcfg
*/

6)clear()

clear()用以清空string中的数据,时间复杂度一般为O(1)。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcd";
	str.clear();
	cout<<str.length()<<endl;
	return 0;
}
/*
result: 0
*/

7)substr()

substr(pos, len)返回从pos位开始、长度为len的子串,时间复杂度为O(len)。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "Your smile is hard to forget";
	cout << str.substr(0, 4) << endl;
	cout << str.substr(5, 5) << endl;
	
	return 0;
}
/*
result:
Your
smile
*/

8)string::npos

string::npos是一个常数,其本身的值为-1,但由于是unsigned_int类型,因此实际上也可以认为是unsigned_int类型的最大值。string::npos用来作为find()函数失配时的返回值。

#include <iostream>
#include <string>
using namespace std;
int main(){
	if(string::npos == -1)
		cout << "bool" << endl;
		
	return 0;
}
/*
result: bool
*/ 

9)find()

str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置;如果str2不是str的子串,那么返回string::npos。

str.find(str2, pos),从str的pos号位开始匹配str2,返回值与上相同。

时间复杂度为O(nm),其中n和m分别是str和str2的长度。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "Thank you for your smile.";
	string str2 = "you";
	string str3 = "me";
	if(str.find(str2) != string::npos)
		cout << str.find(str2) << endl;
	if(str.find(str2, 7) != string::npos)
		cout << str.find(str2, 7) << endl;
	if(str.find(str3) != string::npos)
		cout << str.find(str3) << endl;
	else
		cout<< "I know there is no position for me." << endl;
	
	return 0;
}
/*
result:
6
14
I know there is no position for me.
*/

10)replace

str.replace(pos, len, str2)把str从pos位开始、长度为len的子串替换成str2。

str.replace(it1, it2, str2)把str的迭代器[it1, it2)范围的子串替换为str2.

时间复杂度为O(str.length())。

示例如下:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "Maybe you will turn around.";
	string str2 = "will not";
	string str3 = "surely";
	cout << str.replace(10, 4, str2) << endl;
	cout << str.replace(str.begin(), str.begin() + 5, str3) << endl;
	return 0;
}
/*
result:
Maybe you will not turn around.
surely you will trun around.
*/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AryCra_07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值