当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::string::compare()用法及代码示例


compare()是字符串类的公共成员函数。它将字符串对象(或子字符串)的值与其参数指定的字符序列进行比较。
compare()可以为每个字符串处理多个参数,以便可以通过其索引和长度指定子字符串。

返回类型:compare()返回一个整数值,而不是布尔值。

string::compare(的不同语法):


  1. 语法1:比较字符串* this和字符串str。
    int string::compare (const string& str) const
    返回:
    0: if both strings are equal.
    A value < 0:if *this is shorter than str or,
    first character that doesn't match is smaller than str.
    A value > 0: if *this is longer than str or,
    first character that doesn't match is greater
    
    // CPP code for demonstrating  
    // string::compare (const string& str) const 
      
    #include<iostream> 
    using namespace std; 
      
    void compareOperation(string s1, string s2) 
    { 
        // returns a value < 0 (s1 is smaller then s2) 
        if((s1.compare(s2)) < 0) 
            cout << s1 << " is smaller than " << s2 << endl; 
      
        // returns 0(s1, is being comapared to itself) 
        if((s1.compare(s1)) == 0) 
            cout << s1 << " is equal to " << s1 << endl; 
        else
            cout << "Strings didn't match "; 
          
    } 
      
    // Driver Code 
    int main() 
    { 
        string s1("Geeks"); 
        string s2("forGeeks"); 
        compareOperation(s1, s2); 
          
        return 0;  
    }

    输出:

    Geeks is smaller than forGeeks
    Geeks is equal to Geeks
    
  2. 语法2:最多比较字符串* this的len个字符,以索引idx和字符串str开头。
    int string::compare (size_type idx, size_type len, const string& str) const
    Throws out_of_range if index > size().
    
    // CPP code to demonstrate  
    // int string::compare (size_type idx, size_type len,  
    // const string& str) const 
      
    #include<iostream> 
    using namespace std; 
      
    void compareOperation(string s1, string s2) 
    { 
        // Compares 5 characters from index number 3 of s2 with s1 
        if((s2.compare(3, 5, s1)) == 0) 
            cout << "Here, "<< s1 << " are " << s2; 
      
        else
            cout << "Strings didn't match "; 
    } 
    // Driver Code 
    int main() 
    { 
        string s1("Geeks"); 
        string s2("forGeeks"); 
        compareOperation(s1, s2); 
           
      return 0;  
    }

    输出:

    Here, Geeks are forGeeks
    
  3. 语法3:比较最多*个字符串的len个字符,此字符以索引idx开头,最多比较以字符串str_idx开头的字符串str的str_len个字符。
    int string::compare (size_type idx, size_type len, const string& 
    str, size_type str_idx, size_type str_len) const
    Throws out_of_range if idx > size().
    Throws out_of_range if str_idx > str.size().
    
    // CPP code to demonstrate  
    // int string::compare (size_type idx, size_type len, const string&  
    // str, size_type str_idx, size_type str_len) const 
      
    #include<iostream> 
    using namespace std; 
      
    void compareOperation(string s1, string s2) 
    { 
        // Compares 5 characters from index number 0 of s1 with 
        // 5 characters from index 3 of s2 
        if((s1.compare(0, 5, s2, 3, 5)) == 0) 
            cout << "Welcome to " << s1 << s2 << " World"; 
      
        else
            cout << "Strings didn't match "; 
    } 
    // Driver Code 
    int main() 
    { 
        string s1("Geeks"); 
        string s2("forGeeks"); 
        compareOperation(s1, s2); 
           
      return 0;  
    }

    输出:

    Welcome, to GeeksforGeeks World
    
  4. 语法4:将字符串* this的字符与C-string cstr的字符进行比较。
    int string::compare (const char* cstr) const
    // CPP code to demonstrate 
    // int string::compare (const char* cstr) const 
      
    #include<iostream> 
    using namespace std; 
      
    void compareOperation(string s1, string s2) 
    { 
        // returns < 0 (s1 < "GeeksforGeeks") 
        if((s1.compare("GeeksforGeeks")) < 0) 
            cout << s1 << " is smaller than string " << "GeeksforGeeks";  
      
        //returns 0 (s2 is "forgeeks") 
        if((s2.compare("forGeeks")) == 0) 
            cout << endl << s2 << " is equal to string " << s2;  
      
        else
            cout << "Strings didn't match "; 
          
    } 
    // Driver Code 
    int main() 
    { 
        string s1("Geeks"); 
        string s2("forGeeks"); 
        compareOperation(s1, s2); 
           
      return 0;  
    }

    输出:

    Geeks is smaller than string GeeksforGeeks
    forGeeks is equal to string forGeeks
    
  5. 语法5:最多比较字符串* this的len个字符,从索引idx到C-string cstr的所有字符。
    int string::compare (size_type idx, size_type len, const char* cstr) const
    

    请注意,cstr可能不是空指针(NULL)。

    // CPP code to demonstrate  
    // int string::compare (size_type idx, size_type len,  
    // const char* cstr) const 
    #include<iostream> 
    using namespace std; 
      
    void compareOperation(string s1) 
    { 
        // Compares 5 characters from 0 index of s1 with "Geeks" 
        if((s1.compare(0, 5, "Geeks")) == 0) 
            cout << s1 << " are " << "awesome people";  
          
        else
            cout << "Strings didn't match "; 
       
    } 
      
    // Driver Code 
    int main() 
    { 
        string s1("Geeks"); 
        compareOperation(s1); 
           
      return 0;  
    }

    输出:

    Geeks are awesome people
    
  6. 语法6:最多比较字符串* this的len个字符,从索引idx开始,以字符数组chars的chars_len字符开头。
    int string::compare (size_type idx, size_type len, const char* chars, 
    size_type chars_len)const

    请注意,字符必须至少包含chars_len字符。字符可以具有任意值。因此,“ \ 0”没有特殊含义。

    // CPP code to demonstrate  
    // int string::compare (size_type idx, size_type len,  
    // const char* chars, size_type chars_len)const 
      
    #include<iostream> 
    using namespace std; 
      
    void compareOperation(string s1, string s2) 
    { 
        // Compares 5 characters from 0 index of s1 with  
        // 5 characters of string "Geeks" 
        if((s1.compare(0, 5, "Geeks", 5)) == 0) 
            cout << "This is " << s1 <<  s2 ;  
       
        else
            cout << "Strings didn't match "; 
    } 
      
    // Driver Code 
    int main() 
    { 
        string s1("Geeks"); 
        string s2("forGeeks"); 
        compareOperation(s1, s2); 
           
      return 0;  
    }

    输出:

    This is GeeksforGeeks
    


相关用法


注:本文由纯净天空筛选整理自 std::string::compare() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。