将数组作为带有默认值的参数传递到int main() [英] Passing an array as a parameter with default values into int main()

查看:144
本文介绍了将数组作为带有默认值的参数传递到int main()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将数组作为参数传递给具有默认值的 int main()
例如:

  int main(int a){} 

奇妙。

  int main(int a = 1){} 

传递 int main()数组也非常奇妙:

  int main(int a [3])

但是,合并这两个概念似乎是:

  int main(int a [1] {0,1})

在大量的 googleing ,我没有找到解决方案。 >

请帮助我这是你唯一的希望!



EDIT



简而言之,目的是为了使我的代码尽可能少的线路,对于我的教授最近发布的挑战(不是为了学习)。该任务是创建递归的12天的圣诞节计划



这是我当前的计划

  #include< iostream> 
#include< string>
void p(std :: string v){std :: cout< std :: string v [13] = {梨树中的鹧鸪\\ n,2 Turtle Doves \\\
,3 French Hens \\\
,4 Colly Birds \\\
,5 Gold Rings\\\
,6 Geese-a-Laying\\\
,7,9,9,9,9等。 Piping \\\
,12 Drummers Drumming \\\
,};
int main(){
switch(v [12] .length()){
case 12:system(pause); return 0;
case 11:p(v [11]);
case 10:p(v [10]);
case 9:p(v [9]);
case 8:p(v [8]);
case 7:p(v [7]);
case 6:p(v [6]);
case 5:p(v [5]);
case 4:p(v [4]);
case 3:p(v [3]);
case 2:p(v [2]);
case 1:p(v [1]);
case 0:p(v [0]);
} v [12] + =0;
main();
}

我想将数组的经文作为参数传递给main在函数上面声明它。我知道,不是最内存/堆栈意识。但它会消除一行:)

解决方案

链接最好地解释:


在C ++中不可能传递
完整的内存块作为一个
参数到一个函数,但我们是
允许传递其地址。


这就是为什么你可以声明一个函数

  void foo(int bar []); 

但您不能声明

  void foo(int bar [] = {0,1}); 

main()无关


I am having difficulty passing an array as an argument into int main() with default values. For example:

int main(int a){}

works wonderfully. As does

int main(int a = 1){}

Passing int main() an array also works wonderfully:

int main(int a[3])

However, combining these two concepts seems break:

int main(int a[1] = {0,1})

After a significant amount of googleing, I haven't found a solution.

please help me SO, you're my only hope!

EDIT

The purpose of this, in short, is to make my code as little lines as possible, for a challenge my professor recently issued (not for points -- just for learning). The assignment is to create a recursive "12-days-of-chirstmas" program

This is my current program

#include <iostream> 
#include <string>
void p(std::string v){std::cout<<v;}
std::string v[13] = {"A Partridge in a Pear Tree.\n\n","2 Turtle Doves\n","3 French Hens\n","4 Colly Birds\n","5 Gold Rings\n","6 Geese-a-Laying\n","7 Swans-a-Swimming\n","8 Maids-a-Milking\n","9 Ladies Dancing\n","10 Lords-a-Leaping\n","11 Pipers Piping\n","12 Drummers Drumming\n",""};
int main(){
    switch(v[12].length()){
        case 12:system("pause"); return 0;
        case 11:p(v[11]);
        case 10:p(v[10]);
        case 9: p(v[9]);
        case 8: p(v[8]);
        case 7: p(v[7]);
        case 6: p(v[6]);
        case 5: p(v[5]);
        case 4: p(v[4]);
        case 3: p(v[3]);
        case 2: p(v[2]);
        case 1: p(v[1]);
        case 0: p(v[0]); 
    }v[12] += "0";
    main();
}

I would like to pass in the array of verses as an argument to main instead of declaring it above the function. I know, not the most memory/stack conscious. But it would eliminate a line :)

解决方案

This link explains it best:

In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address.

That's why you can declare a function with

void foo (int bar[]);

but you can't declare

void foo (int bar[] = {0 ,1});

It has nothing to do with main().

这篇关于将数组作为带有默认值的参数传递到int main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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