大家好,又见面了,我是你们的朋友全栈君。
一 简介
stringstream可以很方便的进行数字与字符串的转换。
头文件<sstream>
template<
class CharT,
class Traits = std::char_traits<CharT>
> class basic_stringstream;
(until C++11)
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
> class basic_stringstream; (since C++11)
stringstream basic_stringstream<char>
派生层次:
(图片引用自cppreference),因此从std::ios_base等父类继承了大量成员函数。
二 例子
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
{
std::cout << std::endl;
std::cout << 1 << std::endl;
std::stringstream sm;
sm << 12345;
sm << "@163.com";
std::cout << "sm.str(): " << sm.str() << std::endl;
}
{
std::cout << std::endl;
std::cout << 2 << std::endl;
std::stringstream sm;
sm << "54321@163.com";
int i = 0;
sm >> i;
std::cout << "i: " << i << std::endl;
}
{
std::cout << std::endl;
std::cout << 3 << std::endl;
std::stringstream sm;
sm << std::setfill('0') << std::setw(4) << 1;
std::cout << "sm.str(): " << sm.str() << std::endl;
sm.str("");
sm << "0x" << std::hex << 123456;
std::cout << "sm.str(): " << sm.str() << std::endl;
}
std::cin.get();
return 0;
}
三 参考
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137430.html原文链接:https://javaforall.cn