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容 ...
随机推荐
- SpringBoot入门系列(四)整合模板引擎Thymeleaf
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
- NLP(二十四)利用ALBERT实现命名实体识别
本文将会介绍如何利用ALBERT来实现命名实体识别.如果有对命名实体识别不清楚的读者,请参考笔者的文章NLP入门(四)命名实体识别(NER) . 本文的项目结构如下: 其中,albert_ ...
- sonarqube配置全指南,集成阿里巴巴p3c规范
环境准备 内置数据库 Sonar安装成功后,默认内置H2数据库,用于记录单次的扫描结果,对同一个project重复扫码,会覆盖之前的扫描记录,所以H2 数据库只应用于测试,不可以用于生产环境,那如果你 ...
- python社区要放弃了pip?版本信息里带警告很不寻常哦
pip是python的一个包管理器. 今天再查询Pip3 -V 时,除了正常的版本信息外,多了几行信息 WARNING: pip is being invoked by an old script w ...
- SQLi-Labs之1~6关 - 常规注入与盲注
年初五 财神入 第一关 联合注入 1.准备 2.加'截断 3.order by 判断查询列数 4.同上 5.联合查询判断字段位置 6.查数据库名 7.1 查表名 7.2 查列名 8.查数据 第二关 不 ...
- windows 安装 jenkins 自动化构建部署至linux服务器上
一.环境准备 1.git安装环境 参考链接 https://www.cnblogs.com/yuarvin/p/12500038.html 2.maven安装环境,包括jdk环境安装 参考链接 htt ...
- DOM解读
DOM解读 DOM概念 - document object model:文档对象模型 操作文档的一套方法,document是一个对象,是dom的顶级对象,属于window的一个对象,并且可以说是最出色 ...
- C语言程序设计(八) 数组
第八章 数组 //L8-1 #include <stdio.h> int main() { int score1, score2, score3, score4, score5; int ...
- C语言程序设计(六) 循环控制结构
第六章 循环控制结构 循环结构:需要重复执行的操作 被重复执行的语句序列称为循环体 计数控制的循环 条件控制的循环 当型循环结构 直到型循环结构 for while do-while while(循环 ...
- 使用 notepad 正则转换 字符串
一..在一堆字符串中找出某一个特定格式的字符串,例如如下 需要摘出 WMID_abc WMID_def WMID_ghi {"abc",WMID_abc,oid_abc} {&qu ...