📄 compare.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>compare</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Strings</a> > <a href= "compare.html">compare</a> </div> <div class="name-format"> compare </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> int compare( const string& str ); int compare( const char* str ); int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string& str ); int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string& str, <strong>size_type</strong> index2, <strong>size_type</strong> length2 ); int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const char* str, <strong>size_type</strong> length2 );</pre> <p>The compare() function either compares <em>str</em> to the current string in a variety of ways, returning</p> <table class="code-table"> <tr> <th class="code-table-th">Return Value</th> <th class="code-table-th">Case</th> </tr> <tr> <td class="code-table-td">less than zero</td> <td class="code-table-td">this < str</td> </tr> <tr> <td class="code-table-td">zero</td> <td class="code-table-td">this == str</td> </tr> <tr> <td class="code-table-td">greater than zero</td> <td class="code-table-td">this > str</td> </tr> </table> <p>The various functions either:</p> <ul> <li>compare <em>str</em> to the current string,</li> <li>compare <em>str</em> to a substring of the current string, starting at <em>index</em> for <em>length</em> characters,</li> <li>compare a substring of <em>str</em> to a substring of the current string, where <em>index2</em> and <em>length2</em> refer to <em>str</em> and <em>index</em> and <em>length</em> refer to the current string,</li> <li>or compare a substring of <em>str</em> to a substring of the current string, where the substring of <em>str</em> begins at zero and is <em>length2</em> characters long, and the substring of the current string begins at <em>index</em> and is <em>length</em> characters long.</li> </ul> <p>For example, the following code uses compare() to compare four strings with eachother:</p> <pre class="example-code"> 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; } </pre> <p>Data from the above code was used to generate this table, which shows how the various strings compare to eachother:</p> <table class="code-table"> <tr> <th class="code-table-th"></th> <th class="code-table-th">Homer</th> <th class="code-table-th">Marge</th> <th class="code-table-th">3-eyed fish</th> <th class="code-table-th">inanimate carbon rod</th> </tr> <tr> <td class="code-table-td">"Homer".compare( x )</td> <td class="code-table-td">0</td> <td class="code-table-td">-1</td> <td class="code-table-td">1</td> <td class="code-table-td">-1</td> </tr> <tr> <td class="code-table-td">"Marge".compare( x )</td> <td class="code-table-td">1</td> <td class="code-table-td">0</td> <td class="code-table-td">1</td> <td class="code-table-td">-1</td> </tr> <tr> <td class="code-table-td">"3-eyed fish".compare( x )</td> <td class="code-table-td">-1</td> <td class="code-table-td">-1</td> <td class="code-table-td">0</td> <td class="code-table-td">-1</td> </tr> <tr> <td class="code-table-td">"inanimate carbon rod".compare( x )</td> <td class="code-table-td">1</td> <td class="code-table-td">1</td> <td class="code-table-td">1</td> <td class="code-table-td">0</td> </tr> </table> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="string_operators.html">String operators</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -