s1.compare(下标,长度,s2,下标,长度)s1.compare(下标,长度,s1,下标,长度)
using namespace std;
int main()
{
int n,q;
scanf ("%d%d", &n,&q);
string s;
cin>>s;
for ( int i=0; i<q;i++ )
{
int a,b,c,d;
scanf ( "%d%d%d%d" ,&a,&b ,&c , &d );
if ( s.compare( a-1,b-a+1,s,c-1,d-c+1)==0 )
printf ( "YES\n" );
else
printf ( "NO\n" );
}
return 0;
}
多个字符串存储
#include<iostream>
#include<string>
using namespace std;
int main()
{int n,i=0;cin>>n;
string str[n];
while(n--)cin>>str[i++];
}
find 函数
#include<iostream>
#include<string>
using namespace std;
int main()
{ string str1,str2;
while(cin>>str1>>str2)
{auto pos=str1.find(str2);
if(pos!=str1.npos)
cout<<pos;
else cout<<"No"<<endl;
}
}
find函数循环(str2,p)p控制查找开始时的下标,p默认为零
fir
#include<iostream>
#include<string>
using namespace std;
int main()`在这里插入代码片`
{string str1;
while(cin>>str1)
{ int p=0;
while(str1.find(str2,p)!=str1.npos)
{cout<<str1.find(str2,p)<<endl;
p++;}
}
return 0;
}
find函数拓展
string类型的查找操作
s.find(str2) 在s中查找args的第一次出现
s.rfind(args) 在s中查找args的最后一次出现
s.find_first_of(args) 在s中查args的任意字符的第一次出现
s.find_last_of(args) 在s中查找args的任意字符的最后一次出现
s.find_first_not_of(args) 在s中查找第一个不属于args的字符
s.find_last_not_of(args) 在s中查找最后一个不属于args的字符
#include<string>
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
string str1="222";
string str2="3330";string str3;
if(str1<str2)//str1.compare(str2)//
{str3='4';}
cout<<str3<<endl;
/*str1.push_back('2');//只能 push back字符//*/
cout<<str1<<endl;
str1+="22222";//在末尾添加字符串或字符//
cout<<str1<<endl;
str1.insert(str1.begin()+1,'1');/*str1.begin()+i就是在下标为i的地方插入
同样也只能插入一个字符*/
cout<<str1<<endl;
/*str1.append(str2);*/
cout<<str1.size()<<" "<<str1.length()<<endl;//string 中的字符个数//
/*cout<<sizeof(str1)<<endl;//实际长度//*/
for(auto i=str1.begin();i!=str1.end();i++)//.begin()指向第一个元素.end()指向最后一个元素的下个元素//
(*i)++;
cout<<str1<<endl;
for(int i=0;i<str2.size();i++)
str2[i]++;
cout<<str2<<endl;
cout<<str1<<endl;
for(auto i=str1.end()-1;i>=str1.begin();i--)//end-1为最后一个字符 begin()为第一个字符//
cout<<*i;cout<<endl;
for(string::reverse_iterator i=str1.rbegin();i<str1.rend();i++ )//反向迭代器也是=begin();<end();i++//
cout<<*i;cout<<endl;
str1.clear();
string str5="2";
string str4="5";
str1=str5+str4;//不能两个字符串常量相加//
str1+="ssdsds";
cout<<str1<<endl;
str1+='w';
cout<<str1<<endl;//可以加单个字符//
char ch[]={'2','3','\0'};//字符数组整体输出前最后一定要保证有‘\0’//
cout<<ch<<endl;
char ch2[3]={'2','3'};
cout<<ch2<<endl;//字符数组大小未满时自动补充结束符//
getline(cin,str1,'8');//第三个参数等效于‘\0’遇见后终止输入并且不保存在缓冲区//
cout<<str1;
/*cout<<std::boolalpha;//只保证下一个判断的输出//
cout<<(str1>str2)<<endl;*/
}
toupper不需要头文件?
#include<iostream>
#include<string>
using namespace std;
int main()
{ string str1;
while(cin>>str1)
{
for(auto i=str1.begin();i!=str1.end();i++)
*i=toupper(*i);//只写toupper(*i)则根本没用,函数只是返回一个大写字母本身并不具有改变功能//
cout<<str1<<endl;
}
}