📄 sstream.html
字号:
<HTML><HEAD><TITLE><sstream></TITLE></HEAD><BODY><H1><A NAME="<sstream>"><CODE><sstream></CODE></A></H1><HR><P>Include the <A HREF="lib_cpp.html#iostreams">iostreams</A>standard header <B><CODE><sstream></CODE></B>to define several template classes that supportiostreams operations onsequences stored in an allocated array object.Such sequences are easily converted to and from objects oftemplate class<CODE><A HREF="string2.html#basic_string">basic_string</A></CODE>.</P><PRE>namespace std {template<class Elem, class Tr = char_traits<Elem>, class Alloc = allocator<Elem> > class <B><A HREF="#basic_stringbuf">basic_stringbuf</A></B>;typedef basic_stringbuf<char> <B><A HREF="#stringbuf">stringbuf</A></B>;typedef basic_stringbuf<wchar_t> <B><A HREF="#wstringbuf">wstringbuf</A></B>;template<class Elem, class Tr = char_traits<Elem>, class Alloc = allocator<Elem> > class <B><A HREF="#basic_istringstream">basic_istringstream</A></B>;typedef basic_istringstream<char> <B><A HREF="#istringstream">istringstream</A></B>;typedef basic_istringstream<wchar_t> <B><A HREF="#wistringstream">wistringstream</A></B>;template<class Elem, class Tr = char_traits<Elem>, class Alloc = allocator<Elem> > class <B><A HREF="#basic_ostringstream">basic_ostringstream</A></B>;typedef basic_ostringstream<char> <B><A HREF="#ostringstream">ostringstream</A></B>;typedef basic_ostringstream<wchar_t> <B><A HREF="#wostringstream">wostringstream</A></B>;template<class Elem, class Tr = char_traits<Elem>, class Alloc = allocator<Elem> > class <B><A HREF="#basic_stringstream">basic_stringstream</A></B>;typedef basic_stringstream<char> <B><A HREF="#stringstream">stringstream</A></B>;typedef basic_stringstream<wchar_t> <B><A HREF="#wstringstream">wstringstream</A></B>; };</PRE><H2><A NAME="basic_stringbuf"><CODE>basic_stringbuf</CODE></A></H2><PRE>template <class Elem, class Tr = char_traits<Elem>, class Alloc = allocator<Elem> > class <B>basic_stringbuf</B> : public basic_streambuf<Elem, Tr> {public: typedef typename basic_streambuf<Elem, Tr>::char_type char_type; typedef typename basic_streambuf<Elem, Tr>::traits_type traits_type; typedef typename basic_streambuf<Elem, Tr>::int_type int_type; typedef typename basic_streambuf<Elem, Tr>::pos_type pos_type; typedef typename basic_streambuf<Elem, Tr>::off_type off_type; typedef Alloc <B><A HREF="#basic_stringbuf::allocator_type">allocator_type</A></B>; <B><A HREF="#basic_stringbuf::basic_stringbuf">basic_stringbuf</A></B>(ios_base::openmode mode = ios_base::in | ios_base::out); <B><A HREF="#basic_stringbuf::basic_stringbuf">basic_stringbuf</A></B>(const basic_string<Elem, Tr, Alloc>& str, ios_base::openmode mode = ios_base::in | ios_base::out); basic_string<Elem, Tr, Alloc> <B><A HREF="#basic_stringbuf::str">str</A></B>() const; void <B><A HREF="#basic_stringbuf::str">str</A></B>(const basic_string<Elem, Tr, Alloc>& newstr);protected: virtual pos_type <B><A HREF="#basic_stringbuf::seekoff">seekoff</A></B>(off_type off, ios_base::seekdir way, ios_base::openmode mode = ios_base::in | ios_base::out); virtual pos_type <B><A HREF="#basic_stringbuf::seekpos">seekpos</A></B>(pos_type sp, ios_base::openmode mode = ios_base::in | ios_base::out); virtual int_type <B><A HREF="#basic_stringbuf::underflow">underflow</A></B>(); virtual int_type <B><A HREF="#basic_stringbuf::pbackfail">pbackfail</A></B>(int_type meta = traits_type::eof()); virtual int_type <B><A HREF="#basic_stringbuf::overflow">overflow</A></B>(int_type meta = traits_type::eof()); };</PRE><P>The template classdescribes a <B><A HREF="streambu.html#stream buffer">stream buffer</A></B> that controlsthe transmission of elementsof type <CODE>Elem</CODE>, whose<A HREF="string2.html#character traits">character traits</A> are determined by theclass <CODE>Tr</CODE>,to and from a sequence of elementsstored in an array object. The object is allocated, extended, andfreed as necessary to accommodate changes in the sequence.</P><P>An object of class<CODE>basic_stringbuf<Elem, Tr, Alloc></CODE>stores a copy of the<CODE>ios_base::<A HREF="ios.html#ios_base::openmode">openmode</A></CODE>argument from its constructor as its<B><A NAME="stringbuf mode">stringbuf mode</A></B> <CODE>mode</CODE>:</P><UL><LI>If <CODE>mode &ios_base::<A HREF="ios.html#ios_base::in">in</A></CODE> is nonzero,the <A HREF="streambu.html#input buffer">input buffer</A>is accessible.</LI><LI>If <CODE>mode &ios_base::<A HREF="ios.html#ios_base::out">out</A></CODE> is nonzero,the <A HREF="streambu.html#output buffer">output buffer</A>is accessible.</LI></UL><H3><A NAME="basic_stringbuf::allocator_type"><CODE>basic_stringbuf::allocator_type</CODE></A></H3><PRE>typedef Alloc <B>allocator_type</B>;</PRE><P>The type is a synonym for the template parameter <CODE>Alloc</CODE>.</P><H3><A NAME="basic_stringbuf::basic_stringbuf"><CODE>basic_stringbuf::basic_stringbuf</CODE></A></H3><PRE><B>basic_stringbuf</B>(ios_base::openmode mode = ios_base::in | ios_base::out);<B>basic_stringbuf</B>(const basic_string<Elem, Tr, Alloc>& str, ios_base::openmode mode = ios_base::in | ios_base::out);</PRE><P>The first constructor stores a null pointer in all the pointerscontrolling the<A HREF="streambu.html#input buffer">input buffer</A> and the<A HREF="streambu.html#output buffer">output buffer</A>. Italso stores <CODE>mode</CODE> as the<A HREF="#stringbuf mode">stringbuf mode</A>.</P><P>The second constructor allocates a copy of the sequence controlledby the string object <CODE>str</CODE>.If <CODE>mode & ios_base::in</CODE> is nonzero,it sets the input buffer to begin readingat the start of the sequence.If <CODE>mode & ios_base::out</CODE> is nonzero,it sets the output buffer to beginwriting at the start of the sequence.It also stores <CODE>mode</CODE> as the<A HREF="#stringbuf mode">stringbuf mode</A>.</P><H3><A NAME="basic_stringbuf::char_type"><CODE>basic_stringbuf::char_type</CODE></A></H3><PRE>typedef Elem <B>char_type</B>;</PRE><P>The type is a synonym for the template parameter <CODE>Elem</CODE>.</P><H3><A NAME="basic_stringbuf::int_type"><CODE>basic_stringbuf::int_type</CODE></A></H3><PRE>typedef typename traits_type::int_type <B>int_type</B>;</PRE><P>The type is a synonym for<CODE>traits_type::<A HREF="string2.html#char_traits::int_type">int_type</A></CODE>.</P><H3><A NAME="basic_stringbuf::off_type"><CODE>basic_stringbuf::off_type</CODE></A></H3><PRE>typedef typename traits_type::off_type <B>off_type</B>;</PRE><P>The type is a synonym for<CODE>traits_type::<A HREF="string2.html#char_traits::off_type">off_type</A></CODE>.</P><H3><A NAME="basic_stringbuf::overflow"><CODE>basic_stringbuf::overflow</CODE></A></H3><PRE>virtual int_type <B>overflow</B>(int_type meta = traits_type::eof());</PRE><P>If <CODE>meta</CODE> does not compare equal to<CODE>traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>,the protected virtual member function endeavors to insert the element<CODE>traits_type::<A HREF="string2.html#char_traits::to_char_type">to_char_type</A>(meta)</CODE>into the<A HREF="streambu.html#output buffer">output buffer</A>.It can do so in various ways:</P><UL><LI>If a <A HREF="streambu.html#write position">write position</A>is available, it can store the element into the write positionand increment the next pointer for the output buffer.</LI><LI>It can make a write position available by allocatingnew or additional storage for the output buffer. (Extending theoutput buffer this way also extends any associated<A HREF="streambu.html#input buffer">input buffer</A>.)</LI></UL><P>If the function cannot succeed, it returns <CODE>traits_type::eof()</CODE>.Otherwise, it returns<CODE>traits_type::<A HREF="string2.html#char_traits::not_eof">not_eof</A>(meta)</CODE>.</P><H3><A NAME="basic_stringbuf::pbackfail"><CODE>basic_stringbuf::pbackfail</CODE></A></H3><PRE>virtual int_type <B>pbackfail</B>(int_type meta = traits_type::eof());</PRE><P>The protected virtual member function endeavors to put back an elementinto the<A HREF="streambu.html#input buffer">input buffer</A>,then make it the current element (pointed toby the next pointer).If <CODE>meta</CODE> compares equal to<CODE>traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>,the element to push back is effectively the one already in the streambefore the current element. Otherwise, that element is replaced by<CODE>byte =traits_type::<A HREF="string2.html#char_traits::to_char_type">to_char_type</A>(meta)</CODE>.The function can put back an element in various ways:</P><UL><LI>If a <A HREF="streambu.html#putback position">putback position</A>is available, and the element stored there compares equal to <CODE>byte</CODE>,it can simply decrement the next pointer for the input buffer.</LI><LI>If a putback position is available,and if the <A HREF="#stringbuf mode">stringbuf mode</A> permitsthe sequence to be altered (<CODE>mode &ios_base::<A HREF="ios.html#ios_base::out">out</A></CODE> is nonzero),it can store <CODE>byte</CODE> into the putback position and decrement thenext pointer for the input buffer.</LI></UL><P>If the function cannot succeed, it returns<CODE>traits_type::eof()</CODE>. Otherwise, it returns<CODE>traits_type::<A HREF="string2.html#char_traits::not_eof">not_eof</A>(meta)</CODE>.</P><H3><A NAME="basic_stringbuf::pos_type"><CODE>basic_stringbuf::pos_type</CODE></A></H3><PRE>typedef typename traits_type::pos_type <B>pos_type</B>;</PRE><P>The type is a synonym for<CODE>traits_type::<A HREF="string2.html#char_traits::pos_type">pos_type</A></CODE>.</P><H3><A NAME="basic_stringbuf::seekoff"><CODE>basic_stringbuf::seekoff</CODE></A></H3><PRE>virtual pos_type <B>seekoff</B>(off_type off, ios_base::seekdir way, ios_base::openmode mode = ios_base::in | ios_base::out);</PRE><P>The protected virtual member function endeavors to alter the currentpositions for the controlled streams. For an object of class<CODE>basic_stringbuf<Elem, Tr, Alloc></CODE>,a stream position consistspurely of a stream offset. Offset zero designates the first elementof the controlled sequence.</P><P>The new position is determined as follows:</P><UL><LI>If <CODE>way ==ios_base::<A HREF="ios.html#ios_base::beg">beg</A></CODE>,the new position is the beginning of the stream plus <CODE>off</CODE>.<LI>If <CODE>way ==ios_base::<A HREF="ios.html#ios_base::cur">cur</A></CODE>,the new position is the current stream position plus <CODE>off</CODE>.<LI>If <CODE>way ==ios_base::<A HREF="ios.html#ios_base::end">end</A></CODE>,the new position is the end of the stream plus <CODE>off</CODE>.</UL><P>If<CODE>mode & ios_base::in</CODE> is nonzero,the function alters the next position to read in the<A HREF="streambu.html#input buffer">input buffer</A>.If <CODE>mode & ios_base::out</CODE> is nonzero,the function alters the next position to write in the<A HREF="streambu.html#output buffer">output buffer</A>.For a stream to be affected, its buffer must exist.For a positioning operation to succeed, the resultingstream position must lie within the controlled sequence.If the function affects both stream positions, <CODE>way</CODE>must be <CODE>ios_base::beg</CODE> or <CODE>ios_base::end</CODE>and both streams are positioned at the same element.Otherwise (or if neither position is affected) the positioningoperation fails.</P><P>If the function succeeds in altering either or both of the stream positions,it returns the resultant stream position.Otherwise, it fails and returns an invalid stream position.</P><H3><A NAME="basic_stringbuf::seekpos"><CODE>basic_stringbuf::seekpos</CODE></A></H3><PRE>virtual pos_type <B>seekpos</B>(pos_type sp, ios_base::openmode mode = ios_base::in | ios_base::out);</PRE><P>The protected virtual member function endeavors to alter the currentpositions for the controlled streams. For an object of class<CODE>basic_stringbuf<Elem, Tr, Alloc></CODE>,a stream position consistspurely of a stream offset. Offset zero designates the first elementof the controlled sequence. The new position is determinedby <CODE>sp</CODE>.</P><P>If<CODE>mode & ios_base::in</CODE> is nonzero,the function alters the next position to read in the<A HREF="streambu.html#input buffer">input buffer</A>.If <CODE>mode & ios_base::out</CODE> is nonzero,the function alters the next position to write in the<A HREF="streambu.html#output buffer">output buffer</A>.For a stream to be affected, its buffer must exist.For a positioning operation to succeed, the resultingstream position must lie within the controlled sequence.Otherwise (or if neither position is affected) the positioningoperation fails.</P><P>If the function succeeds in altering either or both of the stream positions,it returns the resultant stream position.Otherwise, it fails and returns an invalid stream position.</P><H3><A NAME="basic_stringbuf::str"><CODE>basic_stringbuf::str</CODE></A></H3><PRE>basic_string<Elem, Tr, Alloc> <B>str</B>() const;void <B>str</B>(const basic_string<Elem, Tr, Alloc>& newstr);</PRE><P>The first member function returns an object of class<CODE><A HREF="string2.html#basic_string">basic_string</A><Elem, Tr, Alloc></CODE>,whose controlled sequence is a copy of the sequence controlledby <CODE>*this</CODE>. The sequence copied depends on the stored<A HREF="#stringbuf mode">stringbuf mode</A> <CODE>mode</CODE>:</P><UL><LI>If <CODE>mode &ios_base::<A HREF="ios.html#ios_base::out">out</A></CODE> is nonzeroand an <A HREF="streambu.html#output buffer">output buffer</A> exists,the sequence is the entire output buffer(<CODE><A HREF="streambu.html#basic_streambuf::epptr">epptr</A>() -<A HREF="streambu.html#basic_streambuf::pbase">pbase</A>()</CODE>elements beginning with <CODE>pbase()</CODE>).</LI><LI>Otherwise, if <CODE>mode &ios_base::<A HREF="ios.html#ios_base::in">in</A></CODE> is nonzeroand an <A HREF="streambu.html#input buffer">input buffer</A> exists,the sequence is the entire input buffer(<CODE><A HREF="streambu.html#basic_streambuf::egptr">egptr</A>() -<A HREF="streambu.html#basic_streambuf::eback">eback</A>()</CODE>elements beginning with <CODE>eback()</CODE>).</LI><LI>Otherwise, the copied sequence is empty.</LI></UL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -