草庐IT

initialization-vector

全部标签

c++ - 向 std::vector 或 typedef 添加自定义方法

所以我有以下代码可以在vector中“搜索”对象的字符串。#include#include#include#include#includestructmigObj{migObj(conststd::string&a_name,conststd::string&a_location):name(a_name),location(a_location){}std::stringname;std::stringlocation;};intmain(){typedefstd::vectormigVec;migVecv;v.push_back(migObj("fred","belfast"));

c++ - 从 initializer_list 错误构造 std::map

我正在尝试创建一个类构造函数,它将接受一个初始化列表并用它初始化一个映射,如下所示:classTest{std::mapm_ints;public:Test(std::initializer_list>init):m_ints(init){}};但这会导致很长的错误消息,坦率地说,我不明白。我需要更改什么才能使这项工作正常进行? 最佳答案 声明std::initializer_list的模板参数具有类型std::pair这是一个演示程序#include#include#includeclassTest{std::mapm_ints;

c++ - 如何从 mpl::vector 实例化模板

我有一个mpl::vector并且想使用vector元素作为模板参数来实例化一个模板。这是怎么做到的?可以使用参数包来合并额外的mpl::vector元素吗?例如:structA;structB;structC;structD;usingargs=mpl::vector;templatestructderived_type;usingtype_from_vector=derived_type;处理此类问题的最佳方法是什么?谢谢。 最佳答案 [全面披露:我是Boost.Hana的开发者]我知道这个问题是关于Boost.MPL的,但让我

c++ - 冒号 : used in variable initialization?

这个问题在这里已经有了答案:Whatdoesacoloninastructdeclarationmean,suchas:1,:7,:16,or:32?(3个答案)关闭7年前。我找到这条线here:uint32bIsHungry:1;...而且我从未见过这种用于初始化变量的语法。我已经习惯看到这个了:uint32bIsHungry=1;它看起来有点像一个初始化列表,但是对于单个字段?它是什么,它有什么作用,我为什么要关心?

c++ - 如何将可变参数模板成员函数的参数存储到 vector 中?

#include#include#include#includeusingnamespacestd;classCTest{public:templatevoidAddStringsToVector(conststd::string&First,Args&...args);private:std::vectorm_stringsvec;};templatevoidCTest::AddStringsToVector(conststd::string&First,Args&...args){m_stringsvec.push_back(First);m_stringsvec.push_bac

c++ - 将原始字节数组复制到空字节 vector 中的最有效方法

我有一个场景,我需要将原始动态分配的uint8_t数组的内容复制到一个vector中(无论何时发生这种情况,它都保证为空).vectormyVector;constuint8_t*myRawArray;复制操作尽可能高效且可移植(可能会使用各种编译器版本)对我来说非常重要。我想到的一种方法是:myVector.reserve(byteCount);myVector.insert(myVector.begin(),myRawArray,myRawArray+byteCount);关于速度与这个相比如何的任何想法:myVector.resize(byteCount);memcpy(myVe

c++ - 使用迭代器删除元素,不知道 vector

我有一个情况。我为我的一项任务使用了模板化函数。对于这个函数,我通过引用传递迭代器。现在,我必须从vector中删除几个元素。我如何仅使用迭代器来做到这一点?请找到相应的代码:templateboolSomeFunc(BidirectionalIterator&first,BidirectionalIterator&last,IteratoranotherVecBegin){while((first+1)!=last){if(some_condition)//delete(first);HOW?elseif(some_other_condition)//delete(first+1);

c++ - 默认插入 vector 不是默认初始化吗?

其中一个std::vector构造函数被规定为,强调我的:explicitvector(size_typen,constAllocator&=Allocator());Effects:Constructsavectorwithndefault-insertedelementsusingthespecifiedallocator.Requires:TshallbeDefaultInsertableinto*this.Complexity:Linearinn.默认插入与默认初始化有任何关系吗?在这段代码上:std::vectorv(66000);gcc5.2优化产生:400d18:bfd0

c++ - 从类型列表创建 vector 元组

我有一个简单的类型列表实现;templatestructTypelist{staticconstexprsize_tcount{sizeof...(Ts)};};我想用它做的是生成一个std::tuple的std::vector>对于类型列表中的每种类型;例如:structA{};structB{};structC{};usingmyStructs=typelist;usingmyList=tupleOfVectorTypes;tuple,vector,vector>这就是我一直在玩的东西:templateclassT>structList{usingtype=std::tuple..

c++ - 如何获得 vector 的排序索引?

我有一个vector。它没有排序。现在我想得到它的索引,它将对vector进行排序。例如vectorv{1,3,2},排序索引为{0,2,1}因为v[0].如果两个相等,哪个先走并不重要。 最佳答案 您正在寻找的称为标记排序(或索引排序)。这是在C++11中使用lambda的最小示例:#include#include#include#includetemplatestd::vectortag_sort(conststd::vector&v){std::vectorresult(v.size());std::iota(std::beg