[ Web Proxy ]
URL:
Viewing: http://pl.cppreference.com/cpp/string/basic_string/compare [Back]  [Original]

compare cppreference.com
cppreference.com
Przestrzenie nazw
Warianty

compare

Z cppreference.com

Skadnia:

    #include <string>
    int compare( const string& str ) const;
    int compare( const charT* str ) const;
    int compare( size_type index, size_type length, const string& str ) const;
    int compare( size_type index, size_type length, const string& str, size_type index2, size_type length2 ) const;
    int compare( size_type index, size_type length, const charT* str, size_type length2 = npos ) const;

Metoda compare() porwnuje obecny string (this) z str:

Warto zwracana W przypadku
mniejsza od zera this < str
zero this == str
wiksza od zera this > str

Rne metody:

  • porwnuje string obecny z str,
  • porwnuje podcig obecnego stringa od pozycji index majcy lenght znakw z str
  • porwnuj podcig obecnego stringa z podcigiem str, gdzie index i lenght odnosi si do obecnego, a index2 i lenght2 odnosi si do str
  • porwnuje podcig obecnego stringa z prefiksem str, gdzie index i lenght odnosi si do obecnego, a lenght2 jest dugoci prefiksu str


Na przykad poniszy kod uywa compare() by porwna 4 stringi z kadym pozostaym.

   string names[] = {"Homer", "Marge", "3-eyed fish", "inanimate carbon rod"};

   for( int i = 0; i < 4; i++ ) {
     for( int j = 0; j < 4; j++ ) {
       cout << names[i].compare( names[j] ) << " ";
     }
     cout << endl;
   }

Powyszy kod generuje tabel:

Homer Marge 3-eyed fish inanimate carbon rod
"Homer".compare( x ) 0
1
"Marge".compare( x ) 1 0 1
"3-eyed fish".compare( x )
0
"inanimate carbon rod".compare( x ) 1 1 1 0

Powizane tematy: String_operators


Web Proxy Viewer  |  New URL  |  Original Page