Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib basic_istringstream
Personal tools
Document Actions

basic_istringstream



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

    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.

ios_type

    The type ios_type is an instantiation of class basic_ios on type charT.

istringstream

    The type istringstream is an instantiation of class basic_istringstream on type char:

    typedef basic_istringstream<char> istringstream;

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.

sb_type

    The type sb_type is an instantiation of class basic_stringbuf on type charT.

string_type

    The type string_type is an instantiation of class basic_string on type charT.

traits_type

    The type traits_type is a synonym for the template parameter traits.

wistringstream

    The type wistringstream is an instantiation of class basic_istringstream on type wchar_t:

    typedef basic_istringstream<wchar_t> wistringstream;

Constructors

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<charT,traits,Allocator>(which).

explicit basic_istringstream(const string_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<charT,traits,Allocator>(str,which).

Destructor

virtual ~basic_istringstream();

    Destroys an object of class basic_istringstream.

Member Functions

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

    Returns a pointer to the basic_stringbuf associated with the stream.

string_type 
str() const;

    Returns a string object of type string_type, which contains a copy of the underlying buffer contents.

void 
str(const string_type& str);

    Clears the string buffer and copies the string object str into it. If the opening mode is in, initialize the input sequence to point at the first character of the buffer. If the opening mode is out, initialize the output sequence to point at the first character of the buffer. If the opening mode is out | app, initialize the output sequence to point at the last character of the buffer.

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.


Powered by Plone This site conforms to the following standards: