标签: stdarray

std :: array初始化程序如何用于char?

我不确定以下代码是如何工作的.我以为你必须这样做,{'h', 'e' ...etc...}但似乎工作正常.另一方面,如果你这样做std::array<const char*只会向数组添加一个元素.字符串文字初始化是否有特殊规则?

std::array<char, strlen("hello world!") + 1> s = {"hello world!"};
for (size_t i = 0; i < s.size(); ++i)
{
    std::cout << s[i];
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11 stdarray

7
推荐指数
1
解决办法
3893
查看次数

std :: size的大小为零的数组

std::array<int,0>大小为零的数组是什么意思?

在发布之前我已经在SO中经历了类似的问题,所有这些问题都是关于简单的数组类型和C语言,并且大多数人都认为这是非法的.但是在C++中array<int,0>是允许的.

根据cppreference.com

零长度数组(N == 0)有一种特殊情况.在那种情况下,array.begin() == array.end()这是一些独特的价值.调用front()back()对零大小的数组的影响是未定义的.

为什么不将它定义为非法?

c++ stdarray

7
推荐指数
1
解决办法
637
查看次数

是否可以将std :: array移动到std :: vector中?

这是关于堆栈内存和堆内存的交互以及通过std::arraystd::vector类从堆栈到堆的特定情况的问题.

原则std::array<T>上可以看作是指向第一个元素的指针,加上一些关于数组大小的编译时间信息.是否有可能让std::vector<T>构造函数考虑到这一事实并尝试通过复制指针将内容移动arrayvectorjust中.

一个用例是,一个具有返回a的函数 std::array<double, >

std::array<double, 20> fun(){...};

但是后来决定将其分配给a std::vector而不必逐个元素地复制.

std::vector<double> v = fun(); // not working code

现在必须要做

std::array<double, 20> tmp = fun();
std::vector<double> v(tmp.begin(), tmp.end());
Run Code Online (Sandbox Code Playgroud)

这实际上是一些多余的工作,如果可能的话,这些工作是不必要的std::vector<double> v(std::move(tmp)); \\ not working code.

内存布局std::vectorstd::array是一样的,所以不是和障碍.

我知道主要的障碍可能是std::array元素在堆中,而std::vector元素在堆中.很明显,即使std::vector从堆栈中写入仍然存储器的移动构造函数也将被无可挽回地破坏.

所以我想这个问题也可以解读为:

有没有办法将内存从堆栈移动到堆(无论这意味着什么),如果可以与移动构造函数结合使用?

或者如果原则上std::vector可以从一个移动构造函数?std::array

MWE:

#include<array>
#include<vector>

std::array<double, 20> fun(){return {};} // don't change this function …
Run Code Online (Sandbox Code Playgroud)

move heap-memory stack-memory c++11 stdarray

7
推荐指数
2
解决办法
1419
查看次数

初始化std :: array而不复制/移动元素

#include <iostream>
#include <string>
#include <array>

class C {
private:
    std::string a;
    std::string b;
    std::string c;
public:
    C(std::string a_, std::string b_, std::string c_) : a{a_},b{b_},c{c_} {}
    ~C(){};
    C(const C&) =delete;
    C(const C&&) =delete;
    const C& operator=(const C&) =delete;
    const C& operator=(const C&&) =delete;
};

std::array<C,2> array = {C("","",""),C("","","")};

int main()
{}
Run Code Online (Sandbox Code Playgroud)

这将无法编译(带有NDK和clang的Android Studio)带有"调用已删除的c构造函数"错误.我知道我可以使用a std::vectoremplace_back()直接在容器内构造一个元素,但在我的代码中我只想使用固定大小的容器和不可复制/可移动的对象进行优化.我可能在这里缺少基本的,但是没有一种方法来初始化,std::array而不必先构建单个元素然后将它们复制到那里?

c++ initialization copy-constructor c++11 stdarray

7
推荐指数
2
解决办法
864
查看次数

初始化类型为枚举类的二维std ::数组(C++ 11)

我在C++ 11中有以下类:

class MyTable
{
    public:
    enum class EntryType
    {
        USED, FREE
    };

    MyTable(EntryType value)
    {
        for (uint32_t i = 0; i < 10; ++i)
        {
            memset(_table[i].data(), (int)value, sizeof(_table[0][0]) * 50);
        }
    }
    array<array<EntryType, 50>, 10> _table;
}
Run Code Online (Sandbox Code Playgroud)

尝试使用EntryType :: FREE的值构造MyTable的对象,二维数组中的每个项的值都为0x01010101(每8位1b),而不是仅为0x1的预期值

我猜它与我的value被投入有关int,但我不知道为了解决这个问题我应该做些什么.

c++ initialization c++11 enum-class stdarray

7
推荐指数
1
解决办法
785
查看次数

为什么使用堆叠的std :: array声明多维数组需要"双括号"?

#include <array>
using std::array;

constexpr auto d1=2;
constexpr auto d2=3;
constexpr auto d3=4;

// stacked std::array
using arr_t   = array<int,d1>;
using arr2d_t = array<arr_t,d2>;
using arr3d_t = array<arr2d_t,d3>;
constexpr arr3d_t arr1 = {{
    {{  {1,2},  {3,4},  {5,6}  }},
    {{  {1,2},  {3,4},  {5,6}  }},
    {{  {1,2},  {3,4},  {5,6}  }},
    {{  {1,2},  {3,4},  {5,6}  }}
}};

// built-in array
using carr3d_t = int[d3][d2][d1];
constexpr carr3d_t arr2 = {
    { {1,2}, {3,4}, {5,6} },
    { {1,2}, {3,4}, {5,6} },
    { {1,2}, {3,4}, {5,6} …
Run Code Online (Sandbox Code Playgroud)

c++ arrays c++11 stdarray

7
推荐指数
2
解决办法
943
查看次数

为什么在std :: array size_t和std :: vector中的size_type通常是size_t?

该文档说,size_typestd::vector是/ usually / size_t,这是合理的,因为实现可以选择使用不同的东西。

但是为什么size_type = size_tstd::array。特别是在这里,因为std::array使用很少µC,所以最好使实现具有一定的自由度。

这是文档缺陷吗?

c++ size-t stdvector stdarray

7
推荐指数
1
解决办法
101
查看次数

编译时对 std::array 的引用大小不可用

我很想知道为什么static_assert下面代码中的第二个不起作用。似乎即使数组c是对 的引用a,数组的大小也嵌入在类型中,因此它应该在编译时可用。

#include <array>

int main()
{
    std::array<int,2> a = {1,2};
    std::array<int,2> b = {2,3};
    std::array<int,2>& c = a;

    static_assert(a.size() == b.size(), "a.size==b.size"); // ok size of array is compile time constant
    static_assert(c.size() == a.size(), "c.size==a.size"); // compiler error "static_assert expression is not an integral constant expression"
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays static-assert stdarray

7
推荐指数
1
解决办法
517
查看次数

使用std :: array作为boost :: spirit :: x3的Attribute

我正在尝试std::array使用boost :: spirit的最新版本x3(包含在boost 1.54中)将数字列表解析为固定大小的容器.由于std::array具有必要的功能,因此它被检测为容器,但缺少插入功能,使其不兼容.这是我想要完成的一个简短示例:

#include <boost/spirit/home/x3.hpp>

#include <array>
#include <iostream>
#include <string>

namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;

typedef std::array<double, 3> Vertex;

int main(int, char**) {
  using x3::double_;
  using ascii::blank;

  std::string input = "3.1415 42 23.5";
  auto iter = input.begin();

  auto vertex = x3::rule<class vertex, Vertex>{} =
    double_ >> double_ >> double_;

  Vertex v;

  bool const res = x3::phrase_parse(iter, input.end(), vertex, blank, v);
  if (!res || iter != input.end()) return EXIT_FAILURE;

  std::cout << "Match:" …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit stdarray c++14 boost-spirit-x3

6
推荐指数
1
解决办法
401
查看次数

示例,其中std :: array :: max_size和std :: array :: size给出不同的结果

每当我尝试max_size()和使用size()std::array,我得到相同的结果,我想知道是否可能存在两种情况给出不同结果的情况.

c++ arrays stl stdarray

6
推荐指数
2
解决办法
608
查看次数