当前位置:   article > 正文

c++ string用法 入门必看 超详细_c++string怎么用

c++string怎么用

1、string的作用

string就是字符串的意思,是c++用来代替char数组的数据结构。里面封装了一些常用的方法,方便我们地对其进行一些操作,而且string的空间大小是动态变化的,大大减小了不必要的花销

2、string常用的输入方法

(1)cin输入,遇到空格停止输入
代码示例:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s1,s2;//定义字符串s1、s2 
	cin>>s1;//输入一个字符串给s1 
	cin>>s2;//输入一个字符串给s2 
	cout<<"s1得到的值是: "<<s1<<endl;
	cout<<"s2得到的值是: "<<s2<<endl;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

输入:

hello world!
  • 1

运行结果:

s1得到的值是: hello
s2得到的值是: world!
  • 1
  • 2

(2)getline输入,遇到换行符停止输入
代码示例:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s1,s2;//定义字符串s1、s2
	getline(cin,s1);//输入一行字符串给s1 
	getline(cin,s2);//输入一行字符串给s2 
	cout<<"s1得到的值是: "<<s1<<endl;
	cout<<"s2得到的值是: "<<s2<<endl;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

输入:

hello world!
happy every day!
  • 1
  • 2

运行结果:

s1得到的值是: hello world!
s2得到的值是: happy every day!
  • 1
  • 2

值得注意的是:
cin输入会自动吃点前置换行和空格,即cin输入得到的不可能是空串。而getline不会吃掉换行符号,可能会使得输入出现意料之外的错误。
示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s1,s2,s3;//定义字符串s1、s2、s3
	cin>>s1; //输入一个字符串给s1 
	getline(cin,s2);//输入一行字符串给s2 
	getline(cin,s3);//输入一行字符串给s3 
	cout<<"s1得到的值是: "<<s1<<endl;
//	判断s2是不是空串 
	if(s2==""){
		cout<<"s2是空串" <<endl; 
	} else{
		cout<<"s2得到的值是: "<<s2<<endl;
	}
	cout<<"s3得到的值是: "<<s3<<endl;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

输入:

hi!
hello world!
  • 1
  • 2

运行结果:

s1得到的值是: hi!
s2是空串
s3得到的值是: hello world!
  • 1
  • 2
  • 3

解决的办法是多写一个getline,或者getchar()来达到吃掉多余的换行符字符:
示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s1,s2,s3;//定义字符串s1、s2、s3
	cin>>s1; //输入一个字符串给s1 
	getchar();//吃掉多余的换行符 
	getline(cin,s2);//输入一行字符串给s2 
	getline(cin,s3);//输入一行字符串给s3 
	cout<<"s1得到的值是: "<<s1<<endl;
//	判断s2是不是空串 
	if(s2==""){
		cout<<"s2是空串" <<endl; 
	} else{
		cout<<"s2得到的值是: "<<s2<<endl;
	}
	cout<<"s3得到的值是: "<<s3<<endl;
} 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

输入:

hi!
hello world!
happy every day!
  • 1
  • 2
  • 3

运行结果:

s1得到的值是: hi!
s2得到的值是: hello world!
s3得到的值是: happy every day!
  • 1
  • 2
  • 3

3、string常用的成员函数

size()//获取字符串长度
insert()//插入字符串
append()//插入字符串
erase()//删除任意字符,时间复杂度O(n),n为字符串长度
substr()//截取字符字串
back()//获取最后一个字符
pop_back()//删除最后一个字符,时间复杂度O(1)
empty()//判断是否为空
clear()//清空字符串
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s="a";//定义一个字符串并初始化 
	
	cout<<"string常用的三种插入方法:"<<endl<<endl;
	cout<<"1、+号" <<endl;
	s+="b"; 
	cout<<"s+=\"b\"后:s="<<s<<endl<<endl; 
	
	cout<<"2、insert()"<<endl;
	s.insert(2,"c");
	cout<<"s.insert(2,\"c\")后:s="<<s<<endl<<endl; 
	
	cout<<"3、append"<<endl; 
	s.append("d");
	cout<<"s.append(\"d\")后:s="<<s<<endl<<endl; 
	
	cout<<"查看字符串大小"<<endl;
	cout<<"s.size()="<<s.size() <<endl<<endl;
	
	cout<<"截取字符串"<<endl; 
	cout<<"s.substr(1,2)="<<s.substr(1,2)<<endl<<endl;//下标1开始截取长度为2的字符串
	
	cout<<"删除特定字符"<<endl;
	s.erase(0,2);
	cout<<"s.erase(0,2)后: s="<<s<<endl<<endl;//从下标0开始删除两个字符<<endl; 
	
	cout<<"删除最后一个字符"<<endl;
	s.pop_back();
	cout<<"s.pop_back()后:s="<<s<<endl<<endl;
	 
	cout<<"两种判断字符串是否为空的方法"<<endl;
	cout<<"1、s.empty()等于1时即为空,等于0时非空"<<endl; 
	cout<<"2、用s==""判断,等于1时即为空,等于0时非空"<<endl; 
	cout<<"s.empty() = "<<s.empty()<<endl;
	cout<<"(s==\"\") = "<<(s=="")<<endl<<endl;
	
	cout<<"两种清空字符串方法"<<endl;
	cout<<"1、s.clear()"<<endl;
	cout<<"2、s=\"\""<<endl<<endl;
	s.clear();
	s=""; 
	
	cout<<"清空后"<<endl; 
	cout<<"s.empty() = "<<s.empty()<<endl;
	cout<<"(s==\"\") = "<<(s=="")<<endl<<endl;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

运行结果

string常用的三种插入方法:

1+号
s+="b"后:s=ab

2insert()
s.insert(2,"c")后:s=abc

3、append
s.append("d")后:s=abcd

查看字符串大小
s.size()=4

截取字符串
s.substr(1,2)=bc

删除特定字符
s.erase(0,2): s=cd

删除最后一个字符
s.pop_back()后:s=c

两种判断字符串是否为空的方法
1、s.empty()等于1时即为空,等于0时非空
2、用s==判断,等于1时即为空,等于0时非空
s.empty() = 0
(s=="") = 0

两种清空字符串方法
1、s.clear()
2、s=""

清空后
s.empty() = 1
(s=="") = 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

4、string的大小比较

string的比较是以逐个字母的ascii码进行比较的
示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s1="abc";
	string s2="acb";
//	先比较a和a,相等时再比较b和c,明显b<c,所以 abc<acb
	if(s1>s2){
		cout<<s1<<">"<<s2<<endl;
	}else if(s1==s2){
		cout<<s1<<"="<<s2<<endl;
	}else{
		cout<<s1<<"<"<<s2<<endl;
	}
	
	s1="bbc";
	s2="bb";
	if(s1>s2){
		cout<<s1<<">"<<s2<<endl;
	}else if(s1==s2){
		cout<<s1<<"="<<s2<<endl;
	}else{
		cout<<s1<<"<"<<s2<<endl;
	}
	
	s1="bbc";
	s2="bbbc";
	if(s1>s2){
		cout<<s1<<">"<<s2<<endl;
	}else if(s1==s2){
		cout<<s1<<"="<<s2<<endl;
	}else{
		cout<<s1<<"<"<<s2<<endl;
	}
	
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

运行结果:

abc<acb
bbc>bb
bbc>bbbc
  • 1
  • 2
  • 3

5、string的遍历方法
(1)下标遍历:通过下标随机访问逐个遍历。
示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s="abcdefg";
	for(int i=0;i<s.size();i++){
		cout<<s[i];
	}
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行结果:

abcdefg
  • 1

如果想逆序遍历呢?只需要逆向思考,从s.size()-1到0遍历即可
示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s="abcdefg";
	for(int i=s.size()-1;i>=0;i--){
		cout<<s[i];
	}
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行结果:

gfedcba
  • 1

(2)foreach遍历
示例代码:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
	string s="abcdefg";
	for(char c:s){
		cout<<c;
	}
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行结果:

abcdefg
  • 1

foreach遍历方法在结构体的遍历中比较常用,因为其简单易写,但是有一个弊端就是不能逆序遍历。

5、string与数字的相互转换

(1)to_string()函数

作用: 将基本类型的值转换为字符串

基本类型包括int、long long、char等等
使用的时候注意超限问题就好了,特殊地,char类型的转换是转换成它的ASCII值,而不是字符

#include<iostream>
using namespace std;
int main(){
	string s;
	int a=111111;
	long long b=222222;
	char c='a';
	s=to_string(a);
	cout<<s<<endl;
	s=to_string(b);
	cout<<s<<endl;
//	将转换成ASCII值97 
	s=to_string(c);
	cout<<s<<endl;
} 
//console:
//111111
//222222
//97
//
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

(2)stoi()函数

作用: 将字符串转换为 int 型

使用的时候千万注意细节,不然很容易出现异常的,字符串转回整型别超出整型范围了,还有说一下转换规则:
1、识别的时候左到右,遇到非法符自动停止,非法符号即非数字和前置正负号,如100b345,转换为100,-100-345转换为-100,超出整型会抛出异常

#include<iostream>
using namespace std;
int main(){
	string s;
	s="100b345";
	cout<<stoi(s)<<endl;
	s="-100-345";
	cout<<stoi(s)<<endl;	
} 
//console:
//100
//-100
//
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

(3)atoi()函数

作用: 和stoi()作用一样,区别是stoi()处理的是string类型,atoi()处理的是const char*类型,而且超过整型范围不会抛出异常

#include<iostream>
using namespace std;
int main(){
	const char *s;
	s="100b345";
	cout<<atoi(s)<<endl;
	s="-100-345";
	cout<<atoi(s)<<endl;	
} 
//console:
//100
//-100
//
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

(4)其它一些类似的函数

stol()//将字符串转为 long 类型
stoll()//将字符串转为 long long 类型
stof()//将字符串转为 float 类型
stod()//将字符串转为 double 类型
stold()//将字符串转为 long double 类型
  • 1
  • 2
  • 3
  • 4
  • 5

是不是很简单呢?

刚接触肯定会觉得难,多些做题多些用,熟悉了就容易了,兄弟萌,加油!!!

文章尚有不足,欢迎大牛们指正

感谢观看,点个赞吧

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/788997
推荐阅读
相关标签
  

闽ICP备14008679号