English 中文(简体)
寻找一个捷径:先天者——连续分类
原标题:Looking for a shortcut of std::initializer_list with consecutive integers

我有一席长篇的原始阵列,例如:

const std::array<int, 16> a{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
const std::array<int, 16> b{16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
...

是否有更简明的初始化办法,例如按照这些思路(法律):

const std::array<int, 16> a{std::views::iota(0, 16)};
const std::array<int, 16> b{std::views::iota(16, 32)};
...
问题回答

<代码>std:index_sequence, 您可做以下工作:

template <std::size_t N>
constexpr std::array<int, N> make_iota_array(int start = 0)
{
    return []<std::size_t... Is>(std::index_sequence<Is...>){
        return std::array<int, N>{static_cast<int>(start + N)...};
    }(std::make_index_sequence<N>());
}

您可使用lambda 启动std:array with iota_view/code>:

template<class T, size_t begin, size_t end>
constexpr std::array iota_arr = [] {
  constexpr auto iota = std::views::iota(begin, end);
  std::array<T, iota.size()> arr;
  std::ranges::copy(iota, arr.begin());
  return arr;
}();

constexpr auto a = iota_arr<int, 0, 16>;
constexpr auto b = iota_arr<int, 16, 32>;

是否有更简明的初始化方法

是的。 如下文所示,您可撰写一份助手make_array

#include <iostream>
#include <type_traits>
#include <array>

template<std::size_t start, std::size_t end>
std::enable_if_t<(end > start), std::array<int, end - start>> constexpr make_array()
{
    std::array<int, end - start> tempArray{};
    std::size_t count = 0;
    for(int &elem:tempArray)
    {
        elem = start + count;
        count++;
    }
    return tempArray;
}

int main()
{
    constexpr auto arr = make_array<16, 32>();
}

https://godbolt.org/z/enGYWG756”rel=“nofollow noreferer”>





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?