std::string和int类型的相互转换(C/C++)
字符串和数值之前转换,是一个经常碰到的类型转换。
之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考:
优缺点:C++的stringstream智能扩展,不用考虑字符数组长度等..;但C的性能高
有性能要求的推荐用C实现版本。
上测试实例:
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream> using namespace std;
int main()
{
//C++ method
{
//int -- string
stringstream stream; stream.clear(); //在进行多次转换前,必须清除stream
int iValue = ;
string sResult;
stream << iValue; //将int输入流
stream >> sResult; //从stream中抽取前面插入的int值
cout << sResult << endl; // print the string //string -- int
stream.clear(); //在进行多次转换前,必须清除stream
string sValue="";
int iResult;
stream<< sValue; //插入字符串
stream >> iResult; //转换成int
cout << iResult << endl;
} //C method
{
//int -- string(C) 1
int iValueC=;
char cArray[]="";//需要通过字符数组中转
string sResultC;
//itoa由于它不是标准C语言函数,不能在所有的编译器中使用,这里用标准的sprintf
sprintf(cArray, "%d", iValueC);
sResultC=cArray;
cout<<sResultC<<endl; //int -- string(C) 2
int iValueC2=;
string sResultC2;
//这里是网上找到一个比较厉害的itoa >> 后文附实现
sResultC2=itoa(iValueC2);
cout<<sResultC2<<endl; //string -- int(C)
string sValueC="";
int iResultC = atoi(sValueC.c_str());
cout<<iResultC+<<endl;
} return ;
}
test.cpp
如下是网上找到的一片比较经典的itoa实现!
#define INT_DIGITS 19 /* enough for 64 bit integer */ char *itoa(int i)
{
/* Room for INT_DIGITS digits, - and '\0' */
static char buf[INT_DIGITS + ];
char *p = buf + INT_DIGITS + ; /* points to terminating '\0' */
if (i >= ) {
do {
*--p = '' + (i % );
i /= ;
} while (i != );
return p;
}
else { /* i < 0 */
do {
*--p = '' - (i % );
i /= ;
} while (i != );
*--p = '-';
}
return p;
}
itoa.c
std::string和int类型的相互转换(C/C++)的更多相关文章
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)'
opencv报错: test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)' te ...
- String,Integer,int类型之间的相互转换
String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...
- golang 中string和int类型相互转换
总结了golang中字符串和各种int类型之间的相互转换方式: string转成int: int, err := strconv.Atoi(string)string转成int64: int64, e ...
- string类型转换int类型
C++转换形式(C++11): int main(int argc, char* argv[]) { std::"; std::string str2 = "3.14159&quo ...
- 03.枚举和string以及int类型之间的转换
练习1: 将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...
- arduino 通过串口接收string,int类型数据
串口接收string类型数据源码如下 String comdata = ""; void setup() { Serial.begin(9600); } void lo ...
- String与Int类型的转换
http://blog.sina.com.cn/s/blog_4f9d6b1001000bfo.html int -> String int i=12345; String s="&q ...
- [转] java中int,char,string三种类型的相互转换
原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...
随机推荐
- JSON 之FastJson解析
http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html 一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具 ...
- 会话跟踪session cookie
会话跟踪 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在 ...
- css中那些你可能没注意到的东西
1.inline元素,添加position:absolute;可定宽高,position:relative;则不行,不信你试试! 2.inline元素添加浮动后,不用加display:block;也可 ...
- 检测 NSObject 对象持有的强指针
在上一篇文章中介绍了 FBRetainCycleDetector 的基本工作原理,这一篇文章中我们开始分析它是如何从每一个对象中获得它持有的强指针的. 如果没有看第一篇文章这里还是最好看一下,了解一下 ...
- Java基础知识强化之IO流笔记68:Properties和IO流集合使用
1. Properties和IO流集合使用 这里的集合必须是Properties集合: public void load(Reader reader):把文件中的数据读取到集合中 public v ...
- Android_ViewPager
view1源代码及xml资源文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- Linux学习之路:shell变量(一)
一.变量的显示与设置 1.变量的显示运用echo命令 +$符号: 上图例子显示的是系统变量,咱们可以自己设置变量 2.设置变量运用“=”符号 设置了变量NIU 值为“niunai” 变量设置规则: ( ...
- mysql数据库常用语句3
一:查询指定数据库中所有的表名 数据库名:test select table_name from information_schema.tables where table_schema='test' ...
- jquery用on代替bind(),live(),delegate()的方法
Js的功能确实非常强大,奈何我一个php程序员一直在js上没有投入足够的精力去研究,每次遇到不会的都是去百度,以后有时间真的应该买本书系统的学习一下 // Bind $( "#members ...
- JFace中的表格型树TableTreeViewer
表格型树是用TableTreeViewer来实现,自从SWT下的TableTree被废弃之后,其扩展TableTreeViewer也成了鸡肋,不再被建议使用,既然Tree可以实现表格型树,那么其扩展T ...