返回值 | 情况 |
---|---|
小于零 | this < str |
零 | this == str |
大于零 | this > str |
-
比较自己和str,
-
比较自己的子串和str,子串以index索引开始,长度为length
-
比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
-
比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string s,str;
cin>>s>>str;
cout<<s.compare(str)<<endl;//相等为0,s<str ----》值为-1,否则为1;
cout<<s.compare(0,2,str)<<endl;
return 0;
}
- num - 1个字符已经读入,
- 碰到一个换行标志,
- 碰到一个EOF,
- 或者,任意地读入,直到读到字符delim。delim字符不会被放入buffer中。
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string s,str;
getline(cin,str);
cin>>s;
cout<<str<<endl;
cout<<s<<endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main( )
{
string s="abcdef";
string str="AbCDe";
//transform()只适合字符串
transform(s.begin(),s.end(),s.begin(),::toupper);//转换为大写
transform(str.begin(),str.end(),str.begin(),::tolower);//转换为小写
cout<<s<<endl;
cout<<str<<endl;
//字符数组的大小写转换函数
char s1[10]={'a','B','d','R','g','H','s',};
strlwr(s1);
cout<<s1<<endl;
strupr(s1);
cout<<s1<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string first="This comes first" ;
string second="And this is second" ;
first.swap( second );
cout<<first<<endl;
cout<<second<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin,s);
char chr[6]={'a','b','c'};
cout<<strlen(chr)<<endl;//只适用于字符数组
cout<<s.length()<<endl;//只适用于字符串
return 0;
}
评论(0)
您还未登录,请登录后发表或查看评论