sscanf、sprintf、stringstream常见用法
转载自:https://blog.csdn.net/jllongbell/article/details/79092891
前言:
以前没有接触过stringstream这个类的时候,常用的字符串和数字转换函数就是sscanf和sprintf函数。开始的时候就觉得这两个函数应经很叼了,但是毕竟是属于c的。c++中引入了流的概念,通过流来实现字符串和数字的转换方便多了。在这里,总结之前的,并介绍新学的。
常见格式串:
%% 印出百分比符号,不转换。
%c 整数转成对应的 ASCII 字元。
%d 整数转成十进位。
%f 倍精确度数字转成浮点数。
%o 整数转成八进位。
%s 整数转成字符串。
%x 整数转成小写十六进位。
%X 整数转成大写十六进位。
%n sscanf(str, "%d%n", &dig, &n),%n表示一共转换了多少位的字符
sprintf函数
sprintf函数原型为 int sprintf(char *str, const char *format, ...)。作用是格式化字符串,具体功能如下所示:
(1)将数字变量转换为字符串。
(2)得到整型变量的16进制和8进制字符串。
(3)连接多个字符串。
int main(){
char str[] = { };
int data = ;
//将data转换为字符串
sprintf(str,"%d",data);
//获取data的十六进制
sprintf(str,"0x%X",data);
//获取data的八进制
sprintf(str,"0%o",data);
const char *s1 = "Hello";
const char *s2 = "World";
//连接字符串s1和s2
sprintf(str,"%s %s",s1,s2);
cout<<str<<endl;
return ;
}
sscanf函数
sscanf函数原型为int sscanf(const char *str, const char *format, ...)。将参数str的字符串根据参数format字符串来转换并格式化数据,转换后的结果存于对应的参数内。具体功能如下:
(1)根据格式从字符串中提取数据。如从字符串中取出整数、浮点数和字符串等。
(2)取指定长度的字符串
(3)取到指定字符为止的字符串
(4)取仅包含指定字符集的字符串
(5)取到指定字符集为止的字符串
当然,sscanf可以支持格式串"%[]"形式的,有兴趣的可以研究一下。
int main(){
char s[] = "123.432,432";
int n;
double f1;
int f2;
sscanf(s, "%lf,%d%n", &f1, &f2, &n);
cout<<f1<<" "<<f2<<" "<<n;
return ;
}
输出结果:123.432 432 11, 即一共转换了11位的字符。
vstringstream类:
<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。
1.stringstream::str(); returns a string object with a copy of the current contents of the stream.
2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.
3.stringstream清空,stringstream s; s.str("");
4.实现任意类型的转换
template<typename out_type, typename in_value>
out_type convert(const in_value & t){
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}
int main(){
string s = "1 23 # 4";
stringstream ss;
ss<<s;
while(ss>>s){
cout<<s<<endl;
int val = convert<int>(s);
cout<<val<<endl;
}
return ;
}
输出:1 1 23 23 # 0 4 4
sscanf、sprintf、stringstream常见用法的更多相关文章
- stringstream常见用法介绍
1 概述 <sstream> 定义了三个类:istringstream.ostringstream 和 stringstream,分别用来进行流的输入.输出和输入输出操作.本文以 stri ...
- sscanf,sprintf(思修课的收获)
转载的,就是做个笔记 sprintf函数原型为 int sprintf(char *str, const char *format, ...).作用是格式化字符串,具体功能如下所示: (1)将数字变量 ...
- sprintf()函数的用法
Visual C++ sprintf()函数用法 转:http://blog.csdn.net/masikkk/article/details/5634886 在将各种类型的数据构造成字符串时,spr ...
- ACM--string常见用法
在ACM竞赛中,常常需要将读入的数字的每位分离出来,如果采用取余的方法,花费的时间就会太长,这时候,我们可以将读入的数据当成字符串来处理,这样就方便.省时多了.下面这个程序演示了求一个整数各位的和: ...
- sscanf(),sscanf_s()的相关用法
#include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明 sscanf ...
- sprintf,snprintf的用法(可以作为linux中itoa函数的补充)【转】
转自:http://blog.csdn.net/educast/article/details/25068445 函数功能:把格式化的数据写入某个字符串 头文件:stdio.h 函数原型:int sp ...
- C++ stringstream的用法
Created at stringstream的用法 使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h&g ...
- Linux中find常见用法
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- php中的curl使用入门教程和常见用法实例
摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...
随机推荐
- mongoDB 安装和配置环境变量,超详细版本
下载mongoDB进行安装:https://www.mongodb.com/ 到Community Se ...
- [转帖]50个必知的Linux命令技巧,你都掌握了吗?
50个必知的Linux命令技巧,你都掌握了吗? https://blog.51cto.com/lizhenliang/2131141 https://blog.51cto.com/lizhenlian ...
- Vue2.0 子组件和父组件之间的传值
Vue是一个轻量级的渐进式框架,对于它的一些特性和优点在此就不做赘述,本篇文章主要来探讨一下Vue子父组件通信的问题 首先我们先搭好开发环境,我们首先得装好git和npm这两个工具(如果有不清楚的同学 ...
- Flutter 常用工具类库common_utils
地址:https://pub.flutter-io.cn/packages/common_utils#-readme-tab- Dart常用工具类库 common_utils 1.TimelineUt ...
- git上传本地代码到github
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小 ...
- 建议2---编写pythonic代码
(1)要避免劣化代码 1)避免只用大小写来区分不同的对象.如a是一个数值类型变量,A是String类型,虽在编码过程容易区分二者的含义,但这样做毫无益处,它不会给其他阅读代码的人带来多少便利. 2)避 ...
- loadrunner 事务、同步点和思考时间
事务 在LoadRunner里,我们定义事务主要是为了度量服务器的性能.每个事务度量服务器响应指定的Vuser请求所有的时间,这些请求可以是简单任务,也可以是复杂任务. 要度量事务,需要插入Vuser ...
- python time模块介绍(日期格式化 时间戳)
import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...
- 文件上传.ashx
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime ...
- react用class关键字来创建组件
创建组件之前,首先学习一个ES6的写法,叫做展开运算符. 比如我这里有两个数组.如何将第二个数组o2中的所有属性导入到数组o1中呢?一个个输太麻烦,所以就用到了展开运算符. var o2={ age: ...