字符串和数值之前转换,是一个经常碰到的类型转换。

之前字符数组用的多,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++)的更多相关文章

  1. C++ 中 string, char*, int 类型的相互转换

    一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...

  2. 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 ...

  3. String,Integer,int类型之间的相互转换

    String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...

  4. golang 中string和int类型相互转换

    总结了golang中字符串和各种int类型之间的相互转换方式: string转成int: int, err := strconv.Atoi(string)string转成int64: int64, e ...

  5. string类型转换int类型

    C++转换形式(C++11): int main(int argc, char* argv[]) { std::"; std::string str2 = "3.14159&quo ...

  6. 03.枚举和string以及int类型之间的转换

    练习1:   将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...

  7. arduino 通过串口接收string,int类型数据

    串口接收string类型数据源码如下 String comdata = ""; void setup() {     Serial.begin(9600); }   void lo ...

  8. String与Int类型的转换

    http://blog.sina.com.cn/s/blog_4f9d6b1001000bfo.html int -> String int i=12345; String s="&q ...

  9. [转] java中int,char,string三种类型的相互转换

    原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...

随机推荐

  1. Linux开发环境搭建与使用——ubuntu更新设置

    ubuntu操作系统公布时,为了减小操作系统的体积,只配备了主要的系统软件.应用软件.我们开发中须要用到的大部分软件都须要在使用中从网上自行更新. 假设ubuntu没有网络,能够说寸步难行. 以下教大 ...

  2. Unescape HTML entities in Javascript Unescape HTML转成html代码

    前言: 在javascript里面动态创建标准dom对象一般使用: var obj = document.createElement('div'); 然后再给obj设置一些属性. 但是,在实际使用过程 ...

  3. UNIX标准化及实现之标准之间的冲突

    就整体而言,这些不同的标准之间配合得相当好.但是我们也很关注它们之间的差别,特别是ISO C标准和POSIX.1之间的差别. ISO C定义了函数clock,它返回进程使用的CPU时间,返回值类型是c ...

  4. 文件和目录之link、unlink、remove和rename函数

    任何一个文件可以有多个目录项指向其i节点.创建一个指向现有文件的链接的方法是使用link函数. #include <unistd.h> int link( const char *exis ...

  5. QT5中如何自定义窗口部件

    提升法 eg.(定义一个新的QLable部件)1.定义一个类class Label : public base, public QLabel //可以支持多重继承2.在qt creator中打开ui编 ...

  6. java_客户端防表单重复提交和服务器端session防表单重复提交

    用户输入FormServlet链接 FormServlet-〉form.jsp->DoFormServlet FormServlet:产生token,放在session中 form.jsp:hi ...

  7. SVN服务器搭建和使用(二)

      上一篇介绍了VisualSVN Server和TortoiseSVN的下载,安装,汉化.这篇介绍一下如何使用VisualSVN Server建立版本库,以及TortoiseSVN的使用. 首先打开 ...

  8. Java栈实现

    栈数组实现一:优点:入栈和出栈速度快,缺点:长度有限(有时候这也不能算是个缺点) public class Stack { private int top = -1; private Object[] ...

  9. 关于jQuery源码中(function(window,undefined){//dosomething()})(window)写法解释

    一.首先是最常见的闭包 (Closure) 范式自执行函数的写法,这里用匿名函数封装(构造块级作用域),避免了匿名函数内部的代码与外部之间发生冲突(如使用了相同的变量名). (function() { ...

  10. JFace中的表格型树TableTreeViewer

    表格型树是用TableTreeViewer来实现,自从SWT下的TableTree被废弃之后,其扩展TableTreeViewer也成了鸡肋,不再被建议使用,既然Tree可以实现表格型树,那么其扩展T ...