비교하다() 문자열 클래스의 공개 멤버 함수입니다. 문자열 객체(또는 하위 문자열)의 값을 해당 인수로 지정된 문자 시퀀스와 비교합니다.
Compare()는 각 문자열에 대해 둘 이상의 인수를 처리할 수 있으므로 해당 인덱스와 길이로 하위 문자열을 지정할 수 있습니다.
반환 유형: Compare()는 부울 값이 아닌 정수 값을 반환합니다.
string::compare() 의 다양한 구문:
- 구문 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 : *이 값이 str보다 길거나 일치하지 않는 첫 번째 문자가 더 큰 경우>
CPP
// 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 than s2)> >if>((s1.compare(s2)) <0)> >cout << s1 << ' is smaller than ' << s2 << endl;> >// returns 0(s1, is being compared 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: 문자열 *this의 최대 len 문자를 비교합니다. 인덱스 idx로 시작하여 문자열 str을 사용합니다.
int string::compare (size_type idx, size_type len, const string& str) const Throws out_of_range if index>크기().>
CPP
foreach 자바
// 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: 인덱스 idx로 시작하는 문자열 *this의 최대 len 문자를 인덱스 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>크기(). str_idx> str.size().>'>인 경우 out_of_range를 발생시킵니다.CPP
// 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 techcodeview.com World>
- 구문 4: 문자열 *this의 문자를 C 문자열 cstr의 문자와 비교합니다.
int string::compare (const char* cstr) const>
CPP
// CPP code to demonstrate> // int string::compare (const char* cstr) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> >// returns <0 (s1 < 'techcodeview.com')> >if>((s1.compare('techcodeview.com')) <0)> >cout << s1 << ' is smaller than string ' << 'techcodeview.com';> >//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 techcodeview.com forGeeks is equal to string forGeeks>
- 구문 5: C-문자열 cstr의 모든 문자와 인덱스 idx로 시작하여 문자열 *this의 최대 len 문자를 비교합니다.
int string::compare (size_type idx, size_type len, const char* cstr) const>
cstr은 널 포인터(NULL)가 아닐 수 있습니다.
CPP
파이썬에서는
// 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: 문자 배열 chars의 chars_len 문자와 인덱스 idx로 시작하여 문자열 *this의 최대 len 문자를 비교합니다.
int string::compare (size_type idx, size_type len, const char* chars, size_type chars_len)const>
문자에는 적어도 chars_len 문자가 있어야 합니다. 문자는 임의의 값을 가질 수 있습니다. 따라서 ' '은 특별한 의미가 없습니다.
CPP
자바 스윙 튜토리얼
// 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;> }> |
>
>