C++string与int的相互转换(使用C++11)
一、int转string
#include <iostream>
#include <string>
int main()
{
double f = 23.43;
double f2 = 1e-9;
double f3 = 1e40;
double f4 = 1e-40;
double f5 = 123456789;
std::string f_str = std::to_string(f);
std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000"
std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40".
std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000"
std::string f_str5 = std::to_string(f5);
std::cout << "std::cout: " << f << '\n'
<< "to_string: " << f_str << "\n\n"
<< "std::cout: " << f2 << '\n'
<< "to_string: " << f_str2 << "\n\n"
<< "std::cout: " << f3 << '\n'
<< "to_string: " << f_str3 << "\n\n"
<< "std::cout: " << f4 << '\n'
<< "to_string: " << f_str4 << "\n\n"
<< "std::cout: " << f5 << '\n'
<< "to_string: " << f_str5 << '\n';
}
输出
std::cout: 23.43
to_string: 23.430000
std::cout: 1e-09
to_string: 0.000000
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
std::cout: 1e-40
to_string: 0.000000
std::cout: 1.23457e+08
to_string: 123456789.000000
二、string转int
#include <iostream>
#include <string>
int main()
{
std::string str1 = "45";
std::string str2 = "3.14159";
std::string str3 = "31337 with words";
std::string str4 = "words and 2";
int myint1 = std::stoi(str1);
int myint2 = std::stoi(str2);
int myint3 = std::stoi(str3);
// 错误: 'std::invalid_argument'
// int myint4 = std::stoi(str4);
std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
//std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}
结果:
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
C++string与int的相互转换(使用C++11)的更多相关文章
- C++语法小记---string和int的相互转换
string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...
- string与int的相互转换C++(转)
string与int之间的相互转换C++(转) #include<iostream> #include<string> #include<sstream> usin ...
- string和int的相互转换方法
string转为int string str = "100000"; stringstream ss; ss << str; int i; ss >> i; ...
- Java学习之String与int的相互转换
•String 转 int 两种方式 int a = Integer.parseInt(s);int b = Integer.valueOf(s).intValue(); 代码 public clas ...
- Java中String和Int的相互转换
一.将字串 String 转换成整数 intA. 有2个方法:1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([Strin ...
- string与int的相互转换以及把一个字符加入到string的末尾
#include "stdafx.h" #include<sstream> #include<string> #include<iostream> ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- std::string和int类型的相互转换(C/C++)
字符串和数值之前转换,是一个经常碰到的类型转换. 之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考: 优缺点:C++的stringstream智 ...
- String,Integer,int类型之间的相互转换
String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...
随机推荐
- 面试突击91:MD5 加密安全吗?
MD5 是 Message Digest Algorithm 的缩写,译为信息摘要算法,它是 Java 语言中使用很广泛的一种加密算法.MD5 可以将任意字符串,通过不可逆的字符串变换算法,生成一个唯 ...
- 一键体验 Istio
背景介绍 Istio 是一种服务网格,是一种现代化的服务网络层,它提供了一种透明.独立于语言的方法,以灵活且轻松地实现应用网络功能自动化.它是一种管理构成云原生应用的不同微服务的常用解决方案.Isti ...
- Ansible 批处理实战
软件简介 Ansible 是一款自动化运维工具,基于 Python 开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功 ...
- 最全iOS 上架指南
一.上架基本需求资料 1.苹果开发者账号(公司已有可以不用申请,需要开通开发者功能,每年 99 美元) 2.开发好的APP 二.证书 上架版本需要使用正式的证书 1.创建证书 Apple Develo ...
- 嵌入式-C语言基础:二维数组的地址写法
二维数组a的有关指针: 表示形式 含义 ...
- 【云原生 · Kubernetes】部署 kube-proxy 组件
个人名片: 因为云计算成为了监控工程师 个人博客:念舒_C.ying CSDN主页️:念舒_C.ying kube-proxy 运行在所有 worker 节点上,它监听 apiserver 中 se ...
- Codeforces Round #834 (Div. 3) A-G
比赛链接 A 题目 知识点:模拟. 确定开头字母,然后循环比较即可. 时间复杂度 \(O(n)\) 空间复杂度 \(O(n)\) 题解 #include <bits/stdc++.h> # ...
- SpringCloud(十一)- 秒杀 抢购
1.流程图 1.1 数据预热 1.2 抢购 1.3 生成订单 (发送订单消息) 1.4 订单入库 (监听 消费订单消息) 1.5 查看订单状态 1.6 支付 (获取支付链接 ) 1.7 支付成功 微信 ...
- FCOS论文复现:通用物体检测算法
摘要:本案例代码是FCOS论文复现的体验案例,此模型为FCOS论文中所提出算法在ModelArts + PyTorch框架下的实现.本代码支持FCOS + ResNet-101在MS-COCO数据集上 ...
- 【Java并发入门】03 互斥锁(上):解决原子性问题
原子性问题的源头是线程切换 Q:如果禁用 CPU 线程切换是不是就解决这个问题了? A:单核 CPU 可行,但到了多核 CPU 的时候,有可能是不同的核在处理同一个变量,即便不切换线程,也有问题. 所 ...