c++/oop---string类

string 用法

string 变量名

string city  = "Beijing"

一个 string 对象的大小是固定的,与字符串的长短无关,只保存地址和一些其他信息。

string  as[] = {"asd","dfg","ertw"};

cout<<as[1]<<endl;

string 对象可以相互赋值,还可以相加(连接)

查看代码
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string> 
using namespace std;
int main(){
	string a,b;
	while(1){
		cin>>a>>b;
        cout<<a+b<<endl;
		if(a==b)cout<<"=="<<endl;
		if(a>b)cout<<">"<<endl;
		if(a<b)cout<<"<"<<endl;
	}
	return 0;
}

 

比较的依据是字典序

 

其他的函数

S.substr(l,r) 下标为 l,r 的子串

strcpy (a,b) 这是个 C 语言中的函数,用来复制字符串

用法 strcpy (str,s.c_str()) 

将其转换为 C 中的字符串

 

 

 

 

-----------------------------分割线---------------------------------

string 类有多个构造函数;

string s1;string st();//s1="";

string s2("hello");string s2="hello" //s2="hello"

string s3(4,'K');//"KKKK"

可以string s;s='n'(可赋值)

但不可以string s='n'(不可初始化)

string 支持流插入运算符 cin >> s;
string 支持流提取运算符 cout << s;
string 支持 getline 函数 getline(cin ,s); 

https://zh.cppreference.com/w/cpp/string/basic_string/basic_string

 

find():string s1("hello world");s1.find("lo");
在 s1 中从前向后查找“lo”第一次出现的地方,如果找到,返回“lo”
开始的位置,即 l 所在的位置下标。如果找不到,返回 string::npos
(string 中定义的静态常量)
s1.find("ll", 2) //从下标2开始向后查找

 


rfind():string s1("hello world");s1.rfind("lo");
在 s1 中从后向前查找“lo”第一次出现的地方,如果找到,返回“lo”
开始的位置,即 l 所在的位置下标。如果找不到,返回 string::npos 。
s1.rfind("ll", 4) 从下标4开始反向查找

 

最后贴一道题:

描述

给定n个字符串(从1开始编号),每个字符串中的字符位置从0开始编号,长度为1-500,现有如下若干操作:

  • copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串。
  • add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串。
  • find S N:在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
  • rfind S N:在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
  • insert S N X:在第N个字符串的第X个字符位置中插入S字符串。
  • reset S N:将第N个字符串变为S。
  • print N:打印输出第N个字符串。
  • printall:打印输出所有字符串。
  • over:结束操作。

其中N,X,L可由find与rfind操作表达式构成,S,S1,S2可由copy与add操作表达式构成。

输入

第一行为一个整数n(n在1-20之间)

 

接下来n行为n个字符串,字符串不包含空格及操作命令等。

 

接下来若干行为一系列操作,直到over结束。

输出

根据操作提示输出对应字符串。

样例输入

3
329strjvc
Opadfk48
Ifjoqwoqejr
insert copy 1 find 2 1 2 2 2
print 2
reset add copy 1 find 3 1 3 copy 2 find 2 2 2 3
print 3
insert a 3 2
printall
over

样例输出

Op29adfk48
358
329strjvc
Op29adfk48
35a8

提示

推荐使用string类中的相关操作函数。

 

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string> 
using namespace std;
int n;
string s[22];
string op;
int find();
int rfind();
string copy();
string add();
int getint();
string getstring();

bool digital(string s){
	int L=s.length();
	for(int i=0;i<L;i++)if(!isdigit(s[i]))return 0;
	return 1;
}
int getint(){
	string s;
	cin>>s;
	if(digital(s))return atof(s.c_str());
	if(s=="find"){
		return find();
	}
	if(s=="rfind"){
		return rfind();
	}
}
string getstring(){
	string s;
	cin>>s;
	if(s=="copy")return copy();
	if(s=="add")return add();
	return s;
}
int find(){
	string ss=getstring();
	int N=getint();
	int x=s[N].find(ss.c_str());
	return x==string::npos?s[N].length():x;
}
int rfind(){
	string ss=getstring();
	int N=getint();
	int x=s[N].rfind(ss.c_str());
	return x==string::npos?s[N].length():x;
}
string copy(){
	int N,l,x;
	N=getint();x=getint();l=getint();
	char *T = new char [l+1];
	s[N].copy(T,l,x);
	T[l]='\0';
	string tt=T;delete [] T;
	return tt;
}
string add(){
	string s1=getstring(),s2=getstring();
	if(digital(s1)&&s1.length()<=5&&digital(s2)&&s1.length()<=5){
		int v1=atoi(s1.c_str()),v2=atoi(s2.c_str());
		char T[10];int v=v1+v2;
		int cnt=0; 
		while(v)T[cnt++]=v%10+'0',v/=10;
		T[cnt]='\0';
		for(int i=0,j=cnt-1;i<j;i++,j--)swap(T[i],T[j]);
		return T; 
	}
	else return s1+s2; 
	
}
int main(){
//	freopen("ans.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>s[i];
	while(1){
		cin>>op;
		if(op=="copy"){
			cout<<copy()<<endl;
		}
		if(op=="add"){
			cout<<add()<<endl;
		}
		if(op=="find"){
			cout<<find()<<endl;
		}
		if(op=="rfind"){
			cout<<rfind()<<endl;
		}
		if(op=="reset"){
			string ch=getstring();
			int N=getint();
			s[N]=ch;
		}
		if(op=="insert"){
			string ch=getstring();
			int N=getint(),x=getint();
			s[N].insert(x,ch);
//			cout<<s[N]<<endl;
		}
		if(op=="print"){
			int N=getint();
			cout<<s[N]<<endl; 
		}
		if(op=="printall"){
			for(int i=1;i<=n;i++)cout<<s[i]<<endl;
		}
		if(op=="over")break;
		
	}
	return 0;
}
/*

3
329strjvc
Opadfk48
Ifjoqwoqejr
insert copy 1 find 2 1 2 2 2
print 2
reset add copy 1 find 3 1 3 copy 2 find 2 2 2 3
print 3
insert a 3 2
printall
over


insert copy 1 find 2 1 2 2 2
find 2 1 = 1

insert copy 1 1 2 2 2
copy 1 1 2 = "29"
*/

 

注意 copy 以后应该手动加 '\0'

posted @ 2022-03-13 22:28  liankewei123456  阅读(106)  评论(0编辑  收藏  举报