如何解决C ++无论给出多少个参数,如何制作重载函数以编号列表显示其参数?
无论提供多少个参数,我都想创建一个在有序列表中显示其参数的函数。我想制作这个示例程序来对其进行测试,但是我无法使其工作,尝试多种方法。这是简单的测试程序(没有任何实现):
#include <iostream>
void list( ... ); //list() starts from 1 and ends with however many there are
void list0( ... ); //list0() starts from 1 and ends with however many there are
//minus 1,because the last option is marked with a zero
int main() {
int drink,stuff,place,final;
std::cout << "Which would you like?" << std::endl;
list("tea","coffee");
std::cin >> drink;
std::cout << "Which would you like to take?" << std::endl;
list0("milk","cream","sugar","nothing");
std::cin >> stuff;
std::cout << "Where will you be sitting?" << std::endl;
list("table 1","table 2","table 3");
std::cin >> place;
std::cout << "Would you like anything else?" << std::endl;
list0("yes","no");
std::cin >> final;
if (final == 1)
std::cout << "Too bad,Go sit down" << std::endl;
return 0;
}
/* other stuff */
例如,它可以显示以下内容:
Which would you like?
1 - tea
2 - coffee
1
Which would you like to take?
1 - milk
2 - cream
3 - sugar
0 - nothing
1
Where will you be sitting?
1 - table 1
2 - table 2
3 - table 3
2
Would you like anything else?
1 - yes
0 - no
0
我需要帮助的地方是确定实现。这可能需要重载,因此我尝试使用重载的运算符创建类,但无法确定如何使用可变数量的参数来定义函数。
如果对此问题有完全不同的解决方案,不需要任何类似的操作,或者是一种简单得多的技术(除了一遍又一遍地显式编写std::cout << "1 - ..." << std::endl;
之外),请说明一下,因为我需要某种实现。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。