美文网首页
C++字符串与数字的转换

C++字符串与数字的转换

作者: 糖葫芦_4273 | 来源:发表于2018-12-13 00:48 被阅读0次

字符串转数字:
以下函数属于C++,需包含<string>

stod()//字符串转double
stof()//字符串转float
stoi()//字符串转int
stol()//字符串转long
stold()//字符串转double
stoll()//字符串转long long
stoul()//字符串转unsigned long
stoull()//字符串转unsinged long long
//注意!没有unsigned double和unsigned float!!!
//没有 (unsigned+浮点数) 这种类型!!!
//下面用stoi举例,其它类似
//第一个参数可以是string或者wstring
//第二个参数为stoi函数停止的位置
//第三个函数是待转换字符串中的进制
int stoi(const string &str, size_t *idx = (size_t *)nullptr, int base = 10)
int main()
{
    string s = "123456abcd";
    size_t pos;
    int a = stoi(s, &pos, 10);
    cout << pos << endl;
    //pos等于6,因为stoi()函数遇到a时停止的
}

数字转字符串:

//次函数的参数可以传任何数字类型
//int, unsigned int, long, unsigned long, long long, unsigned long long
//float, double
string to_string(待转换数字)

相关文章

  • C语言学习笔记

    C/C++格式化字符串说明 C++的格式化字符串经常用作格式化数字的输出、字符串合并和转换等等很多场合。 1. 格...

  • 关于JavaScript的隐式转换

    一、 数字与数字之间的转换 1.字符串加数字,数字就会转成字符串。 2.数字减字符串,字符串转成数字。如果字符串不...

  • 隐式转换

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 数据类型的转换

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 第三天

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 2018-07-11

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • ☝(•̀˓◞•́)哎呦

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • ☝(•̀˓◞•́)哎呦

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 转换.运算符

    一、隐式转换 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 谢老板

    1,隐式转换结论 数字+字符串:将数字转换为字符串 数字+boolean:将boolean转换为number类型 ...

网友评论

      本文标题:C++字符串与数字的转换

      本文链接:https://www.haomeiwen.com/subject/oelmhqtx.html