- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
由于一些技巧,我能够在编译时生成一个表,尽管表中的值不是很有用。例如一张 5x5 的 table 看起来像这样:
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
逗号是为了清楚起见。创建此表的代码如下:
#include <iostream>
using ll = long long;
template<typename type,type...data>
struct sequence
{
static type seq_data[sizeof...(data)];
static const ll size;
type operator[](ll index){
return seq_data[size-index-1];
}
};
template<typename type,type...data>
type sequence<type,data...>::seq_data[sizeof...(data)] = { data... };
template<typename type,type...data>
const ll sequence<type,data...>::size{sizeof...(data)};
template<ll n,ll l,ll...data> struct create_row
{
typedef typename create_row<n-1,l+1,l,data...>::value value;
};
template<ll l,ll...data>
struct create_row<0,l,data...>
{
typedef sequence<ll,data...> value;
};
template<ll cols,ll rows>
struct table
{
typename create_row<cols,1>::value row_data[rows];
static const ll size;
};
template<ll cols,ll rows>
const ll table<cols,rows>::size{cols*rows};
using namespace std;
int main()
{
table<5,5> my_table;
for(int i{0};i<5;i++)
{
for(int j{0};j<5;j++)
{
cout<<my_table.row_data[i][j]<<",";
}
cout<<endl;
}
}
如您所见,为了创建单行,我不得不在 struct table 中使用硬编码值“1”,因此 create_table 将始终返回相同的序列,一系列从 1 到 n 的数字。因此,表中的每一行都具有相同的值。
我想做的是在编译时为每一行编码一个不同的起始值,以便有一个看起来像这样的表:
1,2,3,4,5,
6,7,8,9,10,
11 <CUT>
我找不到任何方法来创建这种类型的表格。
你知道怎么做吗?
最佳答案
在你的帖子最后我不确定你是否有兴趣在:-
f(i)
以行优先顺序环绕矩阵,例如Cols = 3; Rows = 3; f(i) = 2i; Vals = (1,2,3,4,5,6,7,8,9) -> |02|04|06| ---------- |08|10|12| ---------- |14|16|18|
or:-
f(i)
for some specified initial i
per row, e.g.Cols = 3; f(i) = 3i; First_Vals = (4,7,10) -> |12|15|18| ---------- |21|24|27| ---------- |30|33|36|
Anyhow there are ways to do both, and here is one you can use with aC++14 conforming compiler. (As @AndyG has commented, the appropriateimplementation for a compiletime matrix - leveraging the Standard Library- is an std::array
of std::array
.)
#include <array>
#include <utility>
namespace detail {
template<typename IntType, IntType(*Step)(IntType), IntType Start, IntType ...Is>
constexpr auto make_integer_array(std::integer_sequence<IntType,Is...>)
{
return std::array<IntType,sizeof...(Is)>{{Step(Start + Is)...}};
}
template<typename IntType, IntType(*Step)(IntType), IntType Start, std::size_t Length>
constexpr auto make_integer_array()
{
return make_integer_array<IntType,Step,Start>(
std::make_integer_sequence<IntType,Length>());
}
template<
typename IntType, std::size_t Cols,
IntType(*Step)(IntType),IntType Start, std::size_t ...Rs
>
constexpr auto make_integer_matrix(std::index_sequence<Rs...>)
{
return std::array<std::array<IntType,Cols>,sizeof...(Rs)>
{{make_integer_array<IntType,Step,Start + (Rs * Cols),Cols>()...}};
}
} // namespace detail
/*
Return a compiletime initialized matrix (`std::array` of std::array`)
of `Cols` columns by `Rows` rows. Ascending elements from [0,0]
in row-first order are populated with successive values of the
constexpr function `IntType Step(IntType i)` for `i` in
`[Start + 0,Start + (Rows * Cols))`
*/
template<
typename IntType, std::size_t Cols, std::size_t Rows,
IntType(*Step)(IntType), IntType Start
>
constexpr auto make_integer_matrix()
{
return detail::make_integer_matrix<IntType,Cols,Step,Start>(
std::make_index_sequence<Rows>());
}
/*
Return a compiletime initialized matrix (`std::array` of std::array`)
of `Cols` columns by `sizeof...(Starts)` rows. Successive rows are populated
with successive values of the constexpr function `IntType Step(IntType i)`
for `i` in `[start + 0,start + Cols)`, for `start` successively in `...Starts`.
*/
template<typename IntType, std::size_t Cols, IntType(*Step)(IntType), IntType ...Starts>
constexpr auto make_integer_matrix()
{
return std::array<std::array<IntType,Cols>,sizeof...(Starts)>
{{detail::make_integer_array<IntType,Step,Starts,Cols>()...}};
}
您可以通过附加以下内容来制作演示程序:
#include <iostream>
using namespace std;
template<typename IntType>
constexpr auto times_3(IntType i)
{
return i * 3;
}
static constexpr auto m4x6 = make_integer_matrix<int,4,6,×_3<int>,4>();
static constexpr auto m5x1 = make_integer_matrix<int,5,×_3<int>,7>();
static constexpr auto m6x5 = make_integer_matrix<int,6,×_3<int>,11,13,17,19,23>();
static_assert(m4x6[0][0] == 12,"");
int main()
{
cout << "A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4" << endl;
for (auto const & ar : m4x6) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
cout << endl;
cout << "A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>"
<< endl;
for (auto const & ar : m6x5) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
cout << endl;
cout << "A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>"
<< endl;
for (auto const & ar : m5x1) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
return 0;
}
应该输出:
A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4
12 15 18 21
24 27 30 33
36 39 42 45
48 51 54 57
60 63 66 69
72 75 78 81
A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>
33 36 39 42 45 48
39 42 45 48 51 54
51 54 57 60 63 66
57 60 63 66 69 72
69 72 75 78 81 84
A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>
21 24 27 30 33
您可能还对 std::experimental::make_array 感兴趣
关于c++ - 编译时生成的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334752/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!