定义于头文件 <sstream>
template< class CharT, | (C++11 前) | |
template< class CharT, | (C++11 起) |
类模板 std::basic_istringstream
实现基于字符串的流上的输入与输出操作。它等效地存储一个 std::basic_string 的实例,并在其上进行输入操作。
在低层,该类实际上包装 std::basic_stringbuf 的未处理字符串设备到 std::basic_istream 的高层接口中。提供到独有 std::basic_stringbuf
成员的完整接口。
亦定义对于常用字符类型的二个特化:
类型 | 定义 |
istringstream | basic_istringstream<char> |
wistringstream | basic_istringstream<wchar_t> |
非成员函数
特化 std::swap 算法
std::swap(std::basic_istringstream)
template< class CharT, class Traits, class Alloc > void swap( std::basic_istringstream<CharT,Traits,Alloc>& lhs, std::basic_istringstream<CharT,Traits,Alloc>& rhs ); |
为 std::basic_istringstream 特化 std::swap 算法。交换 lhs
与 rhs
的状态。等效地调用 lhs.swap(rhs) 。
参数
lhs, rhs | - | 要交换状态的流 |
返回值
(无)
异常
(无)
调用示例
#include <sstream>
#include <string>
#include <iostream>int main()
{//3) 以 str 的副本为底层字符串设备的初始内容。std::string string1 = "I am a handsome programmer";std::basic_istringstream<char> basic_istringstream1(string1, std::ios_base::in);std::cout << "basic_istringstream1: " << basic_istringstream1.str() << std::endl;std::cout << "basic_istringstream1 rdstate: ";std::cout << basic_istringstream1.rdstate() << std::endl;std::string string2 = "I am a super handsome guy";std::basic_istringstream<char> basic_istringstream2(string2, std::ios_base::in);std::cout << "basic_istringstream2: " << basic_istringstream2.str() << std::endl;basic_istringstream2.setstate(std::ios_base::badbit);std::cout << "basic_istringstream2 rdstate: ";std::cout << basic_istringstream2.rdstate() << std::endl;std::cout << std::endl;//为 std::basic_istringstream 特化 std::swap 算法。//交换 lhs 与 rhs 的状态。等效地调用 lhs.swap(rhs)std::swap(basic_istringstream1, basic_istringstream2);std::cout << "after swap: " << std::endl;basic_istringstream1.seekg(std::ios_base::beg);std::cout << "basic_istringstream1: " << basic_istringstream1.str() << std::endl;std::cout << "basic_istringstream1 rdstate: ";std::cout << basic_istringstream1.rdstate() << std::endl;basic_istringstream2.seekg(std::ios_base::beg);std::cout << "basic_istringstream2: " << basic_istringstream2.str() << std::endl;std::cout << "basic_istringstream2 rdstate: ";std::cout << basic_istringstream2.rdstate() << std::endl;return 0;
}
输出