相关疑难解决方法(0)

指向数组c ++的指针

以下代码在做什么?

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)

c++ arrays pointers

22
推荐指数
3
解决办法
10万
查看次数

模板参数推导中int a [5]和int(&a)[5]之间的区别

这个问题是关于采用静态已知大小的数组的函数.

以下面的最小程序为例:

#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++ arrays templates template-argument-deduction

9
推荐指数
1
解决办法
648
查看次数

静态2d数组

我试图在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)

静态存取器的正确签名是什么?

c++ arrays static

3
推荐指数
1
解决办法
6230
查看次数