标签: stdarray

C风格阵列的替代品有多可行?

现在,我们有std::array,std::vector和括号的初始化,是C风格数组仍然需要?

c++ arrays c++11 list-initialization stdarray

3
推荐指数
1
解决办法
296
查看次数

"解包"std :: array <T,N>作为函数的参数

这是一个非常好的(不是我的)示例,你可以如何扩展(或"爆炸")元组作为函数的参数:

template<int ...I> struct index_tuple_type {
  template<int N> using append = index_tuple_type<I..., N>;
};

template<int N> struct make_index_impl {
  using type = typename make_index_impl<N-1>::type::template append<N-1>;
};

template<> struct make_index_impl<0> { using type = index_tuple_type<>; };

template<int N> using index_tuple = typename make_index_impl<N>::type;

template <typename I, typename ...Args>
struct func_traits;

template <typename R, int ...I, typename ...Args>
struct func_traits<R, index_tuple_type<I...>, Args...> {
  template <typename TT, typename FT>
  static inline R call(TT &&t, FT &&f) {
    return f(std::get<I>(std::forward<TT>(t))...);
  }
};

template<
  typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 stdtuple stdarray

3
推荐指数
1
解决办法
1182
查看次数

emplace 和 unordered_map&lt;?, std::array&lt;?, N&gt;&gt;

我有一个std::unordered_map<string, std::array<int, 2>>. emplace将值放入地图的语法是什么?

unordered_map<string, array<int, 2>> contig_sizes;
string key{"key"};
array<int, 2> value{1, 2};

// OK ---1
contig_sizes.emplace(key, value);

// OK --- 2
contig_sizes.emplace(key, std::array<int, 2>{1, 2});

// compile error --3
//contig_sizes.emplace(key, {{1,2}});

// OK --4 (Nathan Oliver)
// Very inefficient results in two!!! extra copy c'tor
contig_sizes.insert({key, {1,2}});

// OK --5
// One extra move c'tor followed by one extra copy c'tor
contig_sizes.insert({key, std::array<int, 2>{1,2}});

// OK --6 
// Two extra move constructors
contig_sizes.insert(pair<const string, array<int, …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary unordered-map stdarray emplace

3
推荐指数
1
解决办法
1403
查看次数

C++ 14使用可变参数模板编译时间std :: array

我想使用c ++ 14可变参数模板构建编译时查找表.目前我在那里:

static const unsigned kCount = 5;

template<unsigned Index>
constexpr auto getRow(void)
{
    return std::array<unsigned, 2> { Index, Index * Index };
}

template<unsigned... Indices>
constexpr auto generateTable(std::index_sequence<Indices...>)
{
    return std::array<std::array<unsigned, 2>, sizeof...(Indices)>
    {
        // This is were I'm stuck. How to build a std::array using Indices as template parameter in getRow()?
    };
}

constexpr auto generate(void)
{
    return generateTable(std::make_index_sequence<kCount>{});
}
Run Code Online (Sandbox Code Playgroud)

我希望桌子在一个std::array.每行包含std::array2列.我被困在generateTable()我需要以某种方式将我的指数传递getRow()给模板参数的地方.

这是可以实现的使用std::integer_sequence和模板参数包扩展还是我需要自己实现递归?

(getRow()简化 - 值类型实际上来自模板类型. …

c++ variadic-templates c++11 stdarray c++14

3
推荐指数
1
解决办法
718
查看次数

是否可以以编程方式初始化 constexpr std::array 成员

让我们假设我想写一个结构体,它有一个成员 constexpr std::array ,它包含前 N 个 fibs,其中 N 是一个模板参数。

类似这样的东西,但 vals 在编译时可用:

template <int N>
struct first_n_fibs {
    static_assert(N>0);
    static const std::array<int, N> vals;
    static std::array<int, N> init_fibs(){
        std::array<int,N> result;
        if (N==1) {
            return std::array<int,N>{1};
        } else {
            result[0]=1;
            result[1]=1;
            for(int i =2; i<N;++i) {
                result[i]=result[i-2]+result[i-1];
            }
        }
        return result;
    }
};

template<int N>
const std::array<int, N> first_n_fibs<N>::vals=init_fibs();


int main(){
    std::cout << first_n_fibs<2>::vals.back() << std::endl;
    std::cout << first_n_fibs<5>::vals.back() << std::endl;
    std::cout << first_n_fibs<6>::vals.back() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我怀疑没有解决方法,因为 std::array 构造函数不是 constexpr,所以如果有人知道任何涉及 …

c++ constexpr stdarray

3
推荐指数
1
解决办法
3390
查看次数

std :: array的部分模板参数推导或解决方法?

C++ 17允许我们std::array推导出模板参数.我可以写

std::array ints = { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)

并且ints将型的std::array<int, 3>.

我的问题是:如果我只想指定数组的类型参数但是自动确定数组的大小,该怎么办?

以下不起作用,因为似乎必须指定所有模板参数:

std::array<size_t> sizes = { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)

我的编译器抱怨并说:'std :: array':模板参数太少了.

是否可以通过模板参数推导自动确定数组的大小?如果没有,是否可以通过仅指定其类型而不是其大小来创建数组?

c++ templates stdarray c++17

3
推荐指数
1
解决办法
165
查看次数

std::array 函数的重点是什么?

在 C++array实现中,empty提供了一种方法。但是,正如实施所建议的那样:

constexpr bool
empty() const noexcept { return size() == 0; }
Run Code Online (Sandbox Code Playgroud)

这仅在大小array为 0 时返回 true 那么它的意义何在?

的典型用例std::array是这样的:std::array<type,size> a = {init here maybe};a[index] = value;之后。

是否有任何用例可以声明零长度数组或从空检查有用的地方获取它?

c++ stdarray

3
推荐指数
1
解决办法
111
查看次数

获取多维可变参数 std::array 的根类型

我嵌套了 std::array,其维度由模板参数 std::array<type, n, ...> 处理。

我想知道如何在 std::array<std::array<int, 2>, 2> 中获得 int 类型。

在这种情况下,我们可以只做 std::array<std::array<int, 2>, 2>::value_type::value_type,但是我如何才能做到这一点而不必放置与维度一样多的 value_types?

c++ stdarray

3
推荐指数
1
解决办法
64
查看次数

是否可以在容器中放置 std::array?如果是怎么样?如果不是,为什么?

鉴于简单的代码

#include <array>
#include <vector>
int main() {
    std::vector<std::array<int,3>> v;
    v.emplace_back(std::array<int,3>{1,2,3});
}
Run Code Online (Sandbox Code Playgroud)

我首先担心到底发生了什么。

我的理解emplace_back是,它通过将其参数转发到该元素的构造函数来就地构造一个元素(在本例中为std::array<int,3>具有值1, 2, 3)。

但是哪个构造函数?由于我们正在传递一个与我们想要放置的元素类型相同的对象,我猜选择了移动构造函数,因为我们正在传递一个临时构造函数,或者如果移动构造函数被删除(隐式或显式),则传递复制构造函数。此外,我们首先在构建那个临时的,而且没有到位,对吧?

因此,我们基本上调用“正常”(580)为构造器std::array<int,3>通过传递到它12以及3作为参数,从而取回一个临时的,被传递到复制构造函数的std::array<int,3>该代替构建拷贝,或优选到std::array<int,3>其中 (?) 复制底层 C 样式指针的移动 ctor ?

很明显,这个简单的例子让我很困惑。

更让我困惑的是,如果我真的想利用emplace_back,我只能将参数传递给std::array<int,3>,如

#include <array>
#include <vector>
int main() {
    std::vector<std::array<int,3>> v;
    v.emplace_back(1,2,3);
}
Run Code Online (Sandbox Code Playgroud)

它甚至不编译。

c++ c++11 stdarray emplace

3
推荐指数
1
解决办法
466
查看次数

安全地初始化一个 std::array 的布尔值

鉴于此数组声明和初始化:

std::array<bool, 20> invalid_params{};
Run Code Online (Sandbox Code Playgroud)

我可以假设数组中的所有元素都将始终初始化为 false,还是最好明确地进行初始化?

c++ stdarray zero-initialization default-initialization

3
推荐指数
1
解决办法
74
查看次数