C++ 调用多参数模板函数
我正在尝试从我的main调用模板函数。我以前使用过这种语法,其工作原理如下所示:C++ 调用多参数模板函数,c++,templates,C++,Templates,我正在尝试从我的main调用模板函数。我以前使用过这种语法,其工作原理如下所示: #include <iostream> using namespace std; // One argument in template template<int size> void print(char matrix[][size]) { for (int i = 0; i < size; i++) { for (int j = 0; j <
#include <iostream>
using namespace std;
// One argument in template
template<int size>
void print(char matrix[][size]) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << matrix[i][j];
}
cout << endl;
}
return;
}
// MAIN
int main() {
constexpr int n = 3;
char matrix[n][n] = {'a', 'b', 'c',
'h', 'i', 'd',
'g', 'f', 'e'};
print<n>(matrix); // Calling function like this
return 0;
}
#包括
使用名称空间std;
//模板中的一个参数
样板
无效打印(字符矩阵[][大小]){
对于(int i=0;i
对数组的引用仍然是对数组的引用
这就是为什么std::array
被发明的原因;它的病态行为更少
由于C++中的C兼容性问题,数组参数不是数组;它们“衰减”到指针。
对数组的引用仍然是对数组的引用
这就是为什么std::array
被发明的原因;它的行为不那么病态。首先,rows和cols变量没有定义,所以我做了一些假的变量(如果你真的运行它,它们会崩溃),因为这不是你问题的核心部分(它们被评论为“这很糟糕”)
问题是不能将运行时定义的变量传递给模板。编译器必须在编译时知道这些值,所以我将它们更改为constexpr,这就是解决您所问的主要问题的方法
#包括
使用名称空间std;
样板
void setRowZero(整数矩阵[num_rows][num_cols],整数行,整数列){
for(int i=0;i
正如另一个答案所示,如果不指定num_rows和num_cols,则可以让编译器推断出正确的num_rows和num_cols值。它是根据调用站点使用的变量类型来推断的,传入的变量类型的维度也是编译时常量。首先,rows和cols变量不是定义,所以我做了一些假的(如果你真的运行它,它们会崩溃),因为这不是你问题的核心部分(它们被评论为“这很糟糕”)
问题是不能将运行时定义的变量传递给模板。编译器必须在编译时知道这些值,所以我将它们更改为constexpr,这就是解决您所问的主要问题的方法
#包括
使用名称空间std;
样板
void setRowZero(整数矩阵[num_rows][num_cols],整数行,整数列){
for(int i=0;i
正如另一个答案所示,如果不指定num_rows和num_cols,您可以让编译器推断出正确的num_rows和num_cols值。它是根据调用站点使用的变量类型来推断的,传入的变量类型的维度也是编译时常量。谢谢。这是已编译的。但我没有取消rstand为什么我不能这样调用它:setRowZero(矩阵,I)。@JessicaWang因为num_rows和num_cols是运行时常量,但不是编译时常量,编译时常量是非类型(int)模板参数所必需的谢谢。那是编译的。但我不明白为什么我不能这样调用它:setRowZero(矩阵,I)@JessicaWang,因为num_rows和num_cols是运行时常量,但不是编译时常量,非类型(int)需要编译时常量模板参数我迟到了,但是谢谢你!你的解释很有道理。谢谢你的耐心。我迟到了,但是谢谢你!你的解释很有道理。谢谢你的耐心。
#include <iostream>
using namespace std;
template<int num_rows, int num_cols>
// Set this entire row to 0
void setRowZero(int matrix[num_rows][num_cols], int row, int cols) {
for (int i = 0; i < cols; i++) {
matrix[row][i] = 0;
}
return;
}
int main() {
// Matrix to be changed
int matrix[3][4] = {1, 1, 1, 1,
1, 0, 1, 1,
1, 1, 0, 1};
// Get row and cols
int num_rows = sizeof(matrix)/sizeof(matrix[0]);
int num_cols = sizeof(matrix[0])/sizeof(int);
// Record the position of the 0s
for (int i = 0; i < num_rows; i++) { // rows
for (int j = 0; j < num_cols; j++) { // cols
if (matrix[i][j] == 0) {
rows[i] = 0;
cols[j] = 0;
}
}
}
// Set to 0's
for (int i = 0; i < num_rows; i++) {
if (rows[i] == 0) {
setRowZero<num_rows, num_cols>(matrix, i, num_cols);
}
}
return 0;
}
// Set this entire row to 0
template<int num_rows, int num_cols>
void setRowZero(int (&matrix)[num_rows][num_cols], int row) {
for (int i = 0; i < num_cols; i++) {
matrix[row][i] = 0;
}
}
setRowZero(matrix, i);
#include <iostream>
using namespace std;
template<int num_rows, int num_cols>
void setRowZero(int matrix[num_rows][num_cols], int row, int cols) {
for (int i = 0; i < cols; i++) {
matrix[row][i] = 0;
}
return;
}
int main() {
int * rows; // this is bad
int * cols; // this is bad
// Matrix to be changed
int matrix[3][4] = {1, 1, 1, 1,
1, 0, 1, 1,
1, 1, 0, 1};
// Get row and cols
constexpr int num_rows = sizeof(matrix)/sizeof(matrix[0]);
constexpr int num_cols = sizeof(matrix[0])/sizeof(int);
// Record the position of the 0s
for (int i = 0; i < num_rows; i++) { // rows
for (int j = 0; j < num_cols; j++) { // cols
if (matrix[i][j] == 0) {
rows[i] = 0;
cols[j] = 0;
}
}
}
// Set to 0's
for (int i = 0; i < num_rows; i++) {
if (rows[i] == 0) {
setRowZero<num_rows, num_cols>(matrix, i, num_cols);
}
}
return 0;
}