编译时生成的表

2024-03-13

由于一些技巧,我能够在编译时生成一个表,但表中的值并不是很有用。例如,5x5 的表格如下所示:

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;
    }
}

正如您所看到的,为了创建单行,我被迫在结构表, 为此原因创建表总是会返回相同的sequence,从 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)对于一些指定的初始i每行,例如


    Cols = 3; f(i) = 3i; First_Vals = (4,7,10) -> 
    |12|15|18|
    ----------
    |21|24|27|
    ----------
    |30|33|36|
  

无论如何,有多种方法可以做到这两点,这是一种可以与 符合 C++14 的编译器。 (正如 @AndyG 所评论的,适当的 编译时矩阵的实现 - 利用标准库 - 是一个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,&times_3<int>,4>();
static constexpr auto m5x1 = make_integer_matrix<int,5,&times_3<int>,7>();
static constexpr auto m6x5 = make_integer_matrix<int,6,&times_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

See it 在ideone https://ideone.com/cZTplL

你也可能对此有兴趣std::实验::make_array http://en.cppreference.com/w/cpp/experimental/make_array

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

编译时生成的表 的相关文章

随机推荐

  • SelectListItem 中的选定属性永远不起作用 (DropDownListFor)

    我在选择 DropDownList 的值时遇到问题 我一直在阅读所有类似的帖子 但找不到解决方案 实际的方法对我来说似乎非常好 因为我可以检查 SelectList 内的字段 var selectList new List
  • 我如何告诉 ProGuard 保留用于 onClick 的函数?

    我正在使用android onClick属性在我的 android 应用程序的一些 xml 布局文件中 但 ProGuard 在运行时从我的代码中删除了这些方法 因为我的代码中没有任何内容调用它们 我不想单独指定每个函数 而是想将它们命名为
  • jQuery:正确循环对象?

    我尝试使用以下代码片段循环访问下面显示的 JS 对象 同时需要获取索引键和内部对象 我到底应该怎么做 因为以下不起作用 物体 prop 1 1 2 prop 2 3 4 My code each myObject function key
  • SqlDependency onchange 事件无限循环

    我有一个简单的查询 并且事件在正确的时间触发 然而 一旦被解雇 该财产 HasChanges 的SqlDependency对象始终设置为true 第一次触发 OnChange 时 SqlNotificationEventArgs Info
  • 带有非字母数字字段名称的cosmos db sql查询

    我在 cosmosdb 中的数据结构是下一个 id oid 554f7dc4e4b03c257a33f75c 我需要对集 合进行排序 oid场地 我应该如何形成我的 sql 查询 普通查询SELECT TOP 10 FROM collect
  • 分段控件可在多个表视图之间切换

    我基本上尝试的是实现 Mailbox 中的控制段 表视图 在 2 00 左右查看 我正在其中使用 Core DataUITableViewController连接到一个UITableView 当用户切换UISegmentedControl
  • nunique 排除 pandas 中的某些值

    我正在计算每行的唯一值 但是我想排除值 0 然后计算唯一值 d col1 1 2 3 col2 3 4 0 col3 0 4 0 df pd DataFrame data d df col1 col2 col3 0 1 3 0 1 2 4
  • Lua math.random 不起作用

    所以我正在尝试创建一些东西 并且我到处寻找生成随机数的方法 然而 无论我在哪里测试代码 它都会产生非随机数 这是我写的一个例子 local lowdrops Wooden Sword Wooden Bow Ion Thruster Mach
  • 使用经过身份验证的 REST 请求缓存代理

    考虑以下场景 我有 RESTful URL articles 返回文章列表 用户在每个请求上使用授权 HTTP 标头提供其凭据 根据用户的权限 文章可能因用户而异 对于这种情况 是否可以使用缓存代理 例如 Squid 代理将只看到 URL
  • 如何在Golang中正确使用OAuth2获取谷歌电子邮件

    我已经尝试使用 OAuth 成功进行身份验证golang com x oauth2图书馆 provider variable is oauth2 Config scope is https www googleapis com auth u
  • xcodebuild:错误:“APP.xcworkspace”不存在

    我正在尝试使用 gitlab 设置 CI 当我尝试在本地构建时 出现此错误 xcodebuild error APP xcworkspace does not exist APP 不是真实名称 我也在使用 CocoaPods 我在终端中运行
  • 无法更新 RubyGems

    我在将 RubyGems 从版本 1 1 1 更新到最新版本时遇到困难 我尝试过以下方法 宝石更新 Result 更新已安装的 gem批量更新 Gem 源索引 http gems rubyforge org http gems rubyfo
  • 为结构变量赋值

    结构类型定义为 typedef struct student int id char name double score Student 我构造了一个 Student 类型的变量 并且想为其赋值 我怎样才能有效地做到这一点 int main
  • EVC++下的StandardSDK 4.0可以在远程设备上调试吗?

    我在跑 with 为运行 CE 5 0 的设备开发应用程序 我正在使用为此 它工作得很好 除了以下事实 虽然它以我的设备 即基于 SH4 的 PDA 为目标 但它不会让我选择 StandardSDK 模拟器以外的任何东西进行调试 如果我去工
  • Linux - TCP connect() 失败并出现 ETIMEDOUT

    对于 TCP 客户端 connect 调用 TCP 服务器 Richard Stevens 的 UNIX 网络编程 一书说道 如果客户端 TCP 未收到对其 SYN 段的响应 则返回 ETIMEDOUT 4 4BSD 例如 调用 conne
  • 为什么不能同时为结构体及其指针定义方法?

    鉴于设置第 54 张幻灯片 http tour golang org 54golang之旅 type Abser interface Abs float64 type Vertex struct X Y float64 func v Ver
  • 如何使用$.ajax(); Laravel 中的函数

    我需要通过 ajax 添加新对象 但我不知道如何在 laravel 中使用 ajax 函数 我在刀片模板中的形式是 Form open array url gt expense add method gt POST class gt for
  • 在 IntelliJ 中启用 Grails 3.x 自动重新加载

    可能并不重要 但是有人对 Grails 中的 IntelliJ 重新加载选项有疑问吗 从 IntelliJ Run App 集启动应用程序Reloading active false 我尝试通过控制台 powershwell 清理并重新启动
  • 如何使用 C# 文件 API 检查磁盘上的逻辑和物理文件大小

    如何使用 C api 读取逻辑和物理文件大小 new FileInfo path Length 是实际尺寸 至于磁盘上的大小 我认为没有 API 可以获取它 但您可以使用实际大小和簇大小来获取它 这里需要一些有关计算的信息 http soc
  • 编译时生成的表

    由于一些技巧 我能够在编译时生成一个表 但表中的值并不是很有用 例如 5x5 的表格如下所示 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 为了清楚起见 逗号的位置 创建该表的代码如下