使用命令行将二维数组传递给C ++ [英] Passing Two dimensional array to C++ using command line

查看:72
本文介绍了使用命令行将二维数组传递给C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我需要从命令行调用C ++应用程序,并且需要将int类型的二维数组传递给它。任何人都可以让我知道如何做到这一点,以及如何在C ++应用程序中使用argv参数解释它。



预先感谢。

解决方案

argv 中,您只能传递一维数组,其中包含字符串,它是

  char * argv [] 

因此,您不能真正传递2D数组,但可以模拟它。



例如,传递2个参数,说矩阵的大小是多少-行数和列数,然后将所有元素一一传递。 p>

然后解析程序中的参数,知道您将使用哪种格式。






例如:如果您想通过

  1 2 3 
4 5 6

您可以这样运行程序:

  ./ my_program 2 3 1 2 3 4 5 6 

这种方式,您会知道, argv [1] 是行数, argv [2] 是行数列,它们都是2D数组的所有元素,从左上角开始。



别忘了, argv 是数组,包含 char * 指针。换句话说,您需要转换所有参数 int s。


I am having a requirement where in I need to call a C++ application from command line and need to pass a two dimensional array of int type to it. Can anyone please let me know how to do that, and how to interpret it in C++ application using argv parameter

thanks in advance.

解决方案

In argv you can pass only a one dimensional array, containing strings, it's

char* argv[]

So, you can't really pass 2D array, but you can "simulate" it.

For example, pass 2 parameters, saying what are the sizes of the matrix - number of rows and number of columns and then pass all elements, one by one.

Then parse the arguments in your program, knowing what format you will use.


For example: if you want to pass

1 2 3
4 5 6

you may run your program like this:

./my_program 2 3 1 2 3 4 5 6

This way, you'll know, that argv[1] is the number of rows, argv[2] s the number of columns and them all elements of the 2D array, starting from the upper left corner.

Don't forget, that argv is array, containing char* pointers. In other words, you'll need to convert all parameters ints.

这篇关于使用命令行将二维数组传递给C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆