在C ++中读取单个输入行中的整数列表 [英] Reading a list of integer in a single input line in C++

查看:102
本文介绍了在C ++中读取单个输入行中的整数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从单个输入行读取多个整数到
数组中。输入:100 200 300 400,所以数组是:a [0] = 100,a [1] = 200,a [2] = 300,a [3] = 400
事情是,数量是整数是未知的,因此数组的大小是未知的。

I'm trying to read multiple integers from a single input line into an array eg. Input: 100 200 300 400, so the array is: a[0] = 100, a[1] = 200, a[2] = 300, a[3] = 400 The thing is, the number of integers are unknown, so the size of the array is unknown.

推荐答案

您应该使用自动调整大小的容器,例如 std :: vector

You should use a container that automatically resizes itself, such as std::vector.

例如,类似这样的事情:

For example, something like this:

#include <string>
#include <iostream>
#include <sstream>
#include <utility>
#include <iterator>

std::string line;
getline(instream, line);
std::istringstream this_line(line);
std::istream_iterator<int> begin(this_line), end;
std::vector<int> values(begin, end);

这篇关于在C ++中读取单个输入行中的整数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆