c++11 standard template (STL) (std::basic_istringstream) (2)

Defined in the header file <sstream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_istringstream;
(until C++11)
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_istringstream;
(since C++11)

Class template std::basic_istringstreamImplements input and output operations on string-based streams. It equivalently stores an instance of std::basic_string and performs input operations on it.

At a low level, this class actually wraps the raw string device of std::basic_stringbuf into the high-level interface of std::basic_istream. Provides std::basic_stringbufa complete interface to exclusive members.

Two specializations for common character types are also defined:

type definition
istringstream basic_istringstream<char>
wistringstream basic_istringstream<wchar_t>

member function

Construct a string stream

std::basic_istringstream<CharT,Traits,Allocator>::basic_istringstream

basic_istringstream() : basic_istringstream(ios_base::in) { }

(1) (since C++11)

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

(2) (until C++11)

explicit basic_istringstream( ios_base::openmode mode );

(since C++11)

explicit basic_istringstream( const std::basic_string<CharT,Traits,Allocator>& str,
                              ios_base::openmode mode = ios_base::in );

(3)

basic_istringstream( basic_istringstream&& other );

(4) (since C++11)

 Constructs a new string stream.

1) Default constructor. Constructs a new underlying string device with the default open mode.

2) Constructs a new underlying string device. Construct the underlying basic_stringbufobject with basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::in).

3) With stra copy of as the initial contents of the underlying string device. Construct the underlying basic_stringbufobject with basic_stringbuf<Char,Traits,Allocator>(str, mode | ios_base::in).

4) Move constructor. With move semantics, constructs othera string stream with state of .

parameter

str - the string used to initialize the content of the string stream
mode - Specifies the stream opening mode. It is a bitmask type and defines the following constants:
constant explain
app Seek to end of stream before each write
binary open in binary mode
in open for reading
out open for writing
trunk Discards the contents of the stream on open
ate Seeks to end of stream immediately after opening
other - another string stream to use as source

Notice

In short loops, such as when constructing a single-use basic_istringstreamobject for string conversion, the overhead can be significantly higher than calling str and reusing the same object.

call example

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    //1) 默认构造函数。以默认打开模式构造新的底层字符串设备。
    std::basic_istringstream<int> basic_istringstream1;

    //2) 构造新的底层字符串设备。
    //以 basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::in)
    //构造底层 basic_stringbuf 对象。
    std::basic_istringstream<char> basic_istringstream2(std::ios_base::in);

    //3) 以 str 的副本为底层字符串设备的初始内容。
    std::string string1 = "I am a handsome programmer";
    std::basic_istringstream<char> basic_istringstream3(string1, std::ios_base::in);
    std::cout << "basic_istringstream3: " << basic_istringstream3.rdbuf() << std::endl;
    basic_istringstream3.seekg(std::ios_base::beg);
    while (!basic_istringstream3.eof())
    {
        char ch;
        basic_istringstream3 >> ch;
        std::cout << ch;
    }
    std::cout << std::endl;

    basic_istringstream3.seekg(std::ios_base::beg);
    //4) 移动构造函数。用移动语义,构造拥有 other 的状态的字符串流。
    std::basic_istringstream<char> basic_istringstream4(std::move(basic_istringstream3));
    basic_istringstream4.seekg(std::ios_base::beg);
    std::cout << "basic_istringstream4: " << basic_istringstream4.rdbuf() << std::endl;

    return 0;
}

output

 

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132397106