草庐IT

STL_container_type

全部标签

c++ - 将 STL 算法与 shared_ptr、函数对象一起使用

我有一组shared_ptr,我想将remove_copy_if与谓词的自定义函数对象一起使用。我不知道“最好”的方法。现在,我已经开始工作了:classCellInCol:publicstd::unary_function,bool>{public:CellInCol(size_tcol):_col(col){}booloperator()(conststd::shared_ptr&a)const{return(a->GetX()==_col);}private:size_t_col;};typedefstd::set,CellSorter>Container;Container_g

c++ - 使用指向在结构内声明的字段的指针(又名 CONTAINING_RECORD 宏)计算指向整个结构的指针的可移植方法

例如,在Winnt.h中定义了众所周知的CONTAINING_RECORD()宏:#defineCONTAINING_RECORD(address,type,field)((type*)(\(PCHAR)(address)-\(ULONG_PTR)(&((type*)0)->field)))或在FreeBSD中:#defineCONTAINING_RECORD(addr,type,field)\((type*)((vm_offset_t)(addr)-(vm_offset_t)(&((type*)0)->field)))或在Linux中:#defineoffsetof(TYPE,MEM

c++ - 为什么 STL 数据结构需要完全定义的类型

在寻找thisquestion的解决方案时,我找到了这个threadonanotherforum,它表示该标准要求完全定义STL-Datastructure的所有模板参数。这意味着生成一个结构,该结构在其内部存储其自身类型的元素会调用未定义的行为。然而,据我所知,大多数C++11之前的数据结构(即std::vector、std::map等)都没有捕获到这一点。在STL数据结构中使用不完整类型实际上可能是什么问题?或者更确切地说,以下代码可能导致什么潜在危险:#include#includestructTest{std::mapm_map1;};intmain(){return1;}或者

c++ - 在 C++ 中,在 STL 容器中存储具有重载 "operator&"的类对象是否合法?

根据C++03标准(23.1/3),只有copy-constructible类对象可以存储在STL容器中。Copy-constructible在20.1.3中描述并且要求“&”产生对象的地址。现在假设我有这个类:classClass{public:Class*operator&(){//dosomeloggingreturnthis;}constClass*operator&()const{//dosomeloggingreturnthis;}//whateverelse-assumeitdoesn'tviolaterequierements};此类对象是否可以合法存储在STL容器中?

c++ - 如何以优雅的方式将 vector 复制到STL中的 map

目前,我在vector中有一些数据。目前,我想将vector转换为map。所以它会组织如下(N是偶数)。vector:元素1、元素2、元素3、元素4...元素N。map:key1:元素1,value1:元素2,key2:元素3value2:元素4...目前,我只是枚举vector,有没有其他优雅的方式来做到这一点。C++11是首选。谢谢。for(intx=0;x 最佳答案 您的代码有效(注意MichaelJ的建议,如果有奇数则不处理最后一个元素)。有一点可以改进。调用map[vec[x]]使用value_type的默认构造函数构造一

c++ - C++ STL 容器中的多态性

我想知道是否存在解决此问题的优雅解决方案。假设如下:classBase{private:intdata;protected:Base(intdata):data(data){}intgetData()const{returndata;}virtualboolsomeOp()=0;}classDerived1:publicBase{private:intdata2;public:Derived1(intdata,intdata2):Base(data),data2(data2){}intgetData2()const{returndata2;}boolsomeOp()override{/

c++ - 成对函数评估算法(C++、STL)

我需要将自定义func应用于STL容器成对->即://ifc=>{a,b,c,d,e,f,g};//a,b,c,..arejustaliasesforsomeobjectmy_algorithm(c.begin(),c.end(),[](autoa,autob){a+b});//c++14应该解析成这样:temp1=a+b;temp2=c+d;temp3=e+f;temp4=temp1+temp2;temp5=temp3+g;result=temp4+temp5;(我确定这种算法有一个专有名称,但我不知道这可能是什么)我已经尝试过std::accumulate,我不确定它的实现是否由标

c++ - 从 std::true_type 继承 vs static constexpr const bool 成员

我知道这不是一个非常尖锐的问题。使用一个比另一个有优势(编译时间、依赖性、调试符号大小、可用性、可读性等)吗?templatestructIsSharedPtr:std::false_type{};对比templatestructIsSharedPtr{staticconstexprboolvalue=false;};相关问题...templatestructS;templatestructS{};templatestructS{};对比templatestructS;templatestructS{};templatestructS{}; 最佳答案

c++ - G++ 4.4.7 中的 "names the constructor, not the type"

我有以下简单的C++代码:#includeclassA{public:A(inty):x(y){}A&operator=(constA&rhs);intx;};A::A&A::operator=(constA&rhs){this->x=rhs.x;return*this;}intmain(int,char**){Aa1(5);Aa2(4);printf("a2.x==%d\n",a2.x);a2=a1;printf("a2.x==%d\n",a2.x);return0;}第11行,A的operator=()函数的定义所在,格式不正确......或者,至少,我相信是这样。正如预期的那样,

c++ - 我们可以使用异构查找比较器对 STL 关联容器执行 "partial-match"搜索吗?

所以我在STL的关联容器(自C++14起)中寻找对异构查找的支持,并对我们可以做什么和不应该做什么感到有点困惑。以下片段#include#include#includestructpartial_compare:std::less{//"full"key_typecomparisondonebystd::lessusingless::operator();//"sequence-partitioning"comparison:onlycheckpair'sfirstmemberbooloperator()(std::pairconst&lhs,intrhs)const{returnlh