【整理】C/C++中字符串与整数之间的相互转换
2016-01-13 20:23
579 查看
最近刷题过程中总是碰到这个知识点,因此做个总结记录一下。
一、用C标准IO库中的sprintf()和sscanf()转换
sprintf()函数原型:
因此可利用sprintf()将数字转换为字符串。
sscanf()函数原型:
因此可利用sscanf()将字符串转换为数字。
示例代码:
二、用C++中的stringstream类转换
c++中的 <sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。
在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。
三、atoi、atof、atol、itoa等
C标准库<stdlib.h>提供了 atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, long, long long 型。
而非标准函数itoa、litoa()、ultoa()可以将int、long、unsigned long型整数转换为字符串类型。
atoi()函数的的原型为:int atoi(const char *nptr);
itoa()函数的原型为:char *itoa( int value, char *string,int radix);//value:欲转换的数据、string:目标字符串的地址、radix:转换后的进制数,可以取10,16,8,2等
示例:
注意:itoa并不是一个标准的C函数,不能在所有的编译器中使用,如果要写跨平台的程序,请用sprintf。
总结:三种方法的对比:
从效率对比来看:
下表是各种字串转数字函数对同一组(10万个)随机数字字串的处理时间:
可以看出atoi类的方法效率最高,sscanf次之,stringstream最慢。
从类型安全性来看:
atoi类的输入合法性难以检验,比如当无法转换时返回0,用户无法得知是真的为0,还是转换失败。
C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性,使得stringstream的转换拥有类型安全和不会溢出这样抢眼的特性。
假设你想用sprintf()函数将一个变量从int类型转换到字符串类型。为了正确地完成这个任务,你必须确保证目标缓冲区有足够大空间以容纳转换完的字符串。此外,还必须使用正确的格式化符。如果使用了不正确的格式化符,会导致非预知的后果。下面是一个例子:
而使用stringstream,由于n和s的类型在编译期就确定了,所以编译器拥有足够的信息来判断需要哪些转换。<sstream>库中声明的标准类就利用了这一点,自动选择所必需的转换。而且,转换结果保存在stringstream对象的内部缓冲中。你不必担心缓冲区溢出,因为这些对象会根据需要自动分配存储空间。
参考文章: http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html http://www.cnblogs.com/Anker/p/3351168.html
http://www.cppblog.com/mengkai/archive/2011/09/24/156718.aspx http://www.cplusplus.com/reference/sstream/stringstream/ http://blog.csdn.net/joeblackzqq/article/details/7032703 (超详细的关于stringstream的用法)
一、用C标准IO库中的sprintf()和sscanf()转换
sprintf()函数原型:
#include <stdio.h> int sprintf(char *str,const char *format);函数的功能是:将变量打印到字符串中。(与printf的用法一致,区别仅在于sprintf()打印到字符串,而printf()打印到标准输出)
因此可利用sprintf()将数字转换为字符串。
sscanf()函数原型:
#include <stdio.h> int sprintf(char *str, const char *format, ...);函数的功能是:将参数str的字符串转换成format对应的类型并将转换后的结果存于对应的变量内。(与scanf用法一致,区别在于scanf()从标准输入到参数,而sscanf从字符串输入到变量)
因此可利用sscanf()将字符串转换为数字。
示例代码:
#include <stdio.h> #include <string.h> int main(void) { char str1[10]="1234321"; char str2[10] = {}; int a; //string --> int sscanf(str1,"%d",&a); printf("%d\n",a); //int -->string a = 987654; sprintf(str2,"%d",a); puts(str2); return 0; }
二、用C++中的stringstream类转换
c++中的 <sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。
#include <iostream> #include <sstream> #include <string> using namespace std; int main(void) { string str1="1234321"; string str2; stringstream sstr; int a; //string --> int sstr << str1; sstr >> a; cout << a << endl; //int -->string sstr.clear();<span style="white-space:pre"> </span>//<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">通过使用同一stringstream对象实现多种类型的转换,每一次转换之后都必须调用clear()成员函数。</span> a = 987654; sstr << a; sstr >> str2; cout << str2 <<endl; return 0; }
在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。
三、atoi、atof、atol、itoa等
C标准库<stdlib.h>提供了 atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, long, long long 型。
而非标准函数itoa、litoa()、ultoa()可以将int、long、unsigned long型整数转换为字符串类型。
atoi()函数的的原型为:int atoi(const char *nptr);
itoa()函数的原型为:char *itoa( int value, char *string,int radix);//value:欲转换的数据、string:目标字符串的地址、radix:转换后的进制数,可以取10,16,8,2等
示例:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str1[10]="1234321"; char str2[10] = {}; int a; //string --> int a = atoi(str1); printf("%d\n",a); //int -->string a = 987654; itoa(a,str2,10); puts(str2); return 0; }
注意:itoa并不是一个标准的C函数,不能在所有的编译器中使用,如果要写跨平台的程序,请用sprintf。
总结:三种方法的对比:
从效率对比来看:
下表是各种字串转数字函数对同一组(10万个)随机数字字串的处理时间:
Method PerfCount [int] _tstoi() 34,802 [int] _stscanf() 233,264 [int] std::stringstream 1,553,689 [double] _tstof() 317,463 [double] _stscanf() 821,332 [double] std::stringstream 1,475,996
可以看出atoi类的方法效率最高,sscanf次之,stringstream最慢。
从类型安全性来看:
atoi类的输入合法性难以检验,比如当无法转换时返回0,用户无法得知是真的为0,还是转换失败。
C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性,使得stringstream的转换拥有类型安全和不会溢出这样抢眼的特性。
假设你想用sprintf()函数将一个变量从int类型转换到字符串类型。为了正确地完成这个任务,你必须确保证目标缓冲区有足够大空间以容纳转换完的字符串。此外,还必须使用正确的格式化符。如果使用了不正确的格式化符,会导致非预知的后果。下面是一个例子:
int n=10000; chars[10]; sprintf(s,”%d”,n);// s中的内容为“10000”到目前为止看起来还不错。但是,对上面代码的一个微小的改变就会使程序崩溃:
int n=10000; char s[10]; sprintf(s,”%f”,n);// <span style="font-size: 13.3333px;">错误地使用了%f格式化符来替代了%d。</span>在这种情况下,s在调用完sprintf()后包含了一个不确定的字符串。
而使用stringstream,由于n和s的类型在编译期就确定了,所以编译器拥有足够的信息来判断需要哪些转换。<sstream>库中声明的标准类就利用了这一点,自动选择所必需的转换。而且,转换结果保存在stringstream对象的内部缓冲中。你不必担心缓冲区溢出,因为这些对象会根据需要自动分配存储空间。
参考文章: http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html http://www.cnblogs.com/Anker/p/3351168.html
http://www.cppblog.com/mengkai/archive/2011/09/24/156718.aspx http://www.cplusplus.com/reference/sstream/stringstream/ http://blog.csdn.net/joeblackzqq/article/details/7032703 (超详细的关于stringstream的用法)
相关文章推荐
- c++ 时间类型详解(time_t和tm)
- c++11学习笔记2——稳定性和兼容性
- C语言转义字符相关知识
- C语言的静态函数
- C++ 里大写TRUE和小写true区别
- c语言基础知识
- 解决gcc的error: useless storage class specifier in empty declaration
- 内存对齐原则
- C++ MFC打开文件的流程
- C++11标准库chrono
- eclipse下用使用opencv用C++做图像处理
- 【转载】C++ Virtual详解
- 三种页面置换算法
- C++10中的移动语义
- [Leetcode 98] Validate Binary Search Tree
- C++类型转换
- 关键路径问题--完美版
- 【C++入门】简单的日期类操作
- c 语言鞍点问题处理
- C++学习之拷贝构造函数篇