草庐IT

C++ STL:将派生虚拟类用作 std::sort() 的 "Strict Weak Ordering"

我使用std::sort()撞墙了。我有一个纯虚类(名为Compare),方法的调用者派生自该类(名为MyComp)。我将纯虚拟类用于我的API原型(prototype):voidObject::DoSort(Compare&comp){std::sort(this->mKeys.begin(),this->mKeys.end(),comp);}来电者:classMyComp:publicCompare{booloperator()(constRow*r1,constRow*r2){...}}cmp;...obj->DoSort(cmp);Linux上的g++编译器提示:“无法分配类型

C++ std::queue 不想 push()

这是一个简单的类和简单的测试函数:#include#includenamespace{usingnamespacestd;}classNameStream{queuestream;public:stringoperator*(){returnstream.front();}NameStream&operator++(int){stream.pop();return*this;}NameStream&operator++(){stream.pop();return*this;}NameStream&operator它落在NameStream&operator在队列的推送过程中,这是我的代

c++ - 使用私有(private)复制/移动构造函数进行聚合初始化

我在为anotherquestion测试一些东西时遇到了这个问题关于初始化聚合。我正在使用GCC4.6。当我用列表初始化聚合时,所有成员都在适当的位置构建,无需复制或移动。即:intmain(){std::array,2>a{std::array{Goo{1,2},Goo{3,4}},std::array{Goo{-1,-2},Goo{-3,-4}}};}让我们通过一些嘈杂的构造函数来确认:structGoo{Goo(int,int){}Goo(Goo&&){std::cout运行时,不会打印任何消息。但是,如果我将移动构造函数设为私有(private),编译器会提示'Goo::Goo

c++ - 递归泛型函数用作谓词,编译失败

我正在编写一个函数来比较两个列表的内容。元素的顺序无关紧要,所以我在比较之前先对它们进行排序。列表可以是普通类型list,也是列表的列表list>.这是一个完整的精简示例:#includetemplateboollessThanInAnyOrder(Tlhs,Trhs){returnlhsboollessThanInAnyOrder(std::listlhs,std::listrhs){lhs.sort(lessThanInAnyOrder);rhs.sort(lessThanInAnyOrder);//Docomparisonshere,butfornowjust:returnfal

c++ - 为什么 std::pair 在赋值中调用显式构造函数

考虑以下代码:#include#includestructBase{intbaseint;};structDer1:Base{intder1int;Der1():der1int(1){}explicitDer1(constBase&a):Base(a),der1int(1){std::cerrstructMyPair{Tfirst;Usecond;};intmain(){Der1d1;Der2d2;std::pairp1;std::pairp2;p1=p2;//ThiscompilessuccessfullyMyPairmp1;MyPairmp2;mp1=mp2;//Thiswillr

c++ - 如何为关联容器应用 std::accumulate 算法?

对于像std::map这样的映射,我如何计算它的值总和?实际上,我是用仿函数和std::for_each算法实现的。但我也想使用std::accumulate算法来实现。我不知道如何将它应用到std::map。这可能吗?structAccumurator:std::unary_function,void>{Accumurator():totalValue_(0){}voidoperator()(conststd::pair&p){totalValue_+=p.second;}intresult()const{returntotalValue_;}inttotalValue_;};int

c++ - 丢失的一元 std::copy 的最佳实现

C++11引入了语义以避免不必要的对象复制,std::move引入了语义,否则会发生复制。但是,现在也有一些情况需要拷贝,但默认情况下不需要。例如,考虑一下reverse的这种简单实现。因为基于范围的for使用完美转发,所以在循环内修改容器相当于损坏。autoout_iter=container.rbegin();for(autovalue:container){*out_iter++=value;}目标是使用解决这个问题for(autovalue:copy(container)){这看起来很简单……接受任何参数,获取底层类型并返回一个临时拷贝。 最佳答案

c++ - boost::variant 是否与 std::string 一起使用?

我使用boost::variant用C++编写了一个简单的程序。程序代码如下所示。#include#include#includeintmain(intargc,char**argv){boost::variantv;v=3;std::cout但是当我尝试用命令编译它时g++main.cpp-omain-lboost_system我明白了/usr/include/boost/variant/detail/variant_io.hpp:64:error:nomatchfor‘operator>>*)this)->boost::detail::variant::printer>>::out

c++ - 在 C++ std::<vector> 容器中存储多种类型结构的模式

我有一个表示火车的数据结构,它可以由多种类型的汽车组成,例如火车引擎、运粮车、客车等:structTrainCar{//...Colorcolor;std::stringregistration_number;unsignedlongdestination_id;}structPowerCar:TrainCar{//...constRealPowerCar&engine;}structCargoCar:TrainCar{//...constRealCargoCar&cargo;boolfull;}std::vectorcars;cars.push_back(newTrainCar(..

c++ - 将 boost::bind 与 boost::function 一起使用:检索绑定(bind)变量类型

有什么方法可以检索有关哪些参数受boost::bind限制的信息,还是需要手动存储?即:在.h中classMyClass{voidfoo(inta);voidfoo2(doubleb);voidbar();voidexecute();int_myint;double_mydouble;}在.cpp中MyClass::bar(){vectormyVector;myVector.push_back(boost::bind(&MyClass::foo,this,MyClass::_myint);myVector.push_back(boost::bind(&MyClass::foo2,thi