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 ...
随机推荐
- Android应用插件式开发解决方法[转]
一.现实需求描述 一般的,一个Android应用在开发到了一定阶段以后,功能模块将会越来越多,APK安装包也越来越大,用户在使用过程中也没有办法选择性的加载自己需要的功能模块.此时可能就需要考虑如何分 ...
- WebView的应用 持续积累
在我的项目中载入网页时我们会用到WebView这个控件,关于这个控件的相关的比較有用的API在这里记录一下. 第一 webview 设置javascript可用, mWebView = (WebVi ...
- C#_MVC_ajax for form
在上一篇介绍MVC中的Ajax实现方法的时候,曾经提到了除了使用Ajax HTML Helper方式来实现之外,Jquery也是实现Ajax的另外一种方案. 通过get方法实现AJax请求 View ...
- 利用C语言结构体模拟一个简单的JavaBean
利用C语言模拟一个Javabean 仅封装了,“无参构造函数”,“带参构造函数”,"toString方法" #include <stdio.h> struct User ...
- posix thread概述(示例代码)
一个简单的alarm实例 errors.h头文件 #ifndef __ERRORS_H #define __ERORRS_H #include<stdio.h> #include<u ...
- CSS表格固定列宽
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HashMap多线程死循环问题
HashMap通常会用一个指针数组(假设为table[])来做分散所有的key,当一个key被加入时,会通过Hash算法通过key算出这个数组的下标i,然后就把这个<key, value> ...
- 【C语言】4-指针
直接引用 1. 回想一下,之前我们是如何更改某个变量的值? 我们之前是通过变量名来直接引用变量,然后进行赋值: char a; a = 10; 2. 看上去是很简单,其实程序内部是怎么操作的呢? ...
- 关于async和await的一些误区实例详解
转载自 http://www.jb51.net/article/53399.htm 这篇文章主要介绍了关于async和await的一些误区实例详解,有助于更加深入的理解C#程序设计,需要的朋友可以参考 ...
- asp中的几个取整函数fix(),int(),round()的用法
asp中的几个取整函数是:fix(),int(),round(); Int(number).Fix(number)函数返回数字的整数部分.number 参数可以是任意有效的数值表达式.如果 numbe ...