📄 all.html
字号:
<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><hr> <div class="name-format"> copy </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> <strong>size_type</strong> copy( char* str, <strong>size_type</strong> num, <strong>size_type</strong> index = 0 );</pre> <p>The copy() function copies <em>num</em> characters of the current string (starting at <em>index</em> if it's specified, 0 otherwise) into <em>str</em>.</p> <p>The return value of copy() is the number of characters copied.</p> <p>For example, the following code uses copy() to extract a substring of a string into an array of characters:</p> <pre class="example-code"> char buf[30]; memset( buf, '\0', 30 ); string str = "Trying is the first step towards failure."; str.copy( buf, 24 ); cout << buf << endl; </pre> <p>When run, this code displays:</p> <pre class="example-code"> Trying is the first step </pre> <p>Note that before calling copy(), we first call (Standard C String and Character) <a href="../stdstring/memset.html">memset</a>() to fill the destination array with copies of the <strong>NULL</strong> character. This step is included to make sure that the resulting array of characters is <strong>NULL</strong>-terminated.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="substr.html">substr</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> data </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> const char *data();</pre> <p>The function data() returns a pointer to the first character in the current string.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="string_operators.html">String operators</a><br> <a href="c_str.html">c_str</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> empty </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> bool empty() const;</pre> <p>The empty() function returns true if the string has no elements, false otherwise.</p> <p>For example:</p> <pre class="example-code"> string s1; string s2(""); string s3("This is a string"); cout.setf(ios::boolalpha); cout << s1.empty() << endl; cout << s2.empty() << endl; cout << s3.empty() << endl;</pre> <p>When run, this code produces the following output:</p> <pre class="example-output"> true true false</pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="size.html">size</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> end </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> iterator end(); const_iterator end() const;</pre> <p>The end() function returns an iterator just past the end of the string.</p> <p>Note that before you can access the last element of the string using an iterator that you get from a call to end(), you'll have to decrement the iterator first.</p> <p>For example, the following code uses <a href= "begin.html">begin</a>() and end() to iterate through all of the members of a vector:</p> <pre class="example-code"> vector<int> v1( 5, 789 ); vector<int>::iterator it; for( it = v1.begin(); it != v1.end(); it++ ) { cout << *it << endl; } </pre> <p>The iterator is initialized with a call to <a href= "begin.html">begin</a>(). After the body of the loop has been executed, the iterator is incremented and tested to see if it is equal to the result of calling end(). Since end() returns an iterator pointing to an element just after the last element of the vector, the loop will only stop once all of the elements of the vector have been displayed.</p> <p>end() runs in <a href="../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="begin.html">begin</a><br> <a href="rbegin.html">rbegin</a><br> <a href="rend.html">rend</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> erase </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> iterator erase( iterator loc ); iterator erase( iterator start, iterator end ); string& erase( <strong>size_type</strong> index = 0, <strong>size_type</strong> num = npos );</pre> <p>The erase() function either:</p> <ul> <li>removes the character pointed to by <em>loc</em>, returning an iterator to the next character,</li> <li>removes the characters between <em>start</em> and <em>end</em> (including the one at <em>start</em> but not the one at <em>end</em>), returning an iterator to the character after the last character removed,</li> <li>or removes <em>num</em> characters from the current string, starting at <em>index</em>, and returns *this.</li> </ul> <p>The parameters <em>index</em> and <em>num</em> have default values, which means that erase() can be called with just <em>index</em> to erase all characters after <em>index</em> or with no arguments to erase all characters.</p> <p>For example:</p> <pre class="example-code"> string s("So, you like donuts, eh? Well, have all the donuts in the world!"); cout << "The original string is '" << s << "'" << endl; s.erase( 50, 14 ); cout << "Now the string is '" << s << "'" << endl; s.erase( 24 ); cout << "Now the string is '" << s << "'" << endl; s.erase(); cout << "Now the string is '" << s << "'" << endl; </pre> <p>will display</p> <pre class="example-code"> The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!' Now the string is 'So, you like donuts, eh? Well, have all the donuts' Now the string is 'So, you like donuts, eh?' Now the string is '' </pre> <p>erase() runs in <a href="../complexity.html">linear time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="insert.html">insert</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> find </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> <strong>size_type</strong> find( const string& str, <strong>size_type</strong> index ); <strong>size_type</strong> find( const char* str, <strong>size_type</strong> index ); <strong>size_type</strong> find( const char* str, <strong>size_type</strong> index, <strong>size_type</strong> length ); <strong>size_type</strong> find( char ch, <strong>size_type</strong> index );</pre> <p>The function find() either:</p> <ul> <li>returns the first occurrence of <em>str</em> within the current string, starting at <em>index</em>, string::npos if nothing is found,</li> <li>if the <em>length</em> parameter is given, then find() returns the first occurrence of the first <em>length</em> characters of <em>str</em> within the current string, starting at <em>index</em>, string::npos if nothing is found,</li> <li>or returns the index of the first occurrence <em>ch</em> within the current string, starting at <em>index</em>, string::npos if nothing is found.</li> </ul> <p>For example:</p> <pre class="example-code"> string str1( "Alpha Beta Gamma Delta" ); string::size_type loc = str1.find( "Omega", 0 ); if( loc != string::npos ) { cout << "Found Omega at " << loc << endl; } else { cout << "Didn't find Omega" << endl; }</pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="find_first_not_of.html">find_first_not_of</a><br> <a href="find_first_of.html">find_first_of</a><br> <a href="find_last_not_of.html">find_last_not_of</a><br> <a href="find_last_of.html">find_last_of</a><br> <a href="rfind.html">rfind</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> find_first_not_of </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> <strong>size_type</strong> find_first_not_of( const string& str, <strong>size_type</strong> index = 0 ); <strong>size_type</strong> find_first_not_of( const char* str, <strong>size_type</strong> index = 0 ); <strong>size_type</strong> find_first_not_of( const char* str, <strong>size_type</strong> index, <strong>size_type</strong> num ); <strong>size_type</strong> find_first_not_of( char ch, <strong>size_type</strong> index = 0 );</pre> <p>The find_first_not_of() function either:</p> <ul> <li>returns the index of the first character within the current string that does not match any character in <em>str</em>, beginning the search at <em>index</em>, string::npos if nothing is found,</li> <li>searches the current string, beginning at <em>index</em>, for any character that does not match the first <em>num</em> characters in <em>str</em>, returning the index in the current string of the first character found that meets this criteria, otherwise returning string::npos,</li> <li>or returns the index of the first occurrence of a character that does not match <em>ch</em> in the current string, starting the search at <em>index</em>, string::npos if nothing is found.</li> </ul> <p>For example, the following code searches a string of text for the first character that is not a lower-case character, space, comma, or hypen:</p> <pre class="example-code"> string lower_case = "abcdefghijklmnopqrstuvwxyz ,-"; string str = "this is the lower-case part, AND THIS IS THE UPPER-CASE PART"; cout << "first non-lower-case letter in str at: " << str.find_first_not_of(lower_case) << endl; </pre> <p>When run, find_first_not_of() finds the first upper-case letter in <em>str</em> at index 29 and displays this output:</p> <pre class="example-code"> first non-lower-case letter in str at: 29 </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="find.html">find</a><br> <a href="find_first_not_of.html">find_first_not_of</a><br> <a href="find_first_of.html">find_first_of</a><br> <a href="find_last_not_of.html">find_last_not_of</a><br> <a href="find_last_of.html">find_last_of</a><br> <a href="rfind.html">rfind</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> find_first_of </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> <strong>size_type</strong> find_first_of( const string &str, <strong>size_type</strong> index = 0 ); <strong>size_type</strong> find_first_of( const char* str, <strong>size_type</strong> index = 0 ); <strong>size_type</strong> find_first_of( const char* str, <strong>size_type</strong> index, <strong>size_type</strong> num ); <strong>size_type</strong> find_first_of( char ch, <strong>size_type</strong> index = 0 );</pre> <p>The find_first_of() function either:</p> <ul> <li>returns the index of the first character within the current string that matches any character in <em>str</em>, beginning the search at <em>index</em>, string::npos if nothing is found,</li> <li>searches the current string, beginning at <em>index</em>, for any of the first <em>num</em> characters in <em>str</em>, returning the index in the current string of the first character found, or string::npos if no characters match,</li> <li>or returns the index of the first occurrence of <em>ch</em> in the current string, starting the search at <em>index</em>,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -