它是一个类似的功能在C ++ 11和C11中的二维矩阵? [英] Is it a similar function in C++11 as in C11 for the twodimensional matrix?
问题描述
我注意到,在gcc C11,你可以传递任何矩阵到一个函数 fn(int row,int col,int array [row] [col])
。如何将C11中的下面的(在一个链接到另一个stackoverflow答案)程序转换为C ++ 11中的程序?
I have noticed that in gcc C11 you can pass any matrix to a function fn(int row, int col, int array[row][col])
. How to translate my below placed (in a link to an another stackoverflow answer) program in C11 to a program in C++11?
正如你所看到的,我可以传递给C11的静态和动态分配的数组。在C ++ 11中有可能吗?
As you can see I can pass to functions static and dynamic allocated arrays in C11. Is it possible in C++11?
我已经根据不同的stackoverflow答案做了一个示例程序,但是所有的函数都适用于array1, double array1 [ROW] [COL] = {{}}
和 auto array2 = new double [ROW] [COL]()
?
I have made an exemplary program based on different stackoverflow answers, but all functions work for array1 and none of them works for array2, where double array1[ROW][COL] = { { } }
and auto array2 = new double[ROW][COL]()
?
如何在C11中创建两个数组的函数 fn(int row,int col,int array [ row] [col])
?
How to make a function for both arrays as is made in C11 fn(int row, int col, int array[row][col])
?
#include <iostream>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
const int ROW=2;
const int COL=2;
template <size_t row, size_t col>
void process_2d_array_template(double (&array)[row][col])
{
cout << __func__ << endl;
for (size_t i = 0; i < row; ++i)
{
cout << i << ": ";
for (size_t j = 0; j < col; ++j)
cout << array[i][j] << '\t';
cout << endl;
}
}
void process_2d_array_pointer(double (*array)[ROW][COL])
{
cout << __func__ << endl;
for (size_t i = 0; i < ROW; ++i)
{
cout << i << ": ";
for (size_t j = 0; j < COL; ++j)
cout << (*array)[i][j] << '\t';
cout << endl;
}
}
// int array[][10] is just fancy notation for the same thing
void process_2d_array(double (*array)[COL], size_t row)
{
cout << __func__ << endl;
for (size_t i = 0; i < row; ++i)
{
cout << i << ": ";
for (size_t j = 0; j < COL; ++j)
cout << array[i][j] << '\t';
cout << endl;
}
}
// int *array[10] is just fancy notation for the same thing
void process_pointer_2_pointer(double **array, size_t row, size_t col)
{
cout << __func__ << endl;
for (size_t i = 0; i < row; ++i)
{
cout << i << ": ";
for (size_t j = 0; j < col; ++j)
cout << array[i][j] << '\t';
cout << endl;
}
}
int main()
{
double array1[ROW][COL] = { { } };
process_2d_array_template(array1);
process_2d_array_pointer(&array1); // <-- notice the unusual usage of addressof (&) operator on an array
process_2d_array(array1, ROW);
// works since a's first dimension decays into a pointer thereby becoming int (*)[COL]
double *b[ROW]; // surrogate
for (size_t i = 0; i < ROW; ++i)
{
b[i] = array1[i];
}
process_pointer_2_pointer(b, ROW, COL);
// allocate (with initialization by parentheses () )
auto array2 = new double[ROW][COL]();
// pollute the memory
array2[0][0] = 2;
array2[1][0] = 3;
array2[0][1] = 4;
array2[1][1] = 5;
// show the memory is initialized
for(int r = 0; r < ROW; r++)
{
for(int c = 0; c < COL; c++)
cout << array2[r][c] << " ";
cout << endl;
}
//process_2d_array_pointer(array2);
//process_pointer_2_pointer(array2,2,2);
int info;
cout << abi::__cxa_demangle(typeid(array1).name(),0,0,&info) << endl;
cout << abi::__cxa_demangle(typeid(array2).name(),0,0,&info) << endl;
return 0;
}
推荐答案
虽然C ++与C语言共享了基本语法,但是当它涉及到多维数组时, )数组,在C ++中,数组类型的能力显着降低:在C ++中,数组类型的大小需要是编译时常量。这里有一些例子:
While C++ shares the basic syntax with C when it comes to (multidimensional) arrays, array types are significantly less powerfull in C++: In C++ the sizes of array types are required to be compile time constants. Here are a few examples:
void foo(int a, int b) {
int foo[2][3]; //legal C++, 2 and 3 are constant
int bar[a][3]; //Not C++, proposed for C++17: first dimension of an array may be variable
int baz[a][b]; //Not C++, legal in C99
int (*fooPtr)[2][3]; //legal C++
int (*barPtr)[a][3]; //Not C++, legal in C99
int (*bazPtr)[a][b]; //Not C++, legal in C99
typedef int (*fooType)[2][3]; //legal C++
typedef int (*barType)[a][3]; //Not C++, legal in C99
typedef int (*bazType)[a][b]; //Not C++, legal in C99
int (*dynamicFoo)[3] = new int[2][3]; //legal C++
int (*dynamicBar)[3] = new int[a][3]; //legal C++
int (*dynamicBar)[b] = new int[a][b]; //Not C++
}
如你所见,几乎所有C语言大小的数组在C ++中是不可能的。即使是为下一个C ++标准提出的VLA扩展也无济于事:它限制在数组的第一个维度。
As you see, almost everything that's possible in C with dynamic sized arrays is not possible in C++. Even the VLA extension that's proposed for the next C++ standard does not help much: it's restricted to the first dimension of an array.
在C ++中,你必须使用 std :: vector<>
来实现你可以用C99可变长度数组实现。具有所有后果:
In C++, you have to use std::vector<>
to achieve what you can achieve with C99 variable length arrays. With all the consequences:
-
std :: vector< std :: vector< > >
在内存中不连续。您的缓存可能不喜欢这样。
The data in an
std::vector<std::vector<> >
is not consecutive in memory. Your caches might not like this.
不能保证使用 std :: vector< std :: vector< >
表示所有行数组的长度相同。
There is no guarantee with an std::vector<std::vector<> >
that all line arrays have the same length. This can be useful, or a pain, depending on your use case.
如果你有一个 std中的元素的迭代器:: vector< std :: vector<> >
,您无法将其推进到下一行中的相应元素。
If you have an iterator to an element in an std::vector<std::vector<> >
, you can't advance it to the corresponding element in the next line.
这篇关于它是一个类似的功能在C ++ 11和C11中的二维矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!