📄 cppstring_details.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta name="generator" content="HTML Tidy for Linux/x86 (vers 1st October 2002), see www.w3.org"> <title>C++ Strings</title> </head> <body bgcolor="#ffffff"> <table width="100%" bgcolor="#eeeeff"> <tr> <td><a href="index.html">cppreference.com</a> -> <a href="cppstring.html">C++ Strings</a> -> Details</td> </tr> </table> <h1>C++ Strings</h1> <hr> <h2><a name="Constructors">Constructors</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> string(); string( size_type length, char ch ); string( const char *str ); string( const char *str, size_type length ); string( string &str, size_type index, size_type length ); string( <a href="iterators.html">input_iterator</a> start, <a href="iterators.html">input_iterator</a> end );</pre> </td> </tr> </table> <p>The string constructors create a new string containing:</p> <ul> <li><i>length</i> copies of <i>ch</i>,</li> <li>a duplicate of <i>str</i> (optionally up to <i>length</i> characters long),</li> <li>a substring of <i>str</i> starting at <i>index</i> and <i>length</i> characters long, or</li> <li>elements from <i>start</i> to <i>end</i>.</li> </ul> For example, <br> <br> <pre> string str1( 5, 'c' ); string str2( "Now is the time..." ); string str3( str2, 11, 4 ); cout << str1 << endl; cout << str2 << endl; cout << str3 << endl;</pre> <p>displays</p><pre> ccccc Now is the time... time </pre> <hr> <h2><a name="Operators">Operators</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> == > < >= <= != + += []</pre> </td> </tr> </table> <p>You can compare strings with ==, >, <, >=, <=, and !=. You can combine two strings with the + operator or the += operator, and look at a specific character with the [] operator.</p> <i>Related topics:</i><br> <strong><a href="#at">at()</a>, <a href="#compare">compare()</a>.</strong> <hr> <h2><a name="append">append</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> basic_string &append( const basic_string &str ); basic_string &append( const char *str ); basic_string &append( const basic_string &str, size_type index, size_type len ); basic_string &append( const char *str, size_type num ); basic_string &append( size_type num, char ch ); basic_string &append( <a href="iterators.html">input_iterator</a> start, <a href="iterators.html">input_iterator</a> end );</pre> </td> </tr> </table> <p>The append() function either:</p> <ul> <li>appends <i>str</i> on to the end of the current string,</li> <li>appends a substring of <i>str</i> starting at <i>index</i> that is <i>len</i> characters long on to the end of the current string,</li> <li>appends <i>num</i> characters of <i>str</i> on to the end of the current string,</li> <li>appends <i>num</i> repititions of <i>ch</i> on to the end of the current string,</li> <li>or appends the sequence denoted by <i>start</i> and <i>end</i> on to the end of the current string.</li> </ul> For example, the following code: <br> <br> <pre> string str = "Hello World"; str.append( 10, '!' ); cout << str << endl;</pre> <p>displays</p><pre> Hello World!!!!!!!!!!</pre> <i>Related topics:</i><br> <strong><a href="#operators">The + operator</a></strong> <hr> <h2><a name="assign">assign</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> basic_string &assign( const basic_string &str ); basic_string &assign( const char *str ); basic_string &assign( const char *str, size_type num ); basic_string &assign( const basic_string &str, size_type index, size_type len ); basic_string &assign( size_type num, char ch );</pre> </td> </tr> </table> <p>The function assign either:</p> <ul> <li>assigns <i>str</i> to the current string,</li> <li>assigns the first <i>num</i> characters of <i>str</i> to the current string,</li> <li>assigns a substring of <i>str</i> starting at <i>index</i> that is <i>len</i> characters long to the current string,</li> <li>or assigns <i>num</i> reptitions of <i>ch</i> to the current string.</li> </ul> For example, the following code: <br> <br> <pre> string str1, str2 = "War and Peace"; str1.assign( str2, 4, 3 ); cout << str1 << endl;</pre> <p>displays</p><pre> and </pre> <hr> <h2><a name="at">at</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> reference at( size_type index );</pre> </td> </tr> </table> <p>The at() function returns a reference to the character at location <i>index</i>. If <i>index</i> is not within the string, then at() reports an out of range error by throwing an object of the <strong>out_of_range</strong> class. For example, this code:</p><pre> string text = "ABCDEF"; char ch = text.at( 2 );</pre> <p>displays the character 'C'.</p> <i>Related topics:</i><br> <strong><a href="#Operators">The [] operator</a></strong> <hr> <h2><a name="begin">begin</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> begin();</pre> </td> </tr> </table> <p>The begin() function returns an <a href="iterators.html">iterator</a> to the first element of the current string.</p> <i>Related topics:</i><br> <strong><a href="#end">end()</a></strong> <hr> <h2><a name="c_str">c_str</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> const char *c_str();</pre> </td> </tr> </table> <p>The function c_str() returns a pointer to a regular C string, identical to the current string.</p> <i>Related topics:</i><br> <strong><a href="#Operators">The [] operator</a></strong> <hr> <h2><a name="capacity">capacity</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type capacity();</pre> </td> </tr> </table> <p>The capacity() function returns the number of characters that the current string can hold before it will need to allocate more memory. This number will be at least as large as <a href= "#size">size()</a>.</p> <i>Related topics:</i><br> <strong><a href="#max_size">max_size()</a>, <a href="#reserve">reserve()</a>, <a href= "#resize">resize()</a>, <a href="#size">size()</a>,</strong> <hr> <h2><a name="clear">clear</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void clear();</pre> </td> </tr> </table> <p>The clear() function clears the string of all characters, effectively calling erase( begin(), end() ).</p> <i>Related topics:</i><br> <strong><a href="#erase">erase()</a></strong> <hr> <h2><a name="compare">compare</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> int compare( const basic_string &str ); int compare( const char *str ); int compare( size_type index, size_type length, const basic_string &str ); int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 ); int compare( size_type index, size_type length, const char *str, size_type length2 );</pre> </td> </tr> </table> <p>The compare() function either compares <i>str</i> to the current string in a variety of ways, returning</p> <table> <tr> <th>Return Value</th> <th>Case</th> </tr> <tr bgcolor="#eeeeff"> <td>less than zero</td> <td>this < str</td> </tr> <tr> <td>zero</td> <td>this == str</td> </tr> <tr bgcolor="#eeeeff"> <td>greater than zero</td> <td>this > str</td> </tr> </table> <p>The various functions either:</p> <ul> <li>compare <i>str</i> to the current string,</li> <li>compare <i>str</i> to a substring of the current string,starting at <i>index</i> for <i>length</i> characters,</li> <li>compare a substring of <i>str</i> to a substring of the current string, where <i>index2</i> and <i>length2</i> refer to <i>str</i> and <i>index</i> and <i>length</i> refer to the current string,</li>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -