编译时生成的表

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(使用前将#替换为@)

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

  • 如何验证文件名称在 Windows 中是否有效?

    是否有一个 Windows API 函数可以将字符串值传递给该函数 该函数将返回一个指示文件名是否有效的值 我需要验证文件名是否有效 并且我正在寻找一种简单的方法来完成此操作 而无需重新发明轮子 我正在直接使用 C 但针对的是 Win32
  • ASP.NET Core Serilog 未将属性推送到其自定义列

    我有这个设置appsettings json对于我的 Serilog 安装 Serilog MinimumLevel Information Enrich LogUserName Override Microsoft Critical Wr
  • 将数组向左或向右旋转一定数量的位置,复杂度为 o(n)

    我想编写一个程序 根据用户的输入 正 gt 负 include
  • 实时服务器上的 woff 字体 MIME 类型错误

    我有一个 asp net MVC 4 网站 我在其中使用 woff 字体 在 VS IIS 上运行时一切正常 然而 当我将 pate 上传到 1and1 托管 实时服务器 时 我得到以下信息 网络错误 404 未找到 http www co
  • 指针问题(仅在发布版本中)

    不确定如何描述这一点 但我在这里 由于某种原因 当尝试创建我的游戏的发布版本进行测试时 它的敌人创建方面不起作用 Enemies e level1 3 e level1 0 Enemies sdlLib 500 2 3 128 250 32
  • C 预处理器库

    我的任务是开发源分析工具C程序 并且我需要在分析本身之前预处理代码 我想知道什么是最好的图书馆 我需要一些重量轻 便于携带的东西 与其推出自己的 为什么不使用cpp这是的一部分gcc suite http gcc gnu org onlin
  • 使用 System.Text.Json 即时格式化 JSON 流

    我有一个未缩进的 Json 字符串 例如 hash 123 id 456 我想缩进字符串并将其序列化为 JSON 文件 天真地 我可以使用缩进字符串Newtonsoft如下 using Newtonsoft Json Linq JToken
  • 在 ASP.NET Core 3.1 中使用包含“System.Web.HttpContext”的旧项目

    我们有一些用 Net Framework编写的遗留项目 应该由由ASP NET Core3 1编写的API项目使用 问题是这些遗留项目正在使用 System Web HttpContext 您知道它不再存在于 net core 中 现在我们
  • C# 中的递归自定义配置

    我正在尝试创建一个遵循以下递归结构的自定义配置部分
  • Github Action 在运行可执行文件时卡住

    我正在尝试设置运行google tests on a C repository using Github Actions正在运行的Windows Latest 构建过程完成 但是当运行测试时 它被卡住并且不执行从生成的可执行文件Visual
  • for循环中计数器变量的范围是多少?

    我在 Visual Studio 2008 中收到以下错误 Error 1 A local variable named i cannot be declared in this scope because it would give a
  • Qt表格小部件,删除行的按钮

    我有一个 QTableWidget 对于所有行 我将一列的 setCellWidget 设置为按钮 我想将此按钮连接到删除该行的函数 我尝试了这段代码 它不起作用 因为如果我只是单击按钮 我不会将当前行设置为按钮的行 ui gt table
  • C++ 复制初始化和直接初始化,奇怪的情况

    在继续阅读本文之前 请阅读在 C 中 复制初始化和直接初始化之间有区别吗 https stackoverflow com questions 1051379 is there a difference in c between copy i
  • 控制到达非 void 函数末尾 -wreturn-type

    这是查找四个数字中的最大值的代码 include
  • C - 直接从键盘缓冲区读取

    这是C语言中的一个问题 如何直接读取键盘缓冲区中的数据 我想直接访问数据并将其存储在变量中 变量应该是什么数据类型 我需要它用于我们研究所目前正在开发的操作系统 它被称为 ICS OS 我不太清楚具体细节 它在 x86 32 位机器上运行
  • 为什么 C# Math.Ceiling 向下舍入?

    我今天过得很艰难 但有些事情不太对劲 在我的 C 代码中 我有这样的内容 Math Ceiling decimal this TotalRecordCount this PageSize Where int TotalRecordCount
  • Validation.ErrorTemplate 的 Wpf 动态资源查找

    在我的 App xaml 中 我定义了一个资源Validation ErrorTemplate 这取决于动态BorderBrush资源 我打算定义独特的BorderBrush在我拥有的每个窗口以及窗口内的不同块内
  • C 中的异或运算符

    在进行按位操作时 我在确定何时使用 XOR 运算符时遇到一些困难 按位与和或非常简单 当您想要屏蔽位时 请使用按位 AND 常见用例是 IP 寻址和子网掩码 当您想要打开位时 请使用包含或 然而 XOR 总是让我明白 我觉得如果在面试中被问
  • 如何在 C++ BOOST 中像图形一样加载 TIFF 图像

    我想要加载一个 tiff 图像 带有带有浮点值的像素的 GEOTIFF 例如 boost C 中的图形 我是 C 的新手 我的目标是使用从源 A 到目标 B 的双向 Dijkstra 来获得更高的性能 Boost GIL load tiif
  • 恢复上传文件控制

    我确实阅读了以下帖子 C 暂停 恢复上传 https stackoverflow com questions 1048330 pause resume upload in c 使用 HTTP 恢复上传 https stackoverflow

随机推荐

  • 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 为了清楚起见 逗号的位置 创建该表的代码如下