草庐IT

sort_buffer

全部标签

c++ - qsort() vs std::sort,比较函数哲学差异

我想知道为什么在qsort(){Cversion}和std::sort()中有两种完全不同的方法来指定比较函数。qsort需要这样的比较函数:不知道为什么需要三种返回值-1、0、+1。intcomp(int*x,int*y){return*x-*y;}而std::sort()的比较函数对我来说看起来更一致,因为它是根据函数编写的,遵循不变量。即如果x小于y函数返回true,则x相对于y处于正确位置boolcomp(intx,inty){returnx当返回一个bool(或具有两个值0和1的int)更简单和干净时,为什么我们需要三个值-1,0,+1? 最佳答案

c++ - 在一个 Protocol Buffer 二进制文件中存储多条消息

我有重复的消息,我想将它们存储在一个文件中。目前我必须将这条重复的消息包装在另一条消息中。有解决办法吗?packagefoo;messageBox{requiredint32tl_x=1;requiredint32tl_y=2;requiredint32w=3;requiredint32h=4;}messageBoxes{repeatedBoxboxes=1;} 最佳答案 这是"Techniques"ProtocolBuffers文档的一部分提到了重复消息:Ifyouwanttowritemultiplemessagestoasin

c++ - 在对对象的 vector 进行排序时使用 stable_sort

我有classPassanger有变量stringname;stringstation;stringticket;然后我有另一个类,在这个类中我有vectormyQueue;现在我想使用stable_sort排序myQueue.有没有可能,怎么说给stable_sort,应该是什么key,根据它排序myQueue?std::stable_sort(myQueue.begin(),myQueue.end(),maybeSomethingElse());? 最佳答案 std::stable_sort()过载接受自定义比较器作为其第三个参

c++ - std:sort 与插入 std::set

我正在从cin读取一些线段。每条线段由起点和终点表示。2D。X和Y。输入未排序。它是随机排列的。(更新:但我需要它们先按X再按Y排序)我可以读取所有段,将它们存储在一个vector中,然后调用std::sort。另一方面,我可以创建一个空的std::set并在每个段到达时插入它。该集合将自动维护排序顺序。这两种方法哪种更有效?更新:输入的总大小(段数)是预先知道的。 最佳答案 您应该测量这两种方法的性能以确保确定,但可以安全地假设std::vector上的std::sort是way比插入std::set更快,因为局部效应和隐藏在树插

c++ - 为什么 std::sort 会在此代码上抛出段错误?

有人可以解释为什么下面的排序会导致段错误吗?这是g++(指针的排序vector)的已知错误吗?我正在使用g++4.5.2进行编译。#include#include#includeusingnamespacestd;typedefvectorA;boolface_cmp(constA*x,constA*y){returnx!=y;}intmain(intargc,char*argv[]){vectorvec;for(inti=0;i(i%100,i*i));}vector::iteratorit;sort(vec.begin(),vec.end(),face_cmp);returnEXI

c++ - Protocol Buffer 错误版本

我正在尝试安装Caffe库,但由于我的系统中安装了错误的protobuf版本,我遇到了编译问题。Infileincludedfrom.build_release/src/caffe/proto/caffe.pb.cc:5:0:.build_release/src/caffe/proto/caffe.pb.h:17:2:error:#errorThisfilewasgeneratedbyanolderversionofprotocwhichis#errorThisfilewasgeneratedbyanolderversionofprotocwhichis^.build_release/

c++ - 如何将多个 Protocol Buffer 的消息写入一个可追加的压缩文件?

我正在使用ProtocolBuffer的CodedOutputStream和FileOutputStream将多个消息按顺序序列化到一个文件中,如下所示://Fileisopenedusingappendmodeandwrappedinto//aFileOutputStreamandaCodedOutputStreamboolOpen(conststd::string&filename,intbuffer_size=kDefaultBufferSize){file_=open(filename.c_str(),O_WRONLY|O_APPEND|O_CREAT,//openmodeS_

c++ - std::sort() 和 std::sort_heap() 的内存复杂度是多少?

如标题-std::sort()和std::sort_heap()的内存复杂度是多少?(后者需要std::make_heap()所以我也想知道它的内存复杂度。)我尝试在这些网站上搜索:http://www.cplusplus.com/reference/http://en.cppreference.com/w/但要么我错过了,要么他们只提到了时间复杂度。上述函数的内存复杂性是否在任何地方指定(在C++标准或其他文档中)?或者这可能取决于实现? 最佳答案 对于std::sort()我在Cboard上找到了一个答案,它几乎是这样说的:Qu

c++ - 我不明白在下面的代码中将 char buffer[] 与 X 类型的对象对齐的原因

Stroustrup在他的新书第151页中展示了以下使用类型说明符alignas的示例:Sometimes,wehavetousealignmentinadeclaration,whereanexpression,suchasalignof(x+y)isnotallowed.Instead,wecanusethetypespecifieralignas:alignas(T)means"alignjustlikeaT."Forexample,wecansetasideuninitializedstorageforsometypeXlikethis:voiduser(constvector

c++ - 访问 Protocol Buffer 扩展字段

我正在使用C++中的ProtocolBuffer。我的消息只有一个扩展范围。我想在不知道他们的名字的情况下访问所有的扩展字段,只使用他们的号码。我该怎么做??messageBase{optionalint32id=1;extensions1000to1999;}extendBase{optionalint32id2=1000;}到目前为止,我已经获得了ExtensionRange。constgoogle::protobuf::Descriptor::ExtensionRange*rng=desc->extension_range(0);std::cerrstartend但我不知道如何获