草庐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++ - 为什么 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++ - const decltype(*std::begin(container))& val 不会使 val const?

这段代码:std::vectorints(5,1);std::for_each(ints.begin(),ints.end(),[](constdecltype(*std::begin(ints))&val){val*=2;});在VisualStudio2010中编译和运行得很好,并且修改容器中的每个值,就像没有const关键字一样。这是编译器中的错误吗,因为预期的行为是val是不可修改的?(换句话说,我希望它不会编译,但它会编译)更新:std::for_each(ints.begin(),ints.end(),[](conststd::remove_reference::type&

c++ - 如何使用 std::auto_ptr 声明动态数组?

我正在尝试声明一个动态int数组,如下所示:intn;int*pInt=newint[n];我可以用std::auto_ptr做到这一点吗?我试过类似的方法:std::auto_ptrpInt(newint[n]);但是它不编译。我想知道我是否可以使用auto_ptr构造声明一个动态数组,以及如何声明。谢谢! 最佳答案 不,你不能,也不会:C++98在数组方面非常有限,auto_ptr是一个非常笨拙的野兽,它通常不会做你需要的事情。您可以:使用std::vector/std::deque,或std::array,或者使用C++11和

c++ - 将字符串 vector 连接到 std::ostream(如 boost::join)

我有一个字符串vector,我想将它输出到流(实际上是文件流)。我想在vector元素之间有一个分隔符。有一种方法可以使用标准ostream_iteratorstd::vectorstrs;std::ostream_iteratorout_file_iterator(out_file,delim);std::copy(strs.begin(),strs.end(),out_file_iterator);我不喜欢这种方式,因为each元素后有一个delim文本,但我不需要有一个delim在最后一个元素之后。我想使用类似boost::join的东西。但是boost::join返回字符串,而