草庐IT

BEGIN_ARRAY

全部标签

c++ - 为什么在 C++11 中为 std::initializer_list 重载 std::begin() 和 std::end()?

在C++11(引用N3337)中,std::begin()和std::end()被指定为(§24.7[iterator.range]/p2-3)templateautobegin(C&c)->decltype(c.begin());templateautobegin(constC&c)->decltype(c.begin());2Returns:c.begin().templateautoend(C&c)->decltype(c.end());templateautoend(constC&c)->decltype(c.end());3Returns:c.end().但是,std::in

c++ - std::array 聚合初始化需要大量令人困惑的花括号

我有以下代码:enumclassMessageDeliveryMethod{POST_MASTER,BUBBLE,NUM_ENUMERATORS};namespace{usingMapType=std::array,static_cast(MessageDeliveryMethod::NUM_ENUMERATORS)>;MapTypeg_mapping={{{"POST_MASTER",MessageDeliveryMethod::POST_MASTER},{"BUBBLE",MessageDeliveryMethod::BUBBLE},}};}这可以编译,但我不知道为什么。g_map

c++ - 是否有标准名称(模板或宏)来替换 ARRAY_SIZE、_countof 等?

我不是在谈论std::array或任何东西,只是经典的VanillaC/C++数组。我知道可以实现ARRAY_SIZE/_countof的各种方式,我只是想知道他们是否已经设法为此标准化了一个名称(在std::我假设)。如果没有,是否有相关建议? 最佳答案 当前的解决方法std::extent-数组的大小如果您正在使用native数组,您可以使用std::extent来自,用于生成数组的Nth维(默认为第一个)中的元素数。inta1[1024];inta2[std::extent::value];//int[1024]一点间接(通用

c++ - 为什么二维 std::array 不能用两层列表初始化器初始化?

有人能帮我理解为什么我的编译器不能/不能推断出这个吗?(使用g++7.3)不起作用:#includestd::array,2>f(){return{{0,0},{0,0}};}工作正常:#includestd::array,2>f(){return{std::array{0,0},{0,0}};}同样奇怪的是,这也失败了:#includestd::array,2>f(){returnstd::array,2>{{0,0},{0,0}};}@1201ProgramAlarm指出添加另一组花括号是可行的:#includestd::array,2>f(){return{{{0,0},{0,0

c++ - 将 std::array 作为模板可变参数函数的参数传递

我正在尝试了解C++11中的可变参数模板。我有一个类,它基本上是std::array的包装器。我希望能够将函数对象(最好是lambda)传递给成员函数,然后将std::array的元素作为函数对象的参数传递。我使用了static_assert来检查参数的数量是否与数组的长度匹配,但我想不出一种方法来将元素作为参数传递。这是代码#include#include#include#includeusingnamespacestd;templatestructContainer{templateContainer(Ts&&...vs):data{{std::forward(vs)...}}{s

c++ - 追加到 std::array

因为我无法找到这样的函数(不正确?),我正在尝试创建一个编译时函数(constexpr)函数,它接受std::arrayarr。和一个Tt并返回一个新的std::array与t添加到arr的末尾.我从这样的事情开始:templateconstexprstd::arrayappend(std::arraya,Tt);templateconstexprstd::arrayappend(std::arraya,Tt){returnstd::array{t};}templateconstexprstd::arrayappend(std::arraya,Tt){returnstd::array{

c++ - std::make_array 的当前状态

提议的std::make_array函数的当前状态是什么here?我找不到任何关于它可能被接受的信息。根据cppreference.com,它位于std::experimental命名空间中。C++compilersupport上根本没有提到它也不在Wikipedia-C++17,Wikipedia-C++20,和C++17标准草案。 最佳答案 正如@DeiDei所写,C++17包括templateargumentdeductionforclasses,所以你现在可以写:std::pairp(foo,bar);std::arraya

c++ - 获取错误 "array bound is not an integer constant before ' ]' token"

我正在尝试使用数组实现堆栈,但收到错误消息。classStack{private:intcap;intelements[this->cap];//cap=5;this->top=-1;};指示的行有这些错误:Multiplemarkersatthisline-invaliduseof'this'attoplevel-arrayboundisnotanintegerconstantbefore']'token我做错了什么? 最佳答案 在C++中,数组的大小必须是编译时已知的常量。如果不是这种情况,您将收到错误消息。在这里,你有inte

c++ - 为什么我不能执行 std::map.begin() + 1?

我有一个std::map,我想从第二个条目开始对其进行迭代。我可以很好地解决这个问题,但我对为什么“显而易见”的语法无法编译感到困惑。错误消息没有帮助,因为它引用了std::string,我在这里没有使用它。这是一些代码//SupposeIhavesomemap...std::mappSomeMap;//Thisisfine...std::map::const_iteratorpIterOne=pSomeMap.begin();++pIterOne;//Thisdoesn'tcompile...std::map::const_iteratorpIterTwo=pSomeMap.begi

c++ - 如果我使用 vector::begin() 而不是 std::back_inserter(vector) 作为 set_intersection 的输出会怎样?

我一直在使用高度简洁和直观的C​​++语法来查找两个排序的vector的交集并将结果放入第三个vector:vectora,b,c;//...std::set_intersection(a.begin(),a.end(),b.begin(),b.end(),std::back_inserter(c));这应该将c设置为intersection(a,b),假设a和b已排序。但是如果我只使用c.begin()会怎么样(我想我在某个地方看到了一个例子,这就是我这样做的原因):std::set_intersection(a.begin(),a.end(),b.begin(),b.end(),c