Click on the banner to return to the class reference home page.

basic_istringstream


basic_istringstreambasic_istreambasic_iosios_base

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <sstream> 
template<class charT, class traits = char_traits<charT>,
         class Allocator = allocator<void> >
class basic_istringstream
: public basic_istream<charT, traits>

Description

The template class basic_istringstream<charT,traits,Allocator> provides functionality to read from an array in memory. It supports reading objects of class basic_string<charT,traits,Allocator>. It 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.

Interface

template<class charT, class traits = char_traits<charT>,
         class Allocator = allocator<void> >
class basic_istringstream 
: public basic_istream<charT, traits> {

 public:

  typedef basic_stringbuf<charT, traits, Allocator>  sb_type;
  typedef basic_ios<charT, traits>                   ios_type;

  typedef basic_string<charT, traits, Allocator>     string_type;

  typedef traits                           traits_type;
  typedef charT                            char_type;
  typedef typename traits::int_type        int_type;
  typedef typename traits::pos_type        pos_type;
  typedef typename traits::off_type        off_type;

  explicit basic_istringstream(ios_base::openmode which = 
                               ios_base::in);

  explicit basic_istringstream(const string_type& str,
                               ios_base::openmode which =
                               ios_base::in);

  virtual ~basic_istringstream();

  basic_stringbuf<charT,traits,Allocator> *rdbuf() const;
  string_type str() const;

  void str(const string_type& str);

};

Types

char_type
int_type
ios_type
istringstream
off_type
pos_type
sb_type
string_type
traits_type
wistringstream

Constructors

explicit basic_istringstream(ios_base::openmode which =
                    ios_base::in);
explicit basic_istringstream(const string_type& str,
                    ios_base::openmode which =
                    ios_base::in);

Destructor

virtual ~basic_istringstream();

Member Functions

basic_stringbuf<charT,traits,Allocator>* 
rdbuf() const;
string_type 
str() const;
void 
str(const string_type& str);

Examples

//
// stdlib/examples/manual/istringstream.cpp
//
#include<iostream>
#include<sstream>
#include<string>
#include<iomanip>

void main ( )
{
  using namespace std;

  long   l= 20;
  wchar_t *ntbs=L"Il avait l'air heureux";
  wchar_t c;
  wchar_t buf[50];

  // create a read/write string-stream object on wide char
  // and attach it to an wistringstream object
  wistringstream in(ios_base::in | ios_base::out);

  // tie the ostream object to the wistringstream object
  wostream out(in.rdbuf());   

  // output ntbs in out
  out << ntbs;

  // output each word on a separate line
  while ( in.get(c) )
   {
     if ( c == L' ' ) 
      wcout << endl;
     else
      wcout << c;
   }
  wcout << endl << endl;

  // move back the input sequence to the beginning
  in.seekg(0); 

  // clear the state flags
  in.clear();

  // does the same thing as the previous code
  // output each word on a separate line
  while ( in >> buf )
   wcout << buf << endl; 
    
  wcout << endl << endl;

  // create a tiny string object
  string test_string("Il dormait pour l'eternite");

  // create a read/write string-stream object on char
  // and attach it to an istringstream object
  istringstream in_bis(ios_base:: in | ios_base::out |
                       ios_base::app );

  // create an ostream object
  ostream out_bis(in_bis.rdbuf());  

  // initialize the string buffer with test_string
  in_bis.str(test_string);

  out_bis << endl;

  // output the base info before each integer
  out_bis << showbase;

  ostream::pos_type pos= out_bis.tellp();

  // output l in hex with a field with of 20 
  out_bis << hex << setw(20) << l << endl;

  // output l in oct with a field with of 20
  out_bis << oct << setw(20) << l << endl;

  // output l in dec with a field with of 20
  out_bis << dec << setw(20) << l << endl;

  // output the all buffer
  cout << in_bis.rdbuf();

  // seek the input sequence to pos  
  in_bis.seekg(pos);

  int a,b,d;

  // read the previous outputted integer
  in_bis >> a >> b >> d;

  // output 3 times 20
  cout << a << endl << b << endl << d << endl;

}

See Also

char_traits(3C++), ios_base(3C++), basic_ios(3C++), basic_stringbuf(3C++), basic_string(3C++), basic_ostringstream(3C++), basic_stringstream(3C++)

Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.7.2

Standards Conformance

ANSI X3J16/ISO WG21 Joint C++ Committee


©Copyright 1996, Rogue Wave Software, Inc.