栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ ----to

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ ----to

1、to_string

包含在# include。作用是把数值类型如int、double、long等转化为string,

int a = 4;
double b = 3.14;
string str1, str2;
str1 = to_string(a);
str2 = to_string(b);
cout << str1 << endl;
cout << str2 << endl;

2、stoi和atoi

          包含在#include,不是c++11的可以在#include。作用是将字符串转化为int型。区别是stoi的形参是const string*,而atoi的形参是const char*。c_str()的作用是将const string*转化为const char*。

stoi()会做范围检查,默认范围是在int的范围内的,如果超出范围的话则runtime error 而atoi()不会做范围检查,如果超出范围的话,超出上界,则输出上界,超出下界,则输出下界;
string s1("1234567");
char* s2 = "1234567";
int a = stoi(s1);
int b = atoi(s2);
int c = atoi(s1.c_str());
cout << a << endl;
cout << b << endl;
cout << c << endl;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号