我想用C++绘制热图,我遇到了这个代码:用C++中的Gnuplot绘制热图
总而言之gp.send2d(frame);,send2d看到的函数是float*,那么它如何知道尺寸(4x4)?或者甚至可以安全地访问16个元素?
数组的大小是其类型的一部分.在您链接到的示例中frame是float[4][4].自从send2d()宣布为
template <typename T> Gnuplot &send2d(const T &arg)
Run Code Online (Sandbox Code Playgroud)
它将演绎的类型T是float[4][4],使arg该参考.扩大了看起来像
Gnuplot &send2d(const float (&arg)[4][4])
Run Code Online (Sandbox Code Playgroud)
这表明这arg是一个参考2d数组.由于我们有一个引用,因此指针没有衰减.
这是一个示例,显示使用引用维护数组trype而不是衰减到指针
template<typename T>
void foo(T & arr_2d)
{
for (auto& row : arr_2d)
{
for (auto& col : row)
std::cout << col << " ";
std::cout << "\n";
}
}
int main()
{
int bar[2][2] = {1,2,3,4};
foo(bar);
}
Run Code Online (Sandbox Code Playgroud)
输出:
1 2
3 4
Run Code Online (Sandbox Code Playgroud)