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

📄 bas_0007.htm

📁 ARM编辑、编译软件
💻 HTM
📖 第 1 页 / 共 4 页
字号:
basic_string& 
<B>replace</B> (size_type pos, size_type n1, const charT* s);
basic_string&#38; 
<B>replace</B> (size_type pos, size_type n1, size_type n2, charT c);</PRE>
<UL><P>The <SAMP>replace</SAMP> function replaces selected elements of this string with an alternate set of elements.  All of these versions insert the new elements in place of <SAMP>n1</SAMP> elements in this string, starting at position <SAMP>pos</SAMP>.  They each throw an <SAMP>out_of_range</SAMP> exception if <SAMP>pos1 > size()</SAMP>and a <SAMP>length_error</SAMP> exception if the resulting string size exceeds <SAMP>max_size()</SAMP>.</P>
<P>The second version replaces elements of the original string with <SAMP>n2</SAMP> characters from string <SAMP>s</SAMP> starting at position <SAMP>pos2</SAMP>.  It will throw the <SAMP>out_of_range</SAMP> exception if <SAMP>pos2 > s.size().</SAMP>  The third variation of the function replaces elements in the original string with <SAMP>n2</SAMP> elements from the array pointed to by <SAMP>s</SAMP>.  The fourth version replaces elements in the string with elements from the array pointed to by <SAMP>s</SAMP>, up to, but not including, a <SAMP>traits::eos()</SAMP> character.  The fifth replaces <SAMP>n</SAMP> elements with <SAMP>n2</SAMP> repetitions of character <SAMP>c</SAMP>.</P>
</UL>
<PRE>basic_string&#38; 
<B>replace</B> (iterator i1, iterator i2, 
         const basic_string&#38; str);
basic_string&#38; 
<B>replace</B> (iterator i1, iterator i2, const charT* s,
         size_type n);
basic_string&#38; 
<B>replace</B> (iterator i1, iterator i2, const charT* s);
basic_string&#38;  
<B>replace</B> (iterator i1, iterator i2, size_type n,
         charT c);
template&#60;class InputIterator>
basic_string&#38;  
<B>replace</B> (iterator  i1,  iterator  i2,
         InputIterator j1, InputIterator j2);</PRE>
<UL><P>Replace selected elements of this string with an alternative set of elements.  All of these versions of <SAMP>replace</SAMP> require iterators <SAMP>i1</SAMP> and <SAMP>i2</SAMP> to be valid iterators on this string.  The elements specified by the range <SAMP>[i1, i2)</SAMP> are replaced by the new elements.</P>
<P>The first version shown here replaces with all members in <SAMP>str</SAMP>.  The second version starts at position <SAMP>i1</SAMP>, and replaces the next  <SAMP>n</SAMP> characters with <SAMP>n</SAMP> characters of the array pointed to by <SAMP>s</SAMP>.  The third variation replaces string elements with elements from the array pointed to by <SAMP>s</SAMP> up to, but not including, a<SAMP> traits::eos()</SAMP> character.  The fourth version replaces string elements with <SAMP>n</SAMP> repetitions of <SAMP>c</SAMP>.  The last variation shown here replaces string elements with the members specified in the range <SAMP>[j1, j2)</SAMP>.</P>
</UL>
<PRE>void <B>reserve</B> (size_type res_arg);</PRE>
<UL><P>Assures that the storage capacity is at least <SAMP>res_arg</SAMP>.</P>
</UL>
<PRE>void
<B>resize</B> (size_type n, charT c);
void
<B>resize</B> (size_type n);</PRE>
<UL><P>Changes the capacity of this string to <SAMP>n</SAMP>.  If the new capacity is smaller than the current size of the string, then it is truncated.  If the capacity is larger, then the string is padded with <SAMP>c </SAMP>characters.  The latter <SAMP>resize</SAMP> member pads the string with default characters specified by  <SAMP>traits::eos()</SAMP>.</P>
</UL>
<PRE>size type
<B>size</B> () const;</PRE>
<UL><P>Return the number of elements contained in this string.</P>
</UL>
<PRE>basic_string 
<B>substr</B> (size_type pos = 0, size_type n = npos) const;</PRE>
<UL><P>Returns a string composed of copies of the lesser of <SAMP>n</SAMP> and <SAMP>size()</SAMP> characters in this string starting at index <SAMP>pos</SAMP>.  Throws an out_of_range exception if <SAMP>pos &#60;= size().</SAMP></P>
</UL>
<PRE>void 
<B>swap</B> (basic_string&#38; s);</PRE>
<UL><P>Swaps the contents of this string with the contents of <SAMP>s</SAMP>.</P>
</UL>
<A NAME="Non-member Operators"><H3>Non-member Operators</H3></A>
<PRE>template&#60;class charT, class traits, class Allocator>
basic_string  
<B>operator+ </B>(const basic_string&#38;  lhs, const basic_string&#38; rhs);</PRE>
<UL><P>Returns a string of length <SAMP>lhs.size()  +  rhs.size()</SAMP>, where the first <SAMP>lhs.size()</SAMP> elements are copies of the  elements of <SAMP>lhs</SAMP>, and the next <SAMP>rhs.size()</SAMP> elements are copies of the elements of <SAMP>rhs</SAMP>.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
basic_string
<B>operator+</B> (const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
basic_string
<B>operator+</B> (charT lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
basic_string  
<B>operator+ </B>(const basic_string&#38;  lhs, const charT* rhs);
template&#60;class charT, class traits, class Allocator>
basic_string 
<B>operator+ </B>(const basic_string&#38; lhs, charT rhs);</PRE>
<UL><P>Returns a string that represents the concatenation of two string-like entities.  These functions return, respectively:</P>
<PRE>basic_string(lhs) + rhs</PRE>
<PRE>basic_string(1, lhs) + rhs
lhs + basic_string(rhs)
lhs + basic_string(1, rhs)
</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool  
<B>operator==</B> (const basic_string&#38; lhs, const basic_string&#38; rhs);</PRE>
<UL><P>Returns a boolean value of <SAMP>true</SAMP> if <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP> are equal, and <SAMP>false</SAMP> if they are not. Equality  is defined by the <SAMP>compare()</SAMP> member function.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool   
<B>operator==</B> (const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
bool  
<B>operator==</B> (const basic_string&#38; lhs, const charT* rhs);</PRE>
<UL><P>Returns a boolean value indicating whether <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP> are equal.  Equality is defined by the <SAMP>compare()</SAMP>  member function.  These functions return, respectively:</P>
<PRE>basic_string(lhs) == rhs</PRE>
<PRE>lhs == basic_string(rhs)</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool  
<B>operator!=</B>  (const basic_string&#38; lhs,
             const basic_string&#38; rhs);</PRE>
<UL><P>Returns a boolean value representing the inequality of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Inequality is defined by the <SAMP>compare()</SAMP> member function.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool 
<B>operator!=</B> (const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
bool  
<B>operator!=</B> (const basic_string&#38; lhs, const charT* rhs);</PRE>
<UL><P>Returns a boolean value representing the inequality of  <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Inequality is defined by the <SAMP>compare()</SAMP>  member function.  The functions return, respectively:</P>
<PRE>basic_string(lhs) != rhs</PRE>
<PRE>lhs != basic_string(rhs)</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool  
<B>operator&#60; </B>(const basic_string&#38; lhs, const basic_string&#38; rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical less-than relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Less-than is defined by the <SAMP>compare() </SAMP>member.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool 
<B>operator&#60;</B> (const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
bool  
<B>operator&#60;</B> (const basic_string&#38; lhs, const charT* rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical less-than relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Less-than is defined by the<SAMP> compare() </SAMP>member function.  These functions return, respectively:</P>
<PRE>basic_string(lhs) &#60; rhs</PRE>
<PRE>lhs &#60; basic_string(rhs)</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool  
<B>operator></B> (const basic_string&#38; lhs, const basic_string&#38; rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical greater-than relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Greater-than is defined by the<SAMP> compare()</SAMP> member function.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool 
<B>operator></B> (const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
bool 
<B>operator></B> (const basic_string&#38; lhs, const charT* rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical greater-than relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Greater-than is defined by the <SAMP>compare()</SAMP> member.  The functions return, respectively:</P>
<PRE>basic_string(lhs) > rhs</PRE>
<PRE>lhs > basic_string(rhs)</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool 
<B>operator&#60;=</B> (const basic_string&#38; lhs,
             const basic_string&#38; rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical less-than-or-equal relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Less-than-or-equal is defined by the <SAMP>compare()</SAMP> member function.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool   
<B>operator&#60;=</B> (const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
bool  
<B>operator&#60;=</B> (const basic_string&#38; lhs, const charT* rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical less-than-or-equal relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Less-than-or-equal is defined by the <SAMP>compare()</SAMP> member function.  These functions return, respectively:</P>
<PRE>basic_string(lhs) &#60;= rhs</PRE>
<PRE>lhs &#60;= basic_string(rhs)</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool  
<B>operator>= </B>(const basic_string&#38; lhs, const basic_string&#38; rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical greater-than-or-equal relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Greater-than-or-equal is defined by the <SAMP>compare()</SAMP> member function.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
bool 
<B>operator>= </B>(const charT* lhs, const basic_string&#38; rhs);
template&#60;class charT, class traits, class Allocator>
bool 
<B>operator>= </B>(const basic_string&#38; lhs, const charT* rhs);</PRE>
<UL><P>Returns a boolean value representing the lexigraphical  greater-than-or-equal  relationship of <SAMP>lhs</SAMP> and <SAMP>rhs</SAMP>.  Greater-than-or-equal is defined by the <SAMP>compare()</SAMP> member.  The functions return, respectively:</P>
<PRE>basic_string(lhs) >= rhs</PRE>
<PRE>lhs >= basic_string(rhs)</PRE></UL>
<PRE>template&#60;class charT, class traits, class Allocator>
istream&#38;
<B>operator>></B> (istream&#38; is, basic_string&#38; str);</PRE>
<UL><P>Reads<SAMP> str</SAMP>  from  <SAMP>is</SAMP> using <SAMP>traits::char_in</SAMP> until a <SAMP>traits::is_del()</SAMP> element  is read.  All elements read, except the delimiter, are placed in <SAMP>str</SAMP>.  After the read, the function returns <SAMP>is</SAMP>.</P>
</UL>
<PRE>template&#60;class charT, class traits, class Allocator>
ostream&#38;
<B>operator&#60;&#60; </B>(ostream&#38; os, const basic_string&#38; str);</PRE>
<UL><P>Writes all elements of <SAMP>str</SAMP> to <SAMP>os</SAMP> in order from first to last, using <SAMP>traits::char_out()</SAMP>.  After the write, the function returns <SAMP>os</SAMP>.</P>
</UL>
<A NAME="Non-member Function"><H3>Non-member Function</H3></A>
<PRE>template &#60;class Stream, class charT, class traits,
          class Allocator>
Stream&#38; 
<B>getline</B> (Stream&#38; is, basic_string&#38; str, charT delim);</PRE>
<UL><P>An unformatted input function that extracts characters from <SAMP>is</SAMP> into <SAMP>str</SAMP> until <SAMP>npos - 1</SAMP> characters are read, the end of the input sequence is reached, or the character read is <SAMP>delim</SAMP>.  The characters are read using <SAMP>traits::char_in()</SAMP>. </P>
</UL>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// string.cpp
//
 #include&#60;string>
 #include &#60;iostream.h>
 int main()
 {
   string test;
   //Type in a string over five characters long
   while(test.empty() ||  test.size() &#60;= 5)
   {
     cout &#60;&#60; "Type a string between 5 and 100 characters long. "
          &#60;&#60; endl;
     cin >> test;
   }
   //Test operator[] access
   cout &#60;&#60; "Changing the third character from " &#60;&#60; test[2] &#60;&#60; 
           " to * " &#60;&#60; endl;
   test[2] = '*';
   cout &#60;&#60; "now its: " &#60;&#60; test &#60;&#60; endl &#60;&#60; endl;
   //Try the insertion member function
   cout &#60;&#60; "Identifying the middle: ";
   test.insert(test.size() / 2, "(the middle is here!)");
   cout &#60;&#60; test &#60;&#60; endl &#60;&#60; endl;
   //Try replacement
   cout &#60;&#60; "I didn't like the word 'middle',so instead,I'll say:"            &#60;&#60; endl;</PRE>
<PRE>   test.replace(test.find("middle",0), 6, "center");
   cout &#60;&#60; test &#60;&#60; endl; 
   return 0;
 }
Output :
Type a string between 5 and 100 characters long.
roguewave
Changing the third character from g to *
now its: ro*uewave
Identifying the middle: ro*u(the middle is here!)ewave
I didn't like the word 'middle', so instead, I'll say:
ro*u(the center is here!)ewave</PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="all_7029.htm"><B><I>allocator</B></I></A></P>
<HR>
<A HREF="bac_0189.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="Bid_7861.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

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