我要创建一个坐标查找表,类似:
int a[n][2] = {{0,1},{2,3}, ... }
对于给定的
n
,在编译时创建.我开始调查
constexpr
,但似乎是一个返回
constexpr std::vector<std::array <int, 2> >
的函数
我无法得到这样的选择:
invalid return type 'std::vector<std::array<int, 2ul> >' of constexpr function
如何创建这样的编译时间数组?
最新回答
- 2021-1-121 #
- 2021-1-122 #
使用C ++ 14,您不需要太多模板魔术.这是一个如何为
f(x) = x^3
建立查询表的示例 第一个坐标是x
值,第二个坐标是f(x)
值:#include <iostream> template<int N> struct Table { constexpr Table() : values() { for (auto i = 0; i < N; ++i) { values[i][0] = i; values[i][1] = i * i * i; } } int values[N][2]; }; int main() { constexpr auto a = Table<1000>(); for (auto x : a.values) std::cout << "f(" << x[0] << ") = " << x[1] << '\n'; }
- 2021-1-123 #
使用GNU gperf或其他一些代码生成实用程序怎么办?
相关问题
- c++:移出的向量始终为空吗?c++c++11vectorlanguagelawyermovesemantics2021-01-12 00:57
- c++:递归typedef函数定义:std :: function返回自己的类型c++c++112021-01-11 23:56
- c++:关闭月食错误(不是真正的错误)c++c++11eclipsecdt2021-01-11 22:27
- c++:如果T不是默认构造函数,如何优雅地初始化std :: array <T,n>?c++arraystemplatesc++11initialization2021-01-11 18:57
- c++11:在C ++中,产生类类型临时变量的表达式可以属于哪些类别(左值,右值,右值等)?c++c++11rvaluereferencervaluexvalue2021-01-11 16:27
我将首先转储代码,然后在必要时/适当时添加引用和注释.如果结果与您要寻找的结果有些接近,请发表评论。
Xeo从这个答案修改包扩展的索引技巧(在此处应用生成器是必需的),已修改为使用
std::size_t
代替unsigned
发电机功能:
用法示例: