gpt4 book ai didi

c++ - cin 转换为不带变量的函数参数

转载 作者:行者123 更新时间:2023-12-02 16:16:44 25 4
gpt4 key购买 nike

我想将用户输入直接传递给函数而不使用变量。现在我正在这样做:

int temp, len;
cin >> len;
for (int i = 0; i < len; i++){
cin >> temp;
foo(temp);
}

我可以在没有温度的情况下完成吗?也许我不应该使用“cin”?

最佳答案

您仍然可以创建包装函数:

template <typename T>
T get_input(std::istream& cin)
{
T res;
std::cin >> res;
return res;
}

然后:

const int len = get_input<int>(std::cin);
for (int i = 0; i < len; i++) {
foo(get_input<int>(std::cin));
}

关于c++ - cin 转换为不带变量的函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61579630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com