[英]Dynamic 1D array as input
I am trying to get a dynamic 1D array of integers as input. 我正在尝试获取整数的动态一维数组作为输入。 The situation is I do not know how many inputs I would have.
情况是我不知道会有多少输入。 I have the following code, but I know its wrong.
我有以下代码,但我知道它是错误的。 Could you please tell me how I can achieve this?
你能告诉我我怎么能做到吗?
int main(string path,string aligned_path,vector<int>cams)
{
string path = "/home/iiith/resect/output.txt";
findpoints(cams,path);
}
The size of cams is actually unknown. 凸轮的尺寸实际上是未知的。 I would not know how many cams they would provide as input.
我不知道他们将提供多少个凸轮作为输入。 Am trying to achieve something like the below
我正在努力实现以下目标
./a.out path1 path2 1 2 3 5 6
The numbers trailing in the end would be the cams. 末尾的数字将是凸轮。 it can be any number of cams.
它可以是任意数量的凸轮。 Is it possible to receive them all as input?
是否可以全部接收它们作为输入? is it possible to have an inut parameter that is dynamic as the above.?
是否有可能具有如上所述的动态inut参数? Please help me here.
请在这里帮助我。 It would b really helpful.
这真的很有帮助。 Thanks in Advance
提前致谢
Your definition of main
is invalid. 您对
main
定义无效。 In C and C++, main has a strict definition (although arguments can be ignored if not used): 在C和C ++中,main有一个严格的定义(尽管如果不使用参数也可以忽略):
int main(int argc, char **argv)
A common extension is to also pass in environment variables: 一个常见的扩展是还传递环境变量:
int main(int argc, char **argv, char **envp)
and if arguments aren't used at all: 如果根本不使用参数:
int main()
The arguments to main
are defined as follows argc
is the number of elements in the list of arguments. main
的自变量定义如下: argc
是自变量列表中元素的数量。 Each of the following argv[0]
.. argv[argc-1]
contain the arguments to your function. 以下每个
argv[0]
.. argv[argc-1]
都包含函数的参数。 argv[0]
is the name of the executable itself. argv[0]
是可执行文件本身的名称。 argv[1]
with your example would be path1
. 例子中的
argv[1]
将是path1
。
So, you know how many cams
there are based on argc
. 因此,您知道有多少个基于
argc
cams
。 Of course, you have to copy the string values in argv[x]
into the relevant place in a vector, using, perhaps, std::stoi(...)
to make it into an integer value. 当然,您必须使用
std::stoi(...)
将argv[x]
的字符串值复制到向量中的相关位置,以使其成为整数值。
The signature of main
must be int main(int argc, char* argv[])
(or equivalent, or int main()
). main
的签名必须是int main(int argc, char* argv[])
(或等效的,或者是int main()
)。 You would process the arguments into something more useful within your own code. 您将在您自己的代码中将参数处理为更有用的东西。 Here's a quick and sloppy example (no proper error handling, etc).
这是一个简短的示例(没有适当的错误处理等)。
#include <iostream>
#include <cstdlib>
#include <vector>
int main(int argc, char * argv[])
{
std::vector<int> nums;
nums.reserve(argc-3);
int pos = 3;
// Read args into vector of ints
while (pos < argc)
{
nums.push_back(atoi(argv[pos]));
++pos;
}
// Print them
for (auto i : nums) std::cout << i << '\n';
}
Here's a run: 运行:
$ ./a.out path1 path2 1 2 3 5 4
1
2
3
5
4
Just in case it isn't clear: argc
contains the number of arguments passed, including the name of the program. 以防万一尚不清楚:
argc
包含传递的参数数量,包括程序名称。 so for $ ./a.out
argc=1
, for $ ./a.out some list of args
argc=5
. 因此对于
$ ./a.out
argc=1
,对于$ ./a.out some list of args
argc=5
。
Then argv
is an array of c
-style strings ( NUL
-terminated const char *
s) containing the text of each argument. 然后
argv
是一个c
样式字符串(以NUL
结尾的const char *
s)的数组,其中包含每个参数的文本。
Note that strictly speaking the argv
array is 1 longer than it needs to be, and the last entry is guaranteed to be a NULL pointer. 请注意,严格来说,
argv
数组比需要的数argv
1,并且保证最后一个条目是NULL指针。
For Standard geeks, I don't have the proper C++
2011 standard, this is from N3337: 对于Standard Geek,我没有正确的
C++
2011标准,它来自N3337:
3.6.1 Main function
3.6.1主要功能
...
...
2 An implementation shall not predefine the
main
function.2实现不得预定义
main
功能。 This function shall not be overloaded.此功能不得重载。 It shall have a return type of type
int
, but otherwise its type is implementation-defined.它的返回类型应该是
int
类型,否则它的类型是实现定义的。 All implementations shall allow both of the following definitions ofmain
:所有实现都应允许以下两个
main
定义:
int main() { /* ... */ }
and
和
int main(int argc, char* argv[]) { /* ... */ }
In the latter form
argc
shall be the number of arguments passed to the program from the environment in which the program is run.在后一种形式中,
argc
是从运行程序的环境传递到程序的参数数量。 Ifargc
is nonzero these arguments shall be supplied inargv[0]
throughargv[argc-1]
as pointers to the initial characters of null-terminated multibyte strings (ntmbs s) (17.5.2.1.4.2) andargv[0]
shall be the pointer to the initial character of a ntmbs that represents the name used to invoke the program or "".如果
argc
为非零,则这些参数必须通过argv[argc-1]
在argv[0]
提供,以作为以空字符结尾的多字节字符串(ntmbs)(17.5.2.1.4.2)的初始字符的指针,而argv[0]
应该是指向ntmbs初始字符的指针,该ntmbs代表用于调用程序的名称或“”。 The value ofargc
shall be non-negative.argc
的值应为非负数。 The value ofargv[argc]
shall be0
.argv[argc]
的值应为0
。 [ Note: It is recommended that any further (optional) parameters be added afterargv
.[注意:建议在
argv
之后添加任何其他(可选)参数。 —end note ]—尾注]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.