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

c语言、c++怎么将输入的一行字符根据空格分割成字符串数组或者整型数组

c++:

#include
#include
#include
#include
using namespace std;
int main() {
string line;
getline(cin, line); // 读取一行输入
istringstream iss(line);
vector nums;
int num;
while (iss >> num) { // 将输入字符串按空格分割并转换为整数存入vector
nums.push_back(num);
}
for (size_t i = 0; i < result.size(); ++i) {
cout << result[i];
if (i != result.size() - 1) { // 不是最后一个元素时输出空格
cout << " ";
}
}
return 0;
}

c语言

#include <stdio.h>
#include <stdlib.h>
int main() {
int temp,i=0;
int nums[100];
while (scanf("%d", &temp) != EOF) {
nums[i]=temp;
i++;
}


char. str[100][100];字符串数组
while (scanf("%s", &temp) != EOF) {
str[i]=temp;
i++;
}
return 0;
}


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

相关文章:

  • 理解 unserialize 函数的用法与安全注意事项
  • Markdown HTML 图像语法
  • Spring Boot 的 pom.xml 文件
  • 论文回顾:NeoBERT:新一代 BERT
  • 特征分解(Eigen decomposition)在深度学习中的应用与理解
  • flutter集成华为推送(Android)
  • build gcc
  • Debian12 安装中文输入法
  • WebSocket 协议爬虫
  • 2025年AI PPT工具精选:让演示文稿更智能、更高效
  • nginx 503错误分析
  • C++11详解(下)
  • MongoDB安全管理
  • 3-2安卓中的目录结构以及核心四大组件应用
  • python3.13安装教程【2025】python3.13超详细图文教程(包含安装包)
  • 人工智能之数学基础:n阶行列式
  • 【高并发内存池】申请内存
  • 浅谈DeepSeek使用技巧
  • 【AI文章解读】The Model is the Product
  • UML之参与者(Actor)