一、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. SpringCloud启动类指定扫描包路径

    //如果这个启动类所在的包路径隐藏的很深,则需要指定扫描包.否则默认扫描启动类所在的子包路径下 @SpringBootApplication(scanBasePackages="com.jo ...

  2. awk 正则匹配指定字段次数统计

    1. 文本数据 head 12315_industry_business.csv name,business,label,label_name 沧州光松房屋拆迁有限公司,旧房拆迁.改造:物业服务(依法 ...

  3. Spring AOP使用注解记录用户操作日志

    最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...

  4. python 简单图像识别--验证码

    python  简单图像识别--验证码 记录下,准备工作安装过程很是麻烦. 首先库:pytesseract,image,tesseract,PIL windows安装PIL,直接exe进行安装更方便( ...

  5. 嵌入式 printf函数

    来自:https://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html #include <stdio.h> #includ ...

  6. More than one file was found with OS independent path 錯誤

    More than one file was found with OS independent path 'lib/armeabi/libmrpoid.so',. 翻譯過來就是:在操作系統的獨立目錄 ...

  7. Linq to SQL -- Union All、Union、Intersect和Top、Bottom和Paging和SqlMethods

    Union All/Union/Intersect操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1. ...

  8. Python中文繁简体转换工具

    Openccpy ___ _____ __ ___ ___ ___ _____ __ __ / __`\/\ '__`\ /'__`\/' _ `\ /'___\ /'___\/\ '__`\/\ \ ...

  9. all-document

    1.memorymanagement-whitepaper J2SE5.0 JVM 垃圾回收器相关英文 链接: https://pan.baidu.com/s/1mzkMxuFE82sfeVOToMb ...

  10. thingsboard填坑之路

    因为thingsboard都是国外的资料,国内基本没有参考资料.所以,记录下来源码安装当中,遇到的问题. thingsboard官网源码安装连接: https://thingsboard.io/doc ...