📄 string_operators.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>String operators</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= "string_operators.html">String operators</a> </div> <div class="name-format"> String operators </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> bool operator==(const string& c1, const string& c2); bool operator!=(const string& c1, const string& c2); bool operator<(const string& c1, const string& c2); bool operator>(const string& c1, const string& c2); bool operator<=(const string& c1, const string& c2); bool operator>=(const string& c1, const string& c2); string operator+(const string& s1, const string& s2 ); string operator+(const char* s, const string& s2 ); string operator+( char c, const string& s2 ); string operator+( const string& s1, const char* s ); string operator+( const string& s1, char c ); ostream& operator<<( ostream& os, const string& s ); istream& operator>>( istream& is, string& s ); string& operator=( const string& s ); string& operator=( const char* s ); string& operator=( char ch ); char& operator[]( <strong>size_type</strong> index );</pre> <p>C++ strings can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Performing a comparison or assigning one string to another takes <a href="../complexity.html">linear time</a>.</p> <p>Two strings are equal if:</p> <pre class="example-code"> 1. Their size is the same, and 2. Each member in location i in one string is equal to the the member in location i in the other string. </pre> <p>Comparisons among strings are done lexicographically.</p> <p>In addition to these normal (C++ Multimaps) <a href= "../cppmultimap/multimap_operators.html">Multimap operators</a>, strings can also be concatenated with the + operator and fed to the C++ I/O stream classes with the << and >> operators.</p> <p>For example, the following code concatenates two strings and displays the result:</p> <pre class="example-code"> string s1 = "Now is the time..."; string s2 = "for all good men..."; string s3 = s1 + s2; cout << "s3 is " << s3 << endl; </pre> <p>Futhermore, strings can be assigned values that are other strings, character arrays, or even single characters. The following code is perfectly valid:</p> <pre class="example-code"> char ch = 'N'; string s; s = ch; </pre> <p>Individual characters of a string can be examined with the [] operator, which runs in <a href="../complexity.html">constant time</a>.</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> <a href="c_str.html">c_str</a><br> <a href="compare.html">compare</a><br> <a href="data.html">data</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -