string用法-学习笔记

string类型,对字符串常用的需求功能进行了封装。
头文件:#include < string > using namespace std;
<string.h>=< cstring >是c的头文件,c语言标准库函数,是c处理c字符串的库函数
< string >是c++字符串类头文件

string的定义

与基本数据类型相同:

string str;
//初始化
string str='abcd';
string中内容的访问
  • 通过下标访问
    可以直接像字符数组那样去访问string:
#include <stdio.h>
#include <string>
using namespace std;
int main(){
	string str="abcd";	//此处是双引号
	for(int i=0;i<str.length();i++){
		printf("%c",str[i]);
	}
	return 0; 
} 

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

#include <stdio.h>
#include <iostream> 	//cin和cout在这个头文件中 
#include <string>
using namespace std;
int main(){
	string str;
	cin>>str;
	cout<<str;
	return 0;
} 

如果非要用pringf来输出string,可以用c_str()将string类型转换为字符数组进行输出:

#include <stdio.h>
#include <iostream> 	//cin和cout在这个头文件中 
#include <string>
using namespace std;
int main(){
	string str="abcd";
	printf("%s\n",str.c_str());	//将string型str用c_str()转为字符数组 
	//cout<<str;
	return 0;
} 
  • 通过迭代器访问
    可以直接定义:
string::iterator it;

通过*it来访问string里的每一位:

#include <stdio.h>
#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;
} 

string和vector一样,支持直接对迭代器进行加减某个数字,如:str.begin()+3

string常用函数实例

operator+=

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

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1="abc",str2="xyz",str3;
	str3=str1+str2;	//字符串拼接
	str1+=str2;		//将str2直接拼接到str1上 
	cout<<str1<<endl;		//endl---换行
	cout<<str3<<endl; 
	return 0;
} 
compare operator

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

#include <stdio.h>
#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;
} 
length()/size()

返回string的长度

string str="abcxyz";
printf("%d %d\n",str.length(),str.size());
insert()
  1. insert(pos,string),在pos号位置插入字符串string。
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1="inset",str2="r",str3="abc",str4="xyz";
	cout<<str1<<endl;
	str1.insert(4,str2);
	cout<<str1<<endl;
	return 0;
} 
  1. insert(it,it2,it3),it为原字符串的欲插入位置,it2和it3为待插字符串的首位迭代器,用来表示串[it2,it3)将被插在it位置上。
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1="inset",str2="rrrr",str3="abc",str4="xyz";
	str1.insert(str1.begin()+4,str2.begin(),str2.end());
	cout<<str1<<endl;
	return 0;
} 
erase()
  1. 删除单个元素
    str.erase(it)
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str1="inset",str2="rr";
	str1.erase(str1.begin()+1);
	cout<<str1<<endl;
	return 0;
} 
  1. 删除区间内的所有元素

(1).str.erase(first,last)

str.erase(str.begin()+2,str.end()-1);

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

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

清空string中的数据

substr()

substr(pos,len)返回从pos号位开始、长度为len的子串。

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str="Thank you for your watching";
	cout<<str.substr(0,5)<<endl;
	cout<<str.substr(6,3)<<endl;
	
	cout<<str<<endl; 
	return 0;
} 
string::npos

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

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	if(string::npos==-1){
		cout<<"-1 is true."<<endl;
	}
	if(string::npos==4294967295){
		cout<<"4294967295 is also true."<<endl; 
	}
	return 0;
} 
find()

str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置;如果str2不是str的子串,返回string::npos。
str.find(str2,pos),从str的pos号位开始匹配str2,返回值与上相同。

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str="thank you for your watching.";
	string str1="you";
	string str2="me";
	if(str.find(str1)!=string::npos){	//str1是str的子串时继续 
		cout<<str.find(str1)<<endl;
	}
	if(str.find(str1,7)!=string::npos){
		cout<<str.find(str1,7)<<endl;
	}
	if(str.find(str2)!=string::npos){
		cout<<str.find(str2)<<endl; 
	}else{
		cout<<"i know there is no position for me."<<endl;
	}
	return 0;
} 
replace()

str.replace(pos,len,str2)把str从pos号位开始、长度为len的子串替换为str2。
str.replace(it1,it2,str2)把str的迭代器[it1,it2)范围的子串替换为str2。

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str="thank you for your watching.";
	string str2="thanks";
	string str3="reading";
	string str4=str.replace(0,9,str2);
	cout<<str4<<endl;	//把str从0号位开始的9个字符替换为str2
	cout<<str.replace(str.end()-9,str.end()-1,str3)<<endl; 
	return 0;
} 

【patA1060 are they equal】

给出两个数,问它们写成保留n位小数的科学计数法后是否相等。如果相等,输出yes,并给出该转换结果;如果不相等,则输出no,并分别给出两个数的转换结果。

#include <iostream>
#include <string>
using namespace std;
int n;	//有效位数
string deal(string s,int& e){ 	//数s,指数e 
	int k=0;	//s的下标
	while(s.length()>0&&s[0]=='0'){
		s.erase(s.begin());	//去掉s的前导0 
	}
	//去掉前导0后是小数点,说明s是小于1的小数 
	if(s[0]=='.'){
		s.erase(s.begin());	//去掉小数点
		while(s.length()>0&&s[0]=='0'){
			s.erase(s.begin());	//去掉小数点后非零位前的所有零
			e--; 		//每去掉一个0,指数-1 
		} 
	}else{	//去掉前导0后不是小数点,则找到后面的小数点删除 
		while(k<s.length()&&s[k]!='.'){	//k从0开始 
			k++;
			e++;
		}
		
	}
	
	//如果去掉前导0后s长度变为0,说明这个数是0 
	if(s.length()==0){
			e=0;
		}
		int num=0;
		k=0;
		string res;
		while(num<n){	//只要精度还没到n 
			if(k<s.length()) res+=s[k++];	//只要还有数字,就加到res末尾
			else res+='0';	//否则res末尾添加0
			num++;		//精度+1 
		}
		return res;
} 
int main(){
	string s1,s2,s3,s4;
	cin>>n>>s1>>s2;
	int e1=0,e2=0;
	s3=deal(s1,e1);
	s4=deal(s2,e2);
	if(s3==s4&&e1==e2){
		cout<<"YES 0."<<s3<<"*10^"<<e1<<endl;
	}else{
		cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好的问题!让我来解答。 首先,让我们来了解一下 `Comparable` 接口。这个接口是 Java 中的一个接口,它只有一个方法 `compareTo()`。这个方法的作用是定义类的自然排序顺序。 如果我们要对一个对象集合进行排序,只需要让这个类实现 `Comparable` 接口,并且重写 `compareTo()` 方法。这个方法返回一个整型值,表示该对象与另一个对象的大小关系。如果该对象小于另一个对象,则返回负整数;如果两个对象相等,则返回零;如果该对象大于另一个对象,则返回正整数。 下面是一个简单的例子,展示如何使用 `Comparable` 接口进行排序。 ```java public class Student implements Comparable<Student> { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } // 重写 compareTo 方法 @Override public int compareTo(Student o) { // 按照年龄从小到大排序 return this.age - o.getAge(); } } ``` 在这个例子中,我们定义了一个 `Student` 类,并且让它实现了 `Comparable` 接口。我们重写了 `compareTo()` 方法,按照年龄从小到大排序。 现在,我们可以使用 `Collections.sort()` 方法对 `Student` 对象进行排序了: ```java List<Student> students = new ArrayList<>(); students.add(new Student("Tom", 20)); students.add(new Student("Jerry", 18)); students.add(new Student("Alice", 22)); Collections.sort(students); for (Student student : students) { System.out.println(student.getName() + " " + student.getAge()); } ``` 输出结果为: ``` Jerry 18 Tom 20 Alice 22 ``` 可以看到,我们成功地按照年龄从小到大对 `Student` 对象进行了排序。 希望这个例子能够帮助你理解如何使用 `Comparable` 接口进行排序。如果你还有其他问题,请随时提出!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值