草庐IT

ITERATOR

全部标签

c++ - 为什么我不能在一个序列中放置多个元素?

C++11引入了emplace函数来在序列中就地构造元素。这是对复制或移动元素的insert的补充。但是,在insert的多个重载中,只有单个元素插入版本,即iteratorinsert(const_iteratorp,Tconst&x);iteratorinsert(const_iteratorp,T&&x);有一个emplace版本,templateiteratoremplace(const_iteratorp,Args&&...x);有什么理由不允许使用emplace就地构造n元素吗?当重载时,templateiteratoremplace(const_iteratorp,siz

c++ - valarray 上 STL 算法 "count"的返回类型是什么

我正在使用VisualStudio2010Pro在Windows764bit上机器,我想使用count(来自header)在valarray上:intmain(){valarrayv(false,10);for(inti(0);i上面程序的输出是正确的:4但是我想将值分配给变量并使用int导致编译器警告精度损失。自valarray没有迭代器,我不知道如何使用iterartor::difference_type.这有可能吗? 最佳答案 Num的正确类型会是:typenameiterator_traits::difference_typ

c++ - 如何在 boost::transform_iterator 中使用 phoenix 表达式?

和往常一样,这个问题是错误的。实际问题是:为什么transform_iterator不使用传统的result_of元函数来确定返回类型,而是直接访问UnaryFunc::result_type。发布了一个解决方法的答案。具体来说,是否有办法使phoenix表达式按照std::unary_function概念的预期公开result_type类型?boost::transform_iterator似乎预料到了这一点,从它的src来看,我没有看到一个简单的解决方法。下面是一些重现我遇到的问题的代码:#include#include#include#includeusingnamespaceb

c++ - OpenMP、C++ 和迭代器

要遍历容器的元素,我通常会使用迭代器,如下所示:containermyContainer;//fillupthecontainercontainer::iteratorit;for(it=myContainer.begin();it!=myContainer.end();++it){//dostufftotheelementsofthecontainer}现在,如果我想使用OpenMP并行化循环,我可能会尝试类似的方法:containermyContainer;//fillupthecontainercontainer::iteratorit,it_begin=myContainer.

c++ - 在 nim 中包装嵌套的模板化类型

我有一个像这样的C++类型:templateclassVector{structIterator{};};在C++中我可以使用Iterator作为Vector::Iterator.我如何包装它以便从Nim使用它?c2nim发射typeVector[T]{.importcpp...}=objecttypeIterator[T]{.importcpp...}无法编译因为nim没有嵌套类型,并且会产生Vector::Iterator而不是Vector::Iterator.我可以在Nim中使用非嵌套类型:typeVectorIterator[T]{.importcpp:"Vector::Ite

c++ - 使用模板类 boost python

我创建了一个环形缓冲区,我想使用boost将该类导入Python。当我尝试这样做时,它会出错。ring.cpp:Infunction‘voidinit_module_ring()’:ring.cpp:130:16:error:wrongnumberoftemplatearguments(1,shouldbe4)class_("Ring").Pleasehelpme.ThanksinAdvance.这是我的代码:#include#includeusingnamespacestd;usingnamespaceboost::python;templateclassRing{public:cl

c++ - 输出迭代器适配器计数但不复制

有多种STL算法依赖于输出迭代器来存储算法的结果。例如,std::set_intersection会将两个已排序范围之间的所有公共(public)元素存储在一个输出迭代器中,然后每个元素输出后递增。有时,我对实际元素不感兴趣,只对输出元素的数量感兴趣。在这种情况下,复制元素会浪费内存和性能。有没有我可以用来计算和避免元素拷贝的迭代器适配器?如果不能,您能否建议此类适配器的通用实现? 最佳答案 Boost的FunctionOutputIterator可以为所欲为:std::size_tcount=0u;intarr[]{0,1,2,3

c++ - 如何实现构造函数,使其只接受使用 typeid 的输入迭代器?

我想为某个对象实现一个范围构造函数,但我想限制它只接受两个输入迭代器。我尝试用gcc7.1.0编译这段代码。文件test.cpp#include#include#includetemplateusingtraits=typenamestd::iterator_traits::iterator_category;templateclassA{private:std::vectorv;public:template)==typeid(std::input_iterator_tag)>>A(InputIteratorfirst,InputIteratorlast):v(first,last)

c++ - Boost any_range 与 "canonical form"- 后者是什么?

Boost的any_range文档说明如下:Despitetheunderlyingany_iteratorbeingthefastestavailableimplementation,theperformanceoverheadofany_rangeisstillappreciableduetothecostofvirtualfunctioncallsrequiredtoimplementincrement,decrement,advance,equaletc.Frequentlyabetterdesignchoiceistoconverttoacanonicalform.作者所说的

c++ - 授予对封装容器的访问权限

classX{public:typedefstd::listContainer;//(1)constContainer&GetElements()const;//(2)Container::iteratorElementBegin();Container::iteratorElementEnd();//(3)CustomIteratorGetElementIterator();private:Containerm_container;};我正在寻找一种一致且干净的方法来为调用者提供封装容器的迭代器。我想到了上面源码中标注的三个思路。提供size()、begin()和end(),非常适合