将2D数组设置为函数,并根据需要在其他位置调用它

时间:2017-05-17 07:58:15

标签: c++

所以我有这个变量我想将它作为函数传递给下面的代码,但仍然没有工作,但任何想法呢?我也很高兴知道我们是否可以添加ctime来跟踪用户的到来,感谢您的帮助..

bool spt_1 [15][12] = {0} 

这是我要传递的代码,所以我可以稍后在我的代码中的某个地方调用它

int col = 1;
int row = 1;

for (col = 1 ; row < 16 ; row ++) {

  if (spot_1 [col][row] == 0) {

cout<<"There is a place reserved for you in spot, the first column , row number "<<" "<<row<<"."<<endl;

string choice;

do {

cout<<"\nDo you want to take that spot? Y/N.\n"<<endl;
cin>>choice;
cout<<"\n"<<endl;

transform(choice.begin(), choice.end(), choice.begin(), toupper); 
}while (choice != "Y" && choice !="YE" && choice != "YES" && cout<<"Wrong input!\n"<<endl);

cout<<"\nHave a nice day.\n"<<endl;
break;

 if (choice == "YES") {

spot_1 [col][row] = 1; // should change that specific 0 to 1 ( which means occupied )

 }

else  {

//it should reject count ++;

} 
 if (spot_1 [col][row] != 0) { // When there is no more place it should cout this and go search in a new array and do same as first one

cout<<"Sorry ,There is no more place available , But you can go to :\n"<<endl;   
    } 
   }
  }
}

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

您应该首先使用2D矢量而不是该数组。但如果您确实需要使用数组,则可以定义模板函数,如下所示:

some_func<15,12>(spt_1);

并在你的程序中调用它:

void some_func(void* my_pointer, int row, int col)
{
    bool* my_pointer_arr = static_cast<bool*>(my_pointer);
    for (int i = 0; i < row * col; i += col)
    {
        for (int j = 0; j < col; j++)
            std::cout << my_pointer_arr[i + j];
        std::cout << std::endl;
    }
    my_pointer_arr[3 * col + 4] = false;
}

评论后编辑:

总有一种方法,但它变得更脏。 我认为这更像是一个C然后是一个C ++问题,我不是一个C语言本地人。 我可能会这样做,但可能有更好的方法:

  .column5 a { 
  display: block;
  text-align: center;
}
.column5 a {
  display: inline-block;
  height: 20px;
  width: 20px;
  border: 3px solid white;
  box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  margin: 10px 20px 20px 20px;
}

        <div class="column5"> 
                <?php 
                echo "<a href=$hiperligacao><img src=\"showfile.php?cod_marcas=$cod_marcas\" height=80 width=80></a>";?>
                </div>
                 <?php  
            }
        }
    }
    ?>

    </a> 

也许只是搜索“C风格的2D数组来运行”,你会得到一个更好的答案。