Logo

Programming-Idioms

  • Go

Idiom #148 Read list of integers from stdin

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

STDIN.read.split.map(&:to_i)

split will split the input around spaces by default.

map(&:to_i) is just short for the block map{|x| x.to_i}
import (
	"bufio"
	"os"
	"strconv"
)
var ints []int
s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanWords)
for s.Scan() {
	i, err := strconv.Atoi(s.Text())
	if err == nil {
		ints = append(ints, i)
	}
}
if err := s.Err(); err != nil {
	return err
}
#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...