string类型转换int类型
C++转换形式(C++11):
int main(int argc, char* argv[])
{
std::string str1 = "";
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);
// error: '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';*/
}
output:
std::stoi("") is
std::stoi("3.14159") is
std::stoi("31337 with words") is
同样, 可以使用 stol(long), stof(float), stod(double) 等.
string类型转换int类型的更多相关文章
- JS中String类型转换Date类型 并 计算时间差
JS中String类型转换Date类型 1.比较常用的方法,但繁琐,参考如下:主要使用Date的构造方法:Date(int year , int month , int day)<script& ...
- 03.枚举和string以及int类型之间的转换
练习1: 将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...
- 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智 ...
- golang 中string和int类型相互转换
总结了golang中字符串和各种int类型之间的相互转换方式: string转成int: int, err := strconv.Atoi(string)string转成int64: int64, e ...
- String,Integer,int类型之间的相互转换
String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...
- arduino 通过串口接收string,int类型数据
串口接收string类型数据源码如下 String comdata = ""; void setup() { Serial.begin(9600); } void lo ...
- String与Int类型的转换
http://blog.sina.com.cn/s/blog_4f9d6b1001000bfo.html int -> String int i=12345; String s="&q ...
- mysql varchar类型转换int类型找出最大值
(1) 不严谨的,最简单的 select MAX(字段名 + 0) from 表名; (2) 使用函数实现 select MAX(cast(字段名 as SIGNED INTEGER)) from 表 ...
随机推荐
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- KD-tree(2维)
用于动态插入以及求某点的最近点的距离(BZOJ2648,BZOJ2716) #include <cstdio> #include <cmath> #include <al ...
- valueOf和toString的区别
基本上所有的JavaScript数据类型都有valueOf(),toString()方法,null除外,这两个方法解决了JavaScript值运算和显示的问题 valueOf()会把数据类型转换成原始 ...
- C语言中函数的传入值与传出值
看到一个函数的原型后,怎么样一眼看出来哪个参数做输入哪个做输出? 函数传参如果传的是普通变量(不是指针)那肯定是输入型参数: 如果传指针就有 2 种可能性了,为了区别,经常的做法是: 如果这个参数是做 ...
- 如何使用PullToRefresh
这里有详解 PullToRefresh使用详解(一)--构建下拉刷新的listView PullToRefresh使用详解(二)---重写BaseAdapter实现复杂XML下拉刷新 PullToRe ...
- java多线程通信 例子
package com.cl.www.thread; public class NumberHolder { private Integer number = 0; // 增加number publi ...
- Python or JavaScript 实现多级评论
Python or JavaScript 实现多级评论 Python 实现 Js 实现
- html和css
html css 和js说明 1.什么是html? HTML(HyperText MarkUp Language)超文本标记语言,通过使用标记来描述文档结构和表现形式的一种语言,由浏览器进行解析,然后 ...
- PHP之:序列化和反序列化-serialize()和unserialize()
撰写日期:2016-7-7 10:56:40 参考PHP在线手册(php.net):http://php.net/manual/zh/function.serialize.php 1.序列化 seri ...