以下代码在做什么?
int g[] = {9,8};
int (*j) = g;
Run Code Online (Sandbox Code Playgroud)
从我的理解,它创建一个指向2个int数组的指针.但是为什么这会起作用:
int x = j[0];
Run Code Online (Sandbox Code Playgroud)
这不起作用:
int x = (*j)[0];
Run Code Online (Sandbox Code Playgroud) 这个问题是关于采用静态已知大小的数组的函数.
以下面的最小程序为例:
#include <iostream>
template<size_t N>
void arrfun_a(int a[N])
{
for(size_t i = 0; i < N; ++i)
std::cout << a[i]++ << " ";
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
arrfun_a<5>(a);
std::cout << std::endl;
arrfun_a<5>(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行时,打印预期结果:
2 3 4 5 6
3 4 5 6 7
Run Code Online (Sandbox Code Playgroud)
但是,当我试图让我的编译器(VS 2010)推断出5它时could not deduce template argument for 'int [n]' from 'int [5]'.
一些研究导致arrfun_b模板参数推导工作的更新:
template<size_t n>
void arrfun_b(int …Run Code Online (Sandbox Code Playgroud) 我试图在C++中定义和使用二维数组
const float twitterCounts[][5] = {
{1,0,0,0,0},
{0,2,0,0,0},
{0,0,3,0,0}
};
Run Code Online (Sandbox Code Playgroud)
像这样返回:
const float ??? TwitterLiveData::data() {
return twitterCounts;
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它
float x = TwitterLiveData::data()[1][1];
Run Code Online (Sandbox Code Playgroud)
静态存取器的正确签名是什么?