一、int转string

1.c++11标准增加了全局函数std::to_string:

string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);

Example:

// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_string int main ()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(++++) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
return ;
}

Output:
pi is 3.141593
28 is a perfect number
2.采用sstream中定义的字符串流对象来实现
ostringstream os; //构造一个输出字符串流,流内容为空
int i = 12;
os << i; //向输出字符串流中输出int整数i的内容
cout << os.str() << endl; //利用字符串流的str函数获取流中的内容

二、string转int
1.可以使用std::stoi/stol/stoll等等函数
Example:

// stoi example
#include <iostream> // std::cout
#include <string> // std::string, std::stoi int main ()
{
std::string str_dec = "2001, A Space Odyssey";
std::string str_hex = "40c3";
std::string str_bin = "-10010110001";
std::string str_auto = "0x7f"; std::string::size_type sz; // alias of size_t
以上一句,需要特别说明,这个sz变量,是为了获取在转换过程中,转换了多少位的数据。如果不需要这个数据,可以设置为0
所以这个变量,也可以直接定义为 site_z sz; 不一定按上面的定义方法 int i_dec = std::stoi (str_dec,&sz);
int i_hex = std::stoi (str_hex,nullptr,);
int i_bin = std::stoi (str_bin,nullptr,);
int i_auto = std::stoi (str_auto,nullptr,); std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
std::cout << str_hex << ": " << i_hex << '\n';
std::cout << str_bin << ": " << i_bin << '\n';
std::cout << str_auto << ": " << i_auto << '\n'; return ;
}

Output:
2001, A Space Odyssey: 2001 and [, A Space Odyssey]
40c3: 16579
-10010110001: -1201
0x7f: 127
2.采用标准库中atoi函数,对于其他类型也都有相应的标准库函数,比如浮点型atof(),long型atol()等等
string s = "12";
int a = atoi(s.c_str());
3.采用sstream头文件中定义的字符串流对象来实现转换
istringstream is("12"); //构造输入字符串流,流的内容初始化为“12”的字符串
int i;
is >> i; //从is流中读入一个int整数存入i中

参考资料:
http://blog.csdn.net/chavo0/article/details/51038397

http://blog.csdn.net/na_beginning/article/details/53576123
---------------------
作者:HarryLi
来源:CSDN
原文:https://blog.csdn.net/u010510020/article/details/73799996
版权声明:本文为博主原创文章,转载请附上博文链接!

【C++】C++中int与string的相互转换的更多相关文章

  1. C++中int与string的相互转换【转】

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

  2. C++中int与string的相互转换

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

  3. qt中int与string的相互转换

    我经常搞错这个问题,一直以为整形int b可以直接使用函数toString呢! 但是在qtCreator中在整形后面不管怎么按点(可以自动提示)他就是不给我提示,我就纳闷了这样居然不行 百度了之后才知 ...

  4. java和js中int和String相互转换常用方法整理

    java中int和String的相互转换常用的几种方法: int  > String int i=10;String s="";第一种方法:s=i+""; ...

  5. C++中int与string的转化

    C++中int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀, ...

  6. JAVA中int与String类型的相互转换

    Java的int和String类型间互相转换,小功能但是经常用到,下面是几种实现的方法: 字符串类型String转换成整数int 1. int i = Integer.parseInt([String ...

  7. JAVA中int、String的类型转换

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  8. STL_string.【转】C++中int、string等常见类型转换

    ZC:#include <sstream> ZC:貌似还有 istringstream 和 ostringstream ... https://www.cnblogs.com/gaobw/ ...

  9. C++ 中 int 与string相互转换

    int -->  string 1.使用itoa()函数 将任意类型的数字变量转换为字串子变量. #include<stdio.h> #include<iostream> ...

随机推荐

  1. css格式

    <head> <style>body {color:#000000;font-weight:normal;} .c1{ color: #fff; background-colo ...

  2. Difference between ulimit, lsof, cat /proc/sys/fs/file-max

    https://unix.stackexchange.com/questions/476351/difference-between-ulimit-lsof-cat-proc-sys-fs-file- ...

  3. 深入理解 ORM框架

    1.ORM是什么? object Relational mapping 对象关系映射,是一种为了解决面向对象与关系型数据库存在不匹配现象的技术,简单说,orm通过描述对象和数据库之间映射的元数据,将程 ...

  4. promise 基础知识

    promise 基础知识 proise:1.Promise是异步编程的一种解决方案,它有三种状态,分别是pending-进行中.resolved-已完成.rejected-已失败2.创建实例//met ...

  5. Error in loadNamespace 的解决之道

    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])   在构建比较复杂的环 ...

  6. 图解Tomcat

  7. [IIS]win7下怎么安装IIS

  8. TensorFlow代码初识

    直接看代码 import tensorflow as tf # tf.Variable生成的变量,每次迭代都会变化, # 这个变量也就是我们要去计算的结果,所以说你要计算什么,你是不是就把什么定义为V ...

  9. 转:APP开发浅谈-Fiddler抓包详解

    原文地址:http://www.luoxudong.com/?p=306 Fiddler抓包工具在APP开发过程中使用非常频繁,对开发者理解HTTP网络传输原理以及分析定位网络方面的问题非常有帮助.今 ...

  10. HTML5 source标签:媒介元素定义媒介资源

    HTML5 source标签是一种媒介元素(比如 <video> 和 <audio>)来定义媒介资源.<source> 标签允许您规定可替换的视频/音频文件供浏览器 ...