⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.htm

📁 自编的一个String类
💻 HTM
📖 第 1 页 / 共 3 页
字号:
</table></div><h3><a name="storage"></a>Storage control</h3><div align="left"><table border="1" width="100%" cellpadding="2">  <tr>    <td width="50%" valign="top"><tt>uint size() const</tt></td>    <td width="50%" valign="top">the length of the String (does not include a trailing zero -    in most cases there isn't one)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint length() const</tt></td>    <td width="50%" valign="top">same as size</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint max_size() const</tt></td>    <td width="50%" valign="top">the maximum size of a String, I have set it to npos-1</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>void resize(uint n, char c = 0)</tt></td>    <td width="50%" valign="top">change the size of a String, either by truncating or filling    out with copies of character c (std does default separately)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint capacity() const</tt></td>    <td width="50%" valign="top">the total space allocated for a String (always &gt;= size())</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>void reserve(uint res_arg = 0)</tt></td>    <td width="50%" valign="top">change the capacity of a String to the maximum of res_arg and    size(). This may be an increase or a decrease in the capacity.</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>void clear()</tt></td>    <td width="50%" valign="top">erase the contents of the string</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>bool empty() const</tt></td>    <td width="50%" valign="top">true if the String is empty; false otherwise</td>  </tr></table></div><h3><a name="character"></a>Character access</h3><div align="left"><table border="1" width="100%" cellpadding="2">  <tr>    <td width="50%" valign="top"><tt>char operator[](uint pos) const</tt></td>    <td width="50%" valign="top">return the pos-th character; return 0 if pos = size()</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>char&amp; operator[](uint pos)</tt></td>    <td width="50%" valign="top">return a reference to the pos-th character; undefined if    pos&gt;=size() - I throw an exception. This reference may become invalid after almost any    manipulation of the String</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>char at(uint n) const</tt></td>    <td width="50%" valign="top">same as operator[] const</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>char&amp; at(uint n)</tt></td>    <td width="50%" valign="top">same as operator[]. Throw an exception of pos &gt;=size()</td>  </tr></table></div><h3><a name="editing"></a>The <em>editing</em> functions</h3><p>For conditions under which references and pointers to data are invalidated by thesefunctions see <a href="#realloc">policy on reallocation</a>.</p><div align="left"><table border="1" width="100%" cellpadding="2">  <tr>    <td width="50%" valign="top"><tt>String&amp; operator+=(const String&amp; rhs)</tt></td>    <td width="50%" valign="top">append rhs to a String</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; operator+=(const char* s)</tt></td>    <td width="50%" valign="top">append the c-string defined by s to a String</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; operator+=(char c)</tt></td>    <td width="50%" valign="top">append the character c to a String</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; append(const String&amp; str)</tt></td>    <td width="50%" valign="top">append str to a String</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; append(const String&amp; str, uint pos, uint    n)</tt></td>    <td width="50%" valign="top">append String(str,pos,n)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; append(const char* s, uint n)</tt></td>    <td width="50%" valign="top">append String(s,n)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; append(const char* s)</tt></td>    <td width="50%" valign="top">append String(s)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; append(uint n, char c)</tt></td>    <td width="50%" valign="top">append character c</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>void push_back(char c)</tt></td>    <td width="50%" valign="top">operator+=(c)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; assign(const String&amp; str)</tt></td>    <td width="50%" valign="top">replace the String by str (this function is not explicitly in    the standard)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; assign(const String&amp; str, uint pos, uint    n)</tt></td>    <td width="50%" valign="top">replace the String by String(str,pos,n)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; assign(const char* s, uint n)</tt></td>    <td width="50%" valign="top">replace the String by String(s, n)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; assign(const char* s)</tt></td>    <td width="50%" valign="top">replace the String by String(s)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; assign(uint n, char c)</tt></td>    <td width="50%" valign="top">replace the String by String(c)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; insert(uint pos1, const String&amp; str)</tt></td>    <td width="50%" valign="top">insert str before character pos1</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; insert(uint pos1, const String&amp; str, uint    pos2, uint n)</tt></td>    <td width="50%" valign="top">insert String(str,pos2,n) before character pos1</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; insert(uint pos, const char* s, uint n =    npos)</tt></td>    <td width="50%" valign="top">insert String(s,n) before character pos (std does default    separately)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; insert(uint pos, uint n, char c)</tt></td>    <td width="50%" valign="top">insert character c(s,n) before character pos</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; erase(uint pos = 0, uint n = npos)</tt></td>    <td width="50%" valign="top">erase characters starting at pos and continuing for n    characters or till the end of the String. This was originally called <em>remove</em></td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; replace(uint pos1, uint n1, const String&amp;    str)</tt></td>    <td width="50%" valign="top">erase(pos1,n1); insert(pos1,str)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; replace(uint pos1, uint n1, const String&amp;    str, uint pos2, uint n2)</tt></td>    <td width="50%" valign="top">erase(pos1,n1); insert(pos1,str,pos2,n2)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; replace(uint pos, uint n1, const char* s,    uint n2 = npos)</tt></td>    <td width="50%" valign="top">erase(pos,n1); insert(pos,s,n2); (std does default    separately)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>String&amp; replace(uint pos, uint n1, uint n2, char c)</tt></td>    <td width="50%" valign="top">erase(pos,n1); insert(pos,n2,c)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint copy(char* s, uint n, uint pos = 0) const</tt></td>    <td width="50%" valign="top">copy a maximum of n characters from a string starting at    position pos to memory starting at location given by s. Return the number of characters    copied. I assume that the program has already allocated space for the characters</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>void swap(String&amp;)</tt></td>    <td width="50%" valign="top">a.swap(b) swaps the contents of Strings a and b. The standard    also provides for a function swap(a,b) - see <a href="#binary">binary operators</a></td>  </tr></table></div><h3><a name="data"></a>Pointer to data</h3><div align="left"><table border="1" width="100%" cellpadding="2">  <tr>    <td width="50%" valign="top"><tt>const char* c_str() const</tt></td>    <td width="50%" valign="top">return a pointer to the contents of a String after appending    (char)0 to the String. This pointer will be invalidated by almost any operation on the    String</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>const char* data() const</tt></td>    <td width="50%" valign="top">return a pointer to the contents of a String. This pointer    will be invalidated by almost any operation on the String</td>  </tr></table></div><h3><a name="find"></a>The <em>find</em> functions</h3><div align="left"><table border="1" width="100%" cellpadding="2">  <tr>    <td width="50%" valign="top"><tt>uint find(const String&amp; str, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find the first location of str in a String starting at    position pos. The location is relative to the beginning of the parent String. Return <a    href="#static">String::npos</a> if not found</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find(const char* s, uint pos, uint n) const</tt></td>    <td width="50%" valign="top">find(String(s,n),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find(const char* s, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find(String(s),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find(const char c, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find(String(1,c),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint rfind(const String&amp; str, uint pos = npos) const</tt></td>    <td width="50%" valign="top">find the last location of str in a String starting at    position pos. ie begin the search with the first character of str at position pos of the    target String. The location is relative to the beginning of the parent String. Return <a    href="#static">String::npos</a> if not found</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint rfind(const char* s, uint pos, uint n) const</tt></td>    <td width="50%" valign="top">rfind(String(s,n),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint rfind(const char* s, uint pos = npos) const</tt></td>    <td width="50%" valign="top">rfind(String(s),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint rfind(const char c, uint pos = npos) const</tt></td>    <td width="50%" valign="top">rfind(String(1,c),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_of(const String&amp; str, uint pos = 0)    const</tt></td>    <td width="50%" valign="top">find first of any element in str starting at pos. Return <a    href="#static">String::npos</a> if not found</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_of(const char* s, uint pos, uint n) const</tt></td>    <td width="50%" valign="top">find_first_of(String(s,n),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_of(const char* s, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find_first_of(String(s),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_of(const char c, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find_first_of(String(1,c),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_of(const String&amp; str, uint pos = npos)    const</tt></td>    <td width="50%" valign="top">find last of any element in str starting at pos. Return <a    href="#static">String::npos</a> if not found</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_of(const char* s, uint pos, uint n) const</tt></td>    <td width="50%" valign="top">find_last_of(String(s,n),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_of(const char* s, uint pos = npos) const</tt></td>    <td width="50%" valign="top">find_last_of(String(s),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_of(const char c, uint pos = npos) const</tt></td>    <td width="50%" valign="top">find_last_of(String(1,c),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_not_of(const String&amp; str, uint pos =    0) const</tt></td>    <td width="50%" valign="top">find first of any element not in str starting at pos. Return <a    href="#static">String::npos</a> if not found</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_not_of(const char* s, uint pos, uint n)    const</tt></td>    <td width="50%" valign="top">find_first_not_of(String(s,n),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_not_of(const char* s, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find_first_not_of(String(s),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_first_not_of(const char c, uint pos = 0) const</tt></td>    <td width="50%" valign="top">find_first_not_of(String(1,c),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_not_of(const String&amp; str, uint pos =    npos) const</tt></td>    <td width="50%" valign="top">find last of any element not in str starting at pos. Return <a    href="#static">String::npos</a> if not found</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_not_of(const char* s, uint pos, uint n)    const</tt></td>    <td width="50%" valign="top">find_last_not_of(String(s,n),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_not_of(const char* s, uint pos = npos)    const</tt></td>    <td width="50%" valign="top">find_last_not_of(String(s),pos)</td>  </tr>  <tr>    <td width="50%" valign="top"><tt>uint find_last_not_of(const char c, uint pos = npos)    const</tt></td>    <td width="50%" valign="top">find_last_not_of(String(1,c),pos)</td>  </tr></table></div><h3><a name="substring"></a>The <em>substring</em> function</h3><div align="left"><table border="1" width="100%" cellpadding="2">  <tr>    <td width="50%" valign="top"><tt>String substr(uint pos = 0, uint n = npos) const</tt></td>    <td width="50%" valign="top">return String(*this, pos, n)</td>  </tr></table>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -