Library: Input/output
basic_istringstreambasic_istream
basic_ios
ios_base
|
allocator_type basic_istringstream() char_type |
int_type off_type pos_type |
rdbuf() str() traits_type |
~basic_istringstream() |
Class that supports reading objects of specializations of class template basic_string from an array in memory
#include <sstream>
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_istringstream;
}
The class template basic_istringstream reads from an array in memory. It supports reading objects of class basic_string, and uses a basic_stringbuf object to control the associated storage. It inherits from basic_istream, and therefore can use all the formatted and unformatted input functions.
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<void> >
class basic_istringstream
: public basic_istream<charT, traits>
{
public:
typedef traits traits_type;
typedef charT char_type;
typedef Allocator allocator_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
explicit
basic_istringstream(ios_base::openmode = ios_base::in);
explicit
basic_istringstream(const basic_string<char_type,
traits_type,
allocator_type>&,
ios_base::openmode = ios_base::in);
virtual ~basic_istringstream();
basic_stringbuf<char_type, traits_type, allocator_type> *
rdbuf() const;
basic_string<char_type, traits_type, allocator_type>
str() const;
void
str(const basic_string<char_type, traits_type,
allocator_type>&);
};
}
allocator_type
The type allocator_type is a synonym for the template parameter Allocator.
char_type
The type char_type is a synonym for the template parameter charT.
int_type
The type int_type is a synonym of type traits::in_type.
off_type
The type off_type is a synonym of type traits::off_type.
pos_type
The type pos_type is a synonym of type traits::pos_type.
traits_type
The type traits_type is a synonym for the template parameter traits.
istringstream
The type istringstream is an instantiation of class basic_istringstream on type char:
typedef basic_istringstream<char> istringstream;
wistringstream
The type wistringstream is an instantiation of class basic_istringstream on type wchar_t:
typedef basic_istringstream<wchar_t> wistringstream;
explicit basic_istringstream(ios_base::openmode which = ios_base::in);
Constructs an object of class basic_istringstream, initializing the base class basic_istream with the associated string buffer. The string buffer is initialized by calling the basic_stringbuf constructor:
basic_stringbuf<char_type,traits_type,
allocator_type>(which)
explicit
basic_istringstream(const basic_string<char_type, traits_type,
allocator_type>& str,
ios_base::openmode which = ios_base::in);
Constructs an object of class basic_istringstream, initializing the base class basic_istream with the associated string buffer. The string buffer is initialized by calling the basic_stringbuf constructor:
basic_stringbuf<char_type, traits_type,
allocator_type>(str,which)
virtual ~basic_istringstream();
Destroys an object of class basic_istringstream.
basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
Returns a pointer to the basic_stringbuf associated with the stream.
basic_string<char_type, traits_type, allocator_type> str() const;
Returns rdbuf()->str().
void
str(const basic_string<char_type, traits_type,
allocator_type>& str);
Calls rdbuf()->str(str).
//
// istringstream.cpp
//
#include <iomanip> // for setw
#include <iostream> // for wcout, dec, endl, hex, oct, wostream
#include <sstream> // for strstream, wistringstream
#include <string> // for wstring
int main ( )
{
try {
// create a read/write stream object
std::wistringstream in (std::ios::in | std::ios::out);
// associate the ostream object's buffer with
// the ifstream object
std::wostream out (in.rdbuf ());
// enable exceptions in both streams
in.exceptions (std::ios::badbit);
out.exceptions (std::ios::failbit | std::ios::badbit);
// write string into out
out << "Il avait l'air heureux." << std::endl;
// seek to the beginning of the stream
in.seekg (0);
wchar_t c;
// output each space-separated word on a separate line
while (in.get (c)) {
if (std::isspace (c, in.getloc ()))
std::wcout << std::endl;
else
std::wcout << c;
}
std::wcout << std::endl;
// clear stream state, failbit | eofbit set by
// last call to get ()
in.clear ();
// move back to the beginning of the stream
in.seekg (0);
wchar_t buf [40];
// guard against buffer overflow
in.width (sizeof buf);
// set right justification
std::wcout << std::right;
// does the same thing as the previous code
// output each word on a separate line
// in a field of width 10
while (in >> buf) {
std::wcout.width (10);
std::wcout << buf << std::endl;
}
std::wcout << std::endl;
// clear flags, last in >> buf set fail bit
// because of a newline at end of string
in.clear ();
// output the base info before each integer
out << std::showbase;
std::wostream::pos_type pos= out.tellp ();
const long l = 10;
// output l in hex with a field with of 10
out << std::hex << std::setw (10) << l << std::endl;
// output l in oct with a field with of 10
out << std::oct << std::setw (10) << l << std::endl;
// output l in dec with a field with of 10
out << std::dec << std::setw (10) << l << std::endl;
// move back to the beginning of the stream
in.seekg (0);
// output the entire stream
std::wcout << in.rdbuf () << std::endl;
// clear the flags
in.clear ();
// seek the input sequence to pos
in.seekg (pos);
in.unsetf (std::ios::basefield);
int a, b, d;
// read the previous outputted integer
in >> a >> b >> d;
// output 3 times 10
std::wcout << a << std::endl << b << std::endl
<< d << std::endl;
}
catch (std::ios::failure &e) {
std::wcerr << e.what () << std::endl;
}
return 0;
}
Program Output:
4848525670694967
0048FE1C
0048FE1C
0xa
012
10
10
10
10
char_traits, ios_base, basic_ios, basic_stringbuf, basic_string, basic_ostringstream, basic_stringstream
ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 27.7.2