码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++中将string 类型与int类型的相互转换


    这里写目录标题

    • string 转换为int
      • 1.atoi
      • 2.strtol
      • 3.stoi
      • 4. stringstream
    • int 转换为string
      • 1.sprintf_s
      • 2.stringstream
      • 3.to_string

    string 转换为int

    1.atoi

    #include
    using namespace std;
    int main()
    {
    	int x = 0;
    	string str1 = "10";
    	string str2 = "-10";
    	string str3 = "10a";
    	x = atoi(str1.c_str());
    	cout << x << endl;
    	x = atoi(str2.c_str());
    	cout << x << endl;
    	x = atoi(str3.c_str());
    	cout << x << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    2.strtol

    long int strtol (const char str, char* endptr, int base);

    str 为要转换的字符串,endstr 为第一个不能转换的字符的指针,base 为字符串 str 所采用的进制。

     #include
    using namespace std;
    int main()
    {
    	int x = 0;
    	string str1 = "10";
    	string str2 = "-10";
    	string str3 = "10a";
    	x = strtol(str1.c_str(), nullptr, 10);
    	cout << x << endl;
    	x = strtol(str2.c_str(), nullptr, 10);
    	cout << x << endl;
    	x = strtol(str3.c_str(), nullptr, 10);
    	cout << x << endl;
    
    	return 0;
    }
    
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    3.stoi

    c11之后才有,需要引入 < string>

    #include
    #include
    using namespace std;
    int main()
    {
    	int x = 0;
    	string str1 = "10";
    	string str2 = "-10";
    	string str3 = "10a";
    	x = stoi(str1);
    	cout << x << endl;
    	x = stoi(str2);
    	cout << x << endl;
    	x = stoi(str3);
    	cout << x << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    4. stringstream

    需要引入< sstream>

    #include
    #include
    using namespace std;
    int stringToInt(const string& s)
    {
    	stringstream ss;
    	int result;
    	ss << s;
    	ss >> result;
    	return result;
    
    }
    int main()
    {
    	    string str1 = "10";
    	    string str2 = "-10";
    	    string str3 = "10a";
    		cout << stringToInt(str1)<<endl;
    		cout << stringToInt(str2)<<endl;
    		cout << stringToInt(str3)<<endl;
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    int 转换为string

    1.sprintf_s

    #include
    using namespace std;
    int main()
    {
    	int number = 10;
    	char buff[128] = { 0 };
    	sprintf_s(buff, 128, "%d",number);
    	cout << buff << endl;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    2.stringstream

    需要引入

    #include
    #include
    using namespace std;
    int main()
    {
    	int number = 10;
    	stringstream ss;
    	ss << number;
    	string str = ss.str();
    	cout << str << endl;
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    3.to_string

    c11之后才可用,需引入string

    #include
    #include
    using namespace std;
    int main()
    {
    	int number = 10;
    	string str =to_string(number);
    	cout << str << endl;
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

  • 相关阅读:
    Rust的高效易用日志库—tklog
    Netty10-WebSocket长连接开发
    Linux 应急响应命令总结,收藏版
    前端工作总结215-混入思路
    基于BiLSTM-CRF模型的分词、词性标注、信息抽取任务的详解,侧重模型推导细化以及LAC分词实践
    数据库定时备份winserver2012篇
    SSL OV证书和DV、EV证书的区别
    推荐系统笔记(十六):推荐系统图协同过滤的深入理解/
    Apisix-Ingress服务发现详解
    陇萃堂:CRM系统赋能业务人员以及渠道商,线上线下快速突破和拓展
  • 原文地址:https://blog.csdn.net/aoeaoao/article/details/126147649
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号