我想从键盘读取一个或两个值,第一个代表unnest
,第二个代表library(tidyverse)
df %>%
mutate_at(2:3, as.Date) %>%
transmute(id = id, date = map2(start, end, ~ seq(.x, .y, by = 'month'))) %>%
unnest
# id date
#1 2 2018-10-01
#2 2 2018-11-01
#3 2 2018-12-01
#4 3 2018-01-01
#5 3 2018-02-01
#6 3 2018-03-01
#7 3 2018-04-01
。在输入流中,我可以获得一个值,并且可以使用length_min
检查是否还有另一个值,但是当我从键盘上读取一个数字时,我必须引入另一个字符以打印正确的值。
阅读模板:
length_max
和/或不是cin.get()
(如果该行在length + 'length_min'
之后存在另一个数字,则读'length_max'
)。
length_max
对于这些示例:
length_min
->打印string length;
cin >> length;
int length_min, length_max;
cin >> length_min;
cin.ignore(); // there's a whitespace between length_min and length_max
char c = cin.get();
if (isdigit(c)) // I verified if there's a digit which represents the length_max
{
ungetc(c, stdin);
cin >> length_max;
}
else
{
cin.ignore(); // if there's no digit go to next line in input stream
length_max = 0;
}
length 5 8
->打印5 8
我该如何摆脱这个额外的角色,但我不确定如何。
答案 0 :(得分:1)
要从控制台读取两个变量:
int variable_1;
int variable_2;
std::cin >> variable_1 >> variable_2;
如果第二个变量是可选的,则最好的方法是一次读取一行,然后解析该行:
std::string text;
while (std::getline(std::cin, text))
{
std::istringstream input(text);
input >> variable_1;
if (input >> variable_2)
{
// Do stuff with variable_2
}
}