【发布时间】:2013-10-21 11:22:31
【问题描述】:
我们的讲师要求我们通过以下方式将数据输入代码:
1.Random mode Run with graphs generated by random number generator. The command line for this mode is: $ mst –r n d // run in a random connected graph with n vertices and d% of density. // See Performance measurements section for details. 2.User Input mode $mst -s file-name // read the input from a file ‘file-name’ for simple scheme $mst -f file-name // read the input from a file ‘file-name’ for f-heap scheme
目前我已经写好代码并使用g++编译了:
g++ -o mst.o mst.cpp
我正在像这样从文件中获取输入:
./mst.o < data.txt
但是,我不知道如何满足上述要求。
【问题讨论】:
-
你的意思是你不知道如何处理命令行参数?
-
是的,这是正确的说法。
-
查看
getopt或使用轻量级解决方案。 -
@remyabel:命令行看起来有点简单。只有三个选项只需读取第一个参数(如果它不存在则报告错误)然后使用
if () {} else if () {} else if () {} else {/*error*/}。请注意,您无法打开字符串,因此您需要 else if :-)
标签: c++ input g++ command-line-arguments