📜  C++中的std :: 字符串:: compare()

📅  最后修改于: 2021-05-30 12:14:02             🧑  作者: Mango

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

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

字符串:: compare()的不同语法:

  1. 语法1:比较字符串* this和字符串str。
    int string::compare (const string& str) const
    Returns:
    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
    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
    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的最大str_len个字符(以索引str_idx开头)。
    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
    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字符串cstr的字符进行比较。
    int string::compare (const char* cstr) const
    // CPP code to demonstrate
    // int string::compare (const char* cstr) const
      
    #include
    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字符串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
    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:比较,顶多字符串的len字符*此,从与所述字符数组字符的字符chars_len索引IDX。
    int string::compare (size_type idx, size_type len, const char* chars, 
    size_type chars_len)const

    请注意,char必须至少具有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
    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
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”