STL之sstream的用法
STL之sstream的用法
说在前面:
库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。注意,使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。
使用时必须加上#include<sstream>
stringstream 对象用于输入一行字符串,以 空格 为分隔符把该行分隔开来
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;
int main()
{
string str= "hello world I am very happy!";
stringstream sstream(str); //sstream<<
while (sstream)
{
string substr;
sstream>>substr;
cout << substr <<" "; //也可vec.push_back(substr);
}
return 0;
}
// 输出详解:
hello world I am very happy!
// 大家可能会问这有什么用,如果配上栈结构便能将这个逆序输出:
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;
int main()
{
string str= "hello world I am very happy !",tmp;
stringstream sstream(str);
stack<string> s;
while(sstream>>tmp)
{
s.push(tmp);
}
while(!s.empty())
{
cout<<s.top();
s.pop();
if(s.size()!=0)
cout<<" ";
}
cout<<endl;
return 0;
}
// 输出结果:
! happy very am I world hello
类型转换
利用stringstream进行常见类型转换
(int、long、float、double、long double、long long)
/-----string转int-----/
string str= "123456789";
stringstream sstream(str);
int tmp;
sstream>>tmp;
cout<<tmp<<endl; // 输出结果123456789
/-----int转string-----/
int tmp=123456;
stringstream t;
string s;
t<<tmp;
t>>s;
cout<<s<<endl; // 输出结果123456
/-----int转char-----/
int tmp=123456;
stringstream t;
char s[10];
t<<tmp;
t>>s;
for(int i=0;i<strlen(s);i++)
cout<<s[i]; // 输出结果123456
/-----char转int-----/
stringstream t;
char s='8';
int tmp;
t<<s;
t>>tmp;
cout<<tmp;
return 0; // 输出8;
利用to_string() 进行常见类型转换, 返回值为string
函数原型:
(C++11才有这个函数)
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);功能:
将数值转化为字符串。返回对应的字符串。
例子:
s1=to_string(10.5); //double到string
s2=to_string(123); //int到string
s3=to_string(true); //bool到string#include <iostream>
#include <stack>
#include <sstream>
#include <cstring>
#include <iomanip>
using namespace std;
int main()
{
string s1,s2,s3;
s1=to_string(10.5); //double到string
s2=to_string(123);//int到string
s3=to_string(true);//bool到string
cout<<fixed<<showpoint<<setprecision(1);
cout<<s1<<" "<<s2<<" "<<s3<<endl;
return 0;
}
// 10.500000 123 1重复利用stringstream对象
如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;
在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。
#include <iostream>
#include <stack>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
stringstream stream;
int first;
string second;
stream<< "456"; //插入字符串
stream >> first; //转换成int
cout << first << endl;
stream.clear(); //在进行多次转换前,必须清除stream,否则第二个数据可能输出乱码
stream << "true"; //插入bool值
stream >> second; //提取出int
cout << second << endl;
return 0;
}
// 输出结果:
456
true
STL之sstream的用法的更多相关文章
- STL中mem_fun, mem_fun_ref用法
1.引言 先看一个STL中for_each的用法: #include <iostream> #include <vector> #include <algorithm&g ...
- STL中间set具体用法!!!!
1.关于set C++ STL 之所以得到广泛的赞誉,也被非常多人使用.不仅仅是提供了像vector, string, list等方便的容器,更重要的是STL封装了很多复杂的数据结构算法和大量经常使用 ...
- STL中map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
- STL中erase()的用法
erase()是STL提供的容器中比较常用的方法之一,它的功能是删除容器中的某些元素,其中它的函数原型如下: 1.有两个参数,且参数类型都是size_t型: string& erase ( s ...
- STL 之 map的用法
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- STL源代码学习--vector用法汇总
一.容器vector 使用vector你必须包含头文件<vector>: #include<vector> 型别vector是一个定义于namespace std内的templ ...
- STL中bitset的用法
终于又来写博客了 == bitset存储的是二进数位,就和一个bool性数组差不多.用法上和数组的操作方式也差不多. 每位只占一个字节,大大优化了空间,可以通过数组形式访问. bitset定义 可以用 ...
- STL之list容器用法
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...
随机推荐
- 7种你应该知道的JavaScript常见的错误
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/types-of-native-errors-in- ...
- GitHub 热点速览 Vol.11:回暖的 GitHub 迎来上千星的图片流项目
作者:HelloGitHub-小鱼干 摘要:连着两周成绩平平的 GitHub Trending 榜,终于和三月的天气一样进入全面变暖的模式,无论是本周刚开源搭乘 ocr 热点并获得 1,500+ st ...
- Java反射之对JavaBean的内省操作
上一篇我们说了Java反射之数组的反射应用 这篇我们来模拟实现那些javabean的框架(BeanUtils)的基本操作. [一] 什么是JavaBean JavaBean 是一种JAVA语言写成的可 ...
- oracle使用expdp定时备份数据库
目录 oracle使用expdp备份数据库 备份shell脚本 创建定时任务 oracle使用expdp备份数据库 备份shell脚本 #!/bin/sh #获取当前时间 BACKUPTIME=$(d ...
- Array.isArray() 判断是不是数组
var arr = new Array(); if(Array.isArray()){ console.log('yes') } else { conssole.log('no') }
- 人见人爱A-B 题解
参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法运算.(当然,大家都知道集合的定义,就是 ...
- Java注解 看这一篇就够了
注解 1.概念 注解:说明程序的.给计算机看的 注释:用文字描述程序的.给程序员看的 注解的定义:注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性 ...
- 3D游戏中各种空间变换到底是怎么回事
每一个游戏可以呈现炫丽效果的背后,需要进行一系列的复杂计算,同时也伴随着各种各样的顶点空间变换.渲染游戏的过程可以理解成是把一个个顶点经过层层处理最终转化到屏幕上的过程,本文就旨在说明,顶点是经过了哪 ...
- 懂一点Python系列——快速入门
本文面相有 一定编程基础 的朋友学习,所以略过了 环境安装.IDE 搭建 等一系列简单繁琐的事情. 一.Python 简介 Python 英文原意为 "蟒蛇",直到 1989 年荷 ...
- pycharm 更换pip镜像源为国内,解决下载慢的问题
参考链接:https://www.cnblogs.com/hkgov/p/7799078.html 官方源下载速度太慢,换成国内源会很快. 推荐清华的源:https://pypi.tuna.tsing ...