📄 all.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>C++ Strings</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> </div> <div class="name-format"> append </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> string& append( const string& str ); string& append( const char* str ); string& append( const string& str, <strong>size_type</strong> index, <strong>size_type</strong> len ); string& append( const char* str, <strong>size_type</strong> num ); string& append( <strong>size_type</strong> num, char ch ); string& append( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );</pre> <p>The append() function either:</p> <ul> <li>appends <em>str</em> on to the end of the current string,</li> <li>appends a substring of <em>str</em> starting at <em>index</em> that is <em>len</em> characters long on to the end of the current string,</li> <li>appends <em>num</em> characters of <em>str</em> on to the end of the current string,</li> <li>appends <em>num</em> repititions of <em>ch</em> on to the end of the current string,</li> <li>or appends the sequence denoted by <em>start</em> and <em>end</em> on to the end of the current string.</li> </ul> <p>For example, the following code uses append() to add 10 copies of the '!' character to a string:</p> <pre class="example-code"> string str = "Hello World"; str.append( 10, '!' ); cout << str << endl; </pre> <p>That code displays:</p> <pre class="example-code"> Hello World!!!!!!!!!! </pre> <p>In the next example, append() is used to concatenate a substring of one string onto another string:</p> <pre class="example-code"> string str1 = "Eventually I stopped caring..."; string str2 = "but that was the '80s so nobody noticed."; str1.append( str2, 25, 15 ); cout << "str1 is " << str1 << endl; </pre> <p>When run, the above code displays:</p> <pre class="example-code"> str1 is Eventually I stopped caring...nobody noticed. </pre> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> assign </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> void assign( <strong>size_type</strong> num, const char& val ); void assign( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end ); string& assign( const string& str ); string& assign( const char* str ); string& assign( const char* str, <strong>size_type</strong> num ); string& assign( const string& str, <strong>size_type</strong> index, <strong>size_type</strong> len ); string& assign( <strong>size_type</strong> num, const char& ch );</pre> <p>The deafult assign() function gives the current string the values from <em>start</em> to <em>end</em>, or gives it <em>num</em> copies of <em>val</em>.</p> <p>In addition to the normal (C++ Lists) <a href= "../cpplist/assign.html">assign</a>() functionality that all C++ containers have, strings possess an assign() function that also allows them to:</p> <ul> <li>assign <em>str</em> to the current string,</li> <li>assign the first <em>num</em> characters of <em>str</em> to the current string,</li> <li>assign a substring of <em>str</em> starting at <em>index</em> that is <em>len</em> characters long to the current string,</li> </ul> <p>For example, the following code:</p> <pre class="example-code"> string str1, str2 = "War and Peace"; str1.assign( str2, 4, 3 ); cout << str1 << endl; </pre> <p>displays</p> <pre class="example-code"> and </pre> <p>This function will destroy the previous contents of the string.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> (C++ Lists) <a href="../cpplist/assign.html">assign</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> at </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> <a href="../containers.html">TYPE</a>& at( <strong>size_type</strong> loc ); const <a href="../containers.html">TYPE</a>& at( <strong>size_type</strong> loc ) const;</pre> <p>The at() function returns a reference to the element in the string at index <em>loc</em>. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the string.</p> <p>For example, consider the following code:</p> <pre class="example-code"> vector<int> v( 5, 1 ); for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v[i] << endl; } </pre> <p>This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:</p> <pre class="example-code"> vector<int> v( 5, 1 ); for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v.at(i) << endl; } </pre> <p>Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> (C++ Multimaps) <a href= "../cppmultimap/multimap_operators.html">Multimap operators</a><br> (C++ Double-ended Queues) <a href= "../cppdeque/container_operators.html">Container operators</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> begin </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> iterator begin(); const_iterator begin() const;</pre> <p>The function begin() returns an iterator to the first element of the string. begin() should run in <a href= "../complexity.html">constant time</a>.</p> <p>For example, the following code uses begin() to initialize an iterator that is used to traverse a list:</p> <pre class="example-code"> // Create a list of characters list<char> charList; for( int i=0; i < 10; i++ ) { charList.push_front( i + 65 ); } // Display the list list<char>::iterator theIterator; for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ ) { cout << *theIterator; } </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="end.html">end</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"> c_str </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> const char* c_str();</pre> <p>The function c_str() returns a const pointer to a regular C string, identical to the current string. The returned string is null-terminated.</p> <p>Note that since the returned pointer is of type <a href="../keywords/const.html">const</a>, the character data that c_str() returns <strong>cannot be modified</strong>. Furthermore, you do not need to call <a href="../stdmem/free.html">free()</a> or <a href="../keywords/delete.html">delete</a> on this pointer.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="string_operators.html">String operators</a><br> <a href="data.html">data</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> capacity </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> <strong>size_type</strong> capacity() const;</pre> <p>The capacity() function returns the number of elements that the string can hold before it will need to allocate more space.</p> <p>For example, the following code uses two different methods to set the capacity of two vectors. One method passes an argument to the constructor that suggests an initial size, the other method calls the reserve function to achieve a similar goal:</p> <pre class="example-code"> vector<int> v1(10); cout << "The capacity of v1 is " << v1.capacity() << endl; vector<int> v2; v2.reserve(20); cout << "The capacity of v2 is " << v2.capacity() << endl; </pre> <p>When run, the above code produces the following output:</p> <pre class="example-code"> The capacity of v1 is 10 The capacity of v2 is 20 </pre> <p>C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the <a href= "reserve.html">reserve</a>() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.</p> <p>The capacity() function runs in <a href= "../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="reserve.html">reserve</a><br> <a href="resize.html">resize</a><br> <a href="size.html">size</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> clear </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> void clear();</pre> <p>The function clear() deletes all of the elements in the string. clear() runs in <a href="../complexity.html">linear time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> (C++ Lists) <a href="../cpplist/erase.html">erase</a> </div> </div> </td> </tr> </table></body></html><hr> <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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -