📄 basic_string.html
字号:
<HTML><!-- -- Copyright (c) 1996-1999 -- Silicon Graphics Computer Systems, Inc. -- -- Permission to use, copy, modify, distribute and sell this software -- and its documentation for any purpose is hereby granted without fee, -- provided that the above copyright notice appears in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. Silicon Graphics makes no -- representations about the suitability of this software for any -- purpose. It is provided "as is" without express or implied warranty. -- -- Copyright (c) 1994 -- Hewlett-Packard Company -- -- Permission to use, copy, modify, distribute and sell this software -- and its documentation for any purpose is hereby granted without fee, -- provided that the above copyright notice appears in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. Hewlett-Packard Company makes no -- representations about the suitability of this software for any -- purpose. It is provided "as is" without express or implied warranty. -- --><Head><Title>basic_string<charT, traits, Alloc></Title><!-- Generated by htmldoc --></HEAD><BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" ALINK="#ff0000"> <IMG SRC="CorpID.gif" ALT="SGI" HEIGHT="43" WIDTH="151"> <!--end header--><BR Clear><H1>basic_string<charT, traits, Alloc></H1><Table CellPadding=0 CellSpacing=0 width=100%><TR><TD Align=left><Img src = "containers.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD><TD Align=right><Img src = "type.gif" Alt="" WIDTH = "194" HEIGHT = "39" ></TD></TR><TR><TD Align=left VAlign=top><b>Category</b>: containers</TD><TD Align=right VAlign=top><b>Component type</b>: type</TD></TR></Table><h3>Description</h3>The <tt>basic_string</tt> class represents a <A href="Sequence.html">Sequence</A> of characters. It contains all the usual operations of a <A href="Sequence.html">Sequence</A>, and, additionally,it contains standard string operations such as search and concatenation.<P>The <tt>basic_string</tt> class is parameterized by character type, and bythat type's <A href="character_traits.html">Character Traits</A>. Most of the time, however, there isno need to use the <tt>basic_string</tt> template directly. The types<tt>string</tt> and <tt>wstring</tt> are typedefs for, respectively,<tt>basic_string<char></tt> and <tt>basic_string<wchar_t></tt>.<P>Some of <tt>basic_string</tt>'s member functions use an unusual method ofspecifying positions and ranges. In addition to the conventionalmethod using iterators, many of <tt>basic_string</tt>'s member functions usea single value <tt>pos</tt> of type <tt>size_type</tt> to represent a position (inwhich case the position is <tt>begin() + pos</tt>, and many of<tt>basic_string</tt>'s member functions use two values, <tt>pos</tt> and <tt>n</tt>, torepresent a range. In that case <tt>pos</tt> is the beginning of the rangeand <tt>n</tt> is its size. That is, the range is <tt>[begin() + pos, begin() +pos + n)</tt>.<P>Note that the C++ standard does not specify the complexity of<tt>basic_string</tt> operations. In this implementation, <tt>basic_string</tt>has performance characteristics very similar to those of <tt><A href="Vector.html">vector</A></tt>:access to a single character is <i>O(1</i>), while copy and concatenationare <i>O(N</i>). By contrast, <tt><A href="Rope.html">rope</A></tt> has very different performancecharacteristics: most <tt><A href="Rope.html">rope</A></tt> operations have logarithmic complexity.<P>Note also that, according to the C++ standard, <tt>basic_string</tt> has veryunusual iterator invalidation semantics. Iterators may be invalidatedby <tt>swap</tt>, <tt>reserve</tt>, <tt>insert</tt>, and <tt>erase</tt> (and by functions that areequivalent to <tt>insert</tt> and/or <tt>erase</tt>, such as <tt>clear</tt>, <tt>resize</tt>,<tt>append</tt>, and <tt>replace</tt>). Additionally, however, the first call to<i>any</i> non-const member function, including the non-const version of<tt>begin()</tt> or <tt>operator[]</tt>, may invalidate iterators. (The intent ofthese iterator invalidation rules is to give implementors greaterfreedom in implementation techniques.) In this implementation,<tt>begin()</tt>, <tt>end()</tt>, <tt>rbegin()</tt>, <tt>rend()</tt>, <tt>operator[]</tt>, <tt>c_str()</tt>, and<tt>data()</tt> do not invalidate iterators. In this implementation,iterators are only invalidated by member functions that explicitlychange the string's contents.<h3>Example</h3><pre>int main() { string s(10u, ' '); // Create a string of ten blanks. const char* A = "this is a test"; s += A; cout << "s = " << (s + '\n'); cout << "As a null-terminated sequence: " << s.c_str() << endl; cout << "The sixteenth character is " << s[15] << endl; reverse(s.begin(), s.end()); s.push_back('\n'); cout << s;}</pre><h3>Definition</h3>Defined in the standard header <A href="string">string</A>.<h3>Template parameters</h3><Table border><TR><TH>Parameter</TH><TH>Description</TH><TH>Default</TH></TR><TR><TD VAlign=top><tt>charT</tt></TD><TD VAlign=top>The string's value type: the type of character it contains.</TD><TD VAlign=top> </TD></TR><TR><TD VAlign=top><tt>traits</tt></TD><TD VAlign=top>The <A href="character_traits.html">Character Traits</A> type, which encapsulates basic character operations.</TD><TD VAlign=top><tt>char_traits<charT></tt></TD></TR><TR><TD VAlign=top><tt>Alloc</tt></TD><TD VAlign=top>The string's allocator, used for internal memory management.</TD><TD VAlign=top><tt>alloc</tt></TD></tr></table><h3>Model of</h3><A href="RandomAccessContainer.html">Random Access Container</A>, <A href="Sequence.html">Sequence</A>.<h3>Type requirements</h3>In addition to the type requirements imposed by <A href="RandomAccessContainer.html">Random Access Container</A> and <A href="Sequence.html">Sequence</A>:<UL><LI><tt>charT</tt> is a POD ("plain ol' data") type.<LI><tt>traits</tt> is a <A href="character_traits.html">Character Traits</A> type whose value type is <tt>charT</tt></UL><h3>Public base classes</h3>None.<h3>Members</h3><Table border><TR><TH>Member</TH><TH>Where defined</TH><TH>Description</TH></TR><TR><TD VAlign=top><tt>value_type</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>The type of object, <tt>CharT</tt>, stored in the string.</TD></TR><TR><TD VAlign=top><tt>pointer</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Pointer to <tt>CharT</tt>.</TD></TR><TR><TD VAlign=top><tt>reference</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Reference to <tt>CharT</tt></TD></TR><TR><TD VAlign=top><tt>const_reference</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Const reference to <tt>CharT</tt></TD></TR><TR><TD VAlign=top><tt>size_type</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>An unsigned integral type.</TD></TR><TR><TD VAlign=top><tt>difference_type</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>A signed integral type.</TD></TR><TR><TD VAlign=top><tt>static const size_type npos</tt></TD><TD VAlign=top><tt>basic_string</tt></TD><TD VAlign=top>The largest possible value of type <tt>size_type</tt>. That is, <tt>size_type(-1)</tt>.</TD></TR><TR><TD VAlign=top><tt>iterator</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Iterator used to iterate through a string. A <tt>basic_string</tt> supplies <A href="RandomAccessIterator.html">Random Access Iterators</A>.</TD></TR><TR><TD VAlign=top><tt>const_iterator</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Const iterator used to iterate through a string.</TD></TR><TR><TD VAlign=top><tt>reverse_iterator</tt></TD><TD VAlign=top> <A href="ReversibleContainer.html">Reversible Container</A></TD><TD VAlign=top>Iterator used to iterate backwards through a string.</TD></TR><TR><TD VAlign=top><tt>const_reverse_iterator</tt></TD><TD VAlign=top> <A href="ReversibleContainer.html">Reversible Container</A></TD><TD VAlign=top>Const iterator used to iterate backwards through a string.</TD></TR><TR><TD VAlign=top><tt>iterator begin()</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Returns an <tt>iterator</tt> pointing to the beginning of the string.</TD></TR><TR><TD VAlign=top><tt>iterator end()</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Returns an <tt>iterator</tt> pointing to the end of the string.</TD></TR><TR><TD VAlign=top><tt>const_iterator begin() const</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Returns a <tt>const_iterator</tt> pointing to the beginning of the string.</TD></TR><TR><TD VAlign=top><tt>const_iterator end() const</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Returns a <tt>const_iterator</tt> pointing to the end of the string.</TD></TR><TR><TD VAlign=top><tt>reverse_iterator rbegin()</tt></TD><TD VAlign=top> <A href="ReversibleContainer.html">Reversible Container</A></TD><TD VAlign=top>Returns a <tt>reverse_iterator</tt> pointing to the beginning of the reversed string.</TD></TR><TR><TD VAlign=top><tt>reverse_iterator rend()</tt></TD><TD VAlign=top> <A href="ReversibleContainer.html">Reversible Container</A></TD><TD VAlign=top>Returns a <tt>reverse_iterator</tt> pointing to the end of the reversed string.</TD></TR><TR><TD VAlign=top><tt>const_reverse_iterator rbegin() const</tt></TD><TD VAlign=top> <A href="ReversibleContainer.html">Reversible Container</A></TD><TD VAlign=top>Returns a <tt>const_reverse_iterator</tt> pointing to the beginning of the reversed string.</TD></TR><TR><TD VAlign=top><tt>const_reverse_iterator rend() const</tt></TD><TD VAlign=top> <A href="ReversibleContainer.html">Reversible Container</A></TD><TD VAlign=top>Returns a <tt>const_reverse_iterator</tt> pointing to the end of the reversed string.</TD></TR><TR><TD VAlign=top><tt>size_type size() const</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Returns the size of the string.</TD></TR><TR><TD VAlign=top><tt>size_type length() const</tt></TD><TD VAlign=top><tt>basic_string</tt></TD><TD VAlign=top>Synonym for <tt>size()</tt>.</TD></TR><TR><TD VAlign=top><tt>size_type max_size() const</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Returns the largest possible size of the string.</TD></TR><TR><TD VAlign=top><tt>size_type capacity() const</tt></TD><TD VAlign=top><tt>basic_string</tt></TD><TD VAlign=top>See below.</TD></TR><TR><TD VAlign=top><tt>bool empty() const</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top><tt>true</tt> if the string's size is <tt>0</tt>.</TD></TR><TR><TD VAlign=top><tt>reference operator[](size_type n)</tt></TD><TD VAlign=top> <A href="RandomAccessContainer.html">Random Access Container</A></TD><TD VAlign=top>Returns the <tt>n</tt>'th character.</TD></TR><TR><TD VAlign=top><tt>const_reference operator[](size_type n) const</tt></TD><TD VAlign=top> <A href="RandomAccessContainer.html">Random Access Container</A></TD><TD VAlign=top>Returns the <tt>n</tt>'th character.</TD></TR><TR><TD VAlign=top><tt>const charT* c_str() const</tt></TD><TD VAlign=top><tt>basic_string</tt></TD><TD VAlign=top>Returns a pointer to a null-terminated array of characters representing the string's contents.</TD></TR><TR><TD VAlign=top><tt>const charT* data() const</tt></TD><TD VAlign=top><tt>basic_string</tt></TD><TD VAlign=top>Returns a pointer to an array of characters (not necessarily null-terminated) representing the string's contents.</TD></TR><TR><TD VAlign=top><tt>basic_string()</tt></TD><TD VAlign=top> <A href="Container.html">Container</A></TD><TD VAlign=top>Creates an empty string.</TD></TR><TR><TD VAlign=top><pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -