服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++ string与int的相互转换(使用C++11)

C++ string与int的相互转换(使用C++11)

2022-09-06 12:06李正浩大魔王 C/C++

本文主要介绍了C++ string与int的相互转换(使用C++11),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、int转string

C++ string与int的相互转换(使用C++11)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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

C++ string与int的相互转换(使用C++11)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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的相互转换内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_37316917/article/details/82712017

延伸 · 阅读

精彩推荐
  • C/C++C++ 17转发一个函数调用的完美实现

    C++ 17转发一个函数调用的完美实现

    这篇文章主要给大家介绍了关于C++ 17如何转发一个函数调用的完美实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C++17具有一定的参考...

    孙明琦5742021-05-31
  • C/C++c++加法高精度算法的简单实现

    c++加法高精度算法的简单实现

    下面小编就为大家带来一篇c++加法高精度算法的简单实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C++教程网10782021-04-16
  • C/C++VS2013安装配置和使用Boost库教程

    VS2013安装配置和使用Boost库教程

    这篇文章主要为大家详细介绍了VS2013安装配置和使用Boost库的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    yangykaifa9152021-05-08
  • C/C++C/C++ 开发神器CLion使用入门超详细教程

    C/C++ 开发神器CLion使用入门超详细教程

    这篇文章主要介绍了C/C++ 开发神器CLion使用入门超详细教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参...

    Garry111510902021-11-01
  • C/C++详解C++中的自动存储

    详解C++中的自动存储

    这篇文章主要介绍了详解C++中的自动存储,帮助大家更好的理解和学习C++,感兴趣的朋友可以了解下...

    chuyaoxin10262021-09-28
  • C/C++一个win32窗口创建示例

    一个win32窗口创建示例

    这篇文章主要介绍了一个win32窗口创建示例,需要的朋友可以参考下...

    C语言程序设计5412021-01-19
  • C/C++C语言、C++内存对齐问题详解

    C语言、C++内存对齐问题详解

    这篇文章主要介绍了C语言、C++内存对齐问题详解,内存对齐的问题主要存在于理解struct和union等复合结构在内存中的分布,需要的朋友可以参考下...

    果冻想6312021-02-05
  • C/C++C++动态内存分配(new/new[]和delete/delete[])详解

    C++动态内存分配(new/new[]和delete/delete[])详解

    这篇文章主要介绍了C++动态内存分配(new/new[]和delete/delete[])详解的相关资料,需要的朋友可以参考下...

    wangpengcsdn19502021-05-12