草庐IT

sort_list

全部标签

c++ - `std::sort` 内部使用了什么魔法让它更快?

这个问题在这里已经有了答案:WhatalgorithmsdopopularC++compilersuseforstd::sortandstd::stable_sort?(2个答案)关闭9年前。我有一个简单的快速排序实现:templatevoidquicksort(IteratorTypebegin,IteratorTypeend){if(begin!=end){constautopivot=*(begin+distance(begin,end)/2);constIteratorTypesep=std::partition(begin,end,[pivot](typenameIterat

c++ - 为什么 std::list 想不带参数调用我的分配器?

抱歉,我不能贴出具体的代码。我希望这个小样本就足够了:假设我有一个这样的分配器:templateclassMyAllocator{//...typedefsMyAllocObject_allocObject;public:MyAllocator(){//_allocObject=new..}MyAllocator(constMyAllocator&alloc){_allocObject=alloc.getAllocObject();}templateMyAllocator(constMyAllocator&alloc){_allocObject=alloc.getAllocObject

c++ - 为什么 std::make_unique 等不存在 std::initializer_list 重载?

参见ShouldIuse()or{}whenforwardingarguments?.foo是std::vector克隆。在N4140中,unique.ptr.createstd::make_unique指定为:templateunique_ptrmake_unique(Args&&...args);Remarks:ThisfunctionshallnotparticipateinoverloadresolutionunlessTisnotanarray.Returns:unique_ptr(newT(std::forward(args)...)).这意味着需要实现才能使用()而不是{

c++ - 通过 std::sort 对 C 二维数组进行排序

我有一个二维数组a[][40]。我正在尝试通过调用std::sort对其进行排序,并且我已经编写了Compare函数。但是,C++希望我有一个要排序的std::vector,而不是一个简单的数组,我希望排序后的数组是a本身,我不想创建另一个数组并将排序结果保存在那里。似乎有很多方法可以实现这一目标。我可以想到五种方法,但似乎没有一种有效且有效。1)Directlyusestd::sort(std::begin(a),std::begin(a)+something,cmp);它不起作用,因为std::begin不知道如何指向二维数组的开头。此外,即使编译它也会排序不正确,因为二维数组不是

c++ - 使用可变参数模板制作类似元组的编译时 "linked-list"

我在考虑std::tuple的可能实现方式(以及任何类似的模板类,在编译时定义了可变数量的“成员”),我认为也许可以创建一个类似于链表的“递归类型”。我尝试编译以下测试用例:templateclassTupleLite{public:FirstTypetype_;TupleLiteother_types_;};intmain(){TupleLitemytuple;}类本身编译没有错误,但实例化抛出错误wrongnumberoftemplatearguments(0,shouldbe1ormore).我相信这是因为TupleLite尝试实例化TupleLite,它试图实例化一个Tuple

c++ - 如何对 std::vector 进行排序但不使用 std::sort 更改特定元素?

我有一个包含正整数和-1的vector。我的问题是我想对vector进行排序,但不要仅使用std::sort来触摸-1元素(我知道其他解决方法)。例如:Input:[-1,150,190,170,-1,-1,160,180]Output:[-1,150,160,170,-1,-1,180,190]这是我解决它的想法,但没有奏效:sort(myVector.begin(),myVector.end(),[&](constint&a,constint&b)->bool{if(a==-1||b==-1)return&aMyoutputis:[-1,150,170,190,-1,-1,160,

C++ 构造函数采用大小为 1 的 std::initializer_list

下面的示例程序代码背后的想法是说明这样一个事实,即当只给定一个元素时,拥有一个不等同于默认复制构造函数的初始化列表构造函数可能会导致Clang以某种方式出现意想不到的结果。它还表明Clang和GCC没有在复制构造函数和初始化列表构造函数之间实现相同的优先级,这实际上使得可移植代码不可能使用这种初始化列表构造函数。//Includedirective.#include//Thestructusedinthiscase.structFoo{//Membervalue.intval;//Defaultconstructor.Foo():val(0){}//Initializerlistcon

c++ - 用于 std::list 和 std::map 的 Visual C++11 堆栈分配器

我想提高list和map的特定用法的性能,其中项目的数量有大约100000的硬限制。在这种情况下,STL默认分配器显然不是最佳选择,因为清理所有数千个小对象需要很长时间(>10秒!)。更不用说所有其他潜在问题。很明显,为了改进这一点,我可以预先分配正确数量的内存以包含所有列表/map节点。到目前为止,我已经能够实现默认分配器的工作版本(通过从std::allocator_traits派生),它为每个节点使用alloc/free。但我正在努力找出如何修改它以允许“有状态”使用,例如,我非常简单的堆栈:usingnamespacestd;classMemPoolStack{public:s

c++ - 函数模板推导和initlializer_list

我有以下函数模板:templatevoidf(std::initializer_list>il){//...}我调用函数如下:f({std::pair(1,2),std::pair(3,4)});//(a)而且效果很好。但是,如果我尝试按如下方式调用它:f({{1,2},{3,4}});//(b)它无法推断出正确的类型,我得到了一个编译错误:'nomatchingfunctionforcallto'f()notecandidateis:note:templatevoidf(std::initializer_list>)'如果我这样调用它:f({std::pair(1,2),{3,4}}

c++ - Uniform Initialization with curly brace 被误认为是 Initializer List

我有一个类:#includeclassObject{std::shared_ptrobject_ptr;public:Object(){}templateObject(T&&object):object_ptr{newT{std::move(object)}}{}virtual~Object(){};};我的主要cpp文件是:#include#include"Object.hpp"classFoo{};intmain(){Objecto{Foo{}};}它给我错误:test/test.cpp:13:20:requiredfromhereinclude/Object.hpp:24:49: