当前位置: 首页 > article >正文

c语言、c++怎么将string类型数据转成int,怎么将int转成string

string→int

C++
法一:
string str=“456”;

int num=stoi(str);

法二:
#include
int main() {
string str = “456”;

stringstream ss(str);

int num;

ss >> num;

}

C语言
#include <stdlib.h>
int main() {
char str[] = “123”;

int num = atoi(str);

}

int→string

c++
int num=123;

string str=to_string(num);

C语言

法一:
#include <stdio.h>
int main() {
int num = 123;
char str[20];

sprintf(str, “%d”, num);

printf(“转换后的字符串: %s\n”, str);
return 0;
}

法二:

#include <stdio.h>
#include <stdlib.h>int main() {
int num = 789;
char str[20];

itoa(num, str, 10);

printf(“转换后的字符串: %s\n”, str);
return 0;
}


http://www.kler.cn/a/572644.html

相关文章:

  • 基于stm32的多旋翼无人机(Multi-rotor UAV based on stm32)
  • 软考中级-数据库-3.3 数据结构-树
  • Leetcode3270:求出数字答案
  • C#实现语音合成播报器——基于System.Speech的语音交互方案,在windows上实现语音播报指定文本
  • 请谈谈 HTTP 中的请求方法(GET、POST、PUT、DELETE等),它们的区别是什么?
  • Python----数据分析(Matplotlib四:Figure的用法,创建Figure对象,常用的Figure对象的方法)
  • alloc、malloc 与 allocator:内存管理三剑客
  • 7.1 Hugging Face PEFT 快速入门:参数高效微调实战指南
  • Cpu100%问题处理(包括-线上docker服务)
  • 基于Asp.net的农产品销售管理系统
  • DeepSeek R1助力,腾讯AI代码助手解锁音乐创作新
  • 使用JMeter(组件详细介绍+使用方式及步骤)
  • YOLO11改进-模块-引入多尺度小波池化变压器MWPT 通过结合小波变换、多尺度池化以及门控机制等技术解决多尺度、小目标、边缘模糊等问题
  • 服务器CPU微架构
  • 国产编辑器EverEdit - 工具栏自定义及认识工具栏上的按钮
  • Linux:文件系统的初步认识
  • 蓝桥杯 之 贪心与排序
  • 大模型gpt结合drawio绘制流程图
  • (二 十 三)趣学设计模式 之 解释器模式!
  • 【单片机】嵌入式系统大纲