Logo

Programming-Idioms

  • Smalltalk
  • D

Idiom #148 Read list of integers from stdin

Read a list of integer numbers from the standard input, until EOF.

import std.stdio;
import std.conv : to;
import std.algorithm.iteration : map;
auto list = stdin.byLine.map!(a => a.to!int);

list is a lazy range that evaluates to each line of the standard input converted to int.
#include <vector>
#include <iostream>

using namespace std;
vector<int> v;
for(int t;;){
	cin >> t;
	if(cin.eof())
		break;
	v.push_back(t);
}

there must be 2 semicolon in for loop even it is empty

New implementation...