C++ STL string类的compare函数使用

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a("aBcdef");
    string b("AbcdEf");
    string c("123456");
    string d("123dfg");
    //下面是各种比较方法
    //前面减去后面的ASCII码,>0返回1,<0返回-1,相同返回0
    //完整比较a和b
    int m=a.compare(b);
    //“Bcdef”和“AbcdEf”的比较,比较a和b的从1到5位
    int n=a.compare(1,5,b);
    //“Bcdef”和“Ef”的比较
    int p=a.compare(1,5,b,4,2);
    //"123"和“123”的比较
    int q=c.compare(0,3,d,0,3);
    cout<<"m="<<m<<",n="<<n<<",p="<<p<<",q="<<q<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ibelievesunshine/article/details/80205656