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 ...
随机推荐
- 齐博X1-栏目的调用1
本节来说明下系统提供的几个栏目调用的方法 一节我们制作了一个公共导航,本节我们在首页index中演示下栏目的相关调用 至于其他的数据内容,参考第二季的标签调用即可,直接{qb:tag}调用就可以调用出 ...
- golang的内存管理
0.1.索引 https://blog.waterflow.link/articles/1663406367769 1.内存管理 内存管理是管理计算机内存的过程,在主存和磁盘之间移动进程以提高系统的整 ...
- python查找相似图片或重复图片
1.查找重复图片 利用文件的MD5值可查找完全一样的重复图片 import os,time,hashlib def getmd5(file): if not os.path.isfile(file): ...
- 第二阶段:高级核心基础知识·第4章shell特性·2
1.统计日志,日志内容 39.96.187.239 - - [11/Nov/2019:10:08:01 +0800] "GET / HTTP/1.1" 302 0 "-& ...
- 第2-1-3章 docker-compose安装FastDFS,实现文件存储服务
目录 4 docker-compose安装FastDFS 4.1 docker-compose-fastdfs.yml 4.2 nginx.conf 4.3 storage.conf 4.4 测试 4 ...
- jupyter初体验
安装: 1.若是已经安装了anaconda,则通过 jupyter notebook 命令进入: 2.若是只安了python: pip3 install --upgrade pip 对pip进行 ...
- Linux系统安装python
1. 安装python3 1.1 下载python3安装包及其依赖包(该步骤可忽略,步骤1.2 提供应用包链接) ① 在python官网下载所需的python3,或者用外网centos机器的wget命 ...
- HMM算法python实现
基础介绍,后5项为基础5元素 Q = ['q0', 'q1', 'q2', 'q3'] # 状态集合 States,共 N 种状态 V = ['v0', 'v1'] # 观测集合 Observatio ...
- 一次SpringBoot版本升级,引发的血案
前言 最近项目组升级了SpringBoot版本,由之前的2.0.4升级到最新版本2.7.5,却引出了一个大Bug. 到底是怎么回事呢? 1.案发现场 有一天,项目组的同事反馈给我说,我之前有个接口在新 ...
- mybatis实现数据行级权限拦截
最近在做一个测试平台,其中有一个需求是用户只能看到他有权限的项目数据.一开始这个需求只针对用例模块,我直接在sql后面加上了关联项目权限表.后面因为其他模块也需要这个权限判断,故打算把关联sql抽取出 ...