string 的常见用法详解

目录

1.string 的定义

2.string 中内容的访问

3.string 常用函数实例解析


    在C语言中,一般使用字符数组 char str[]来存放字符串,但是使用字符串数组有时会显得操作麻烦,在C++中加入了string类型。

    注意,如果要使用string,需要添加 string头文件,即# include<string>  (注意 string. h 和 string 是不一样的头文件)。除此之外,还需要在头文件下面加上一句:“ using namespace std"。

即:

#include<string>

using namespace std;

1.string 的定义

定义和初始化和普通的数据类型一样:

string str;//定义变量
string str="abcd";//初始化

2.string 中内容的访问

(1)通过下标访问

一般来说,可以直接像字符数组那样去访问string。

程序代码:

#include<bits/stdc++.h>//万能头文件,里面包含头文件<string> 
#include<string>
using namespace std;
int main(){
	string str = "hello";
	for(int i=0;i<str.length();i++){
		printf("%c ",str[i]);
	}
	return 0;
}

运行结果:

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

程序代码:

//#include<bits/stdc++.h>
//万能头文件,里面包含头文件<string> 和 <iostream>
#include<iostream>//cin和cout在iostream头文件中,而不是stdio.h 
#include<string>
using namespace std;
int main(){
	string str;
	cin>>str;
	cout<<str<<endl;
	return 0;
}

运行结果:

(2)通过迭代器访问

一般仅通过(1)就可以满足访问的要求,但是有些函数比如insert()和erase()则要求以迭代器为参数,所以还是学一下迭代器的用法。

  定义string迭代器

string::iterator it;

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

程序代码:

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

运行结果:

3.string 常用函数实例解析

(1)operator+=

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

示例如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str1= "abcd",str2="xyz",str3;
	str3=str1+str2;//将str1和str2拼接,赋值给str3
	str1+=str2;//将str2直接拼接到str1上
	cout<<str1<<endl;
	cout<<str3<<endl;
	return 0;
}

输出结果:

(2) length()/size()

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

示例如下:

string str="abcxyz";

printf("%d %d\n"),str.length,str.size());

输出结果:

6 6

(3) insert()

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

示例如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "nihaoshijie"; 
	str.insert(2, "**");
	cout<<str<<endl;
	return 0;
}

输出结果:

(4) erase()

  • 删除单个元素

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

示例如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(str.begin()+4);//删除4号位(即e)
	cout<<str<<endl; 
	return 0;
}

输出结果:

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

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

示例如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(3,2);//删除从3号位开始的2个字符,即de 
	cout<<str<<endl; 
	return 0;
}

输出结果:

(5) clear()

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

代码示例:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "youdianlei"; 
	str.clear();	//清空字符串 
	cout<<str.length()<<endl;
	return 0;
}

输出结果:

(6) string::npos

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

(7) find()

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

         str.find(str2,pos),从str的pos号位开始匹配str2,返回值与上相同。时间复杂度为O(nm),其中n和m分别为str和str2的长度。

示例如下:

输出结果:

 声明:本文内容摘自胡凡、曾磊老师主编的《算法笔记》书本内容。

猜你喜欢

转载自blog.csdn.net/weixin_51472673/article/details/123233961