草庐IT

partial_sort_copy

全部标签

c++ - 使用 std::sort 对 std::list 进行排序

这个问题在这里已经有了答案:关闭10年前.PossibleDuplicate:Sortlistusingstlsortfunctionwhyonlystd::list::sort()?我的问题是我们可以使用std::sort函数对两个std::list进行排序吗?我有2个字符串列表std::listlist1,list2;.....//enteringvaluestoliststd::sort(list1.begin(),list1.end());std::sort(list2.begin(),list2.end());当我对这些列表进行排序时,我遇到了错误。我尝试使用std::vec

C++ 静态工厂方法与构造函数 : how to avoid copying?

Thisquestion要求以简洁的方式在C++中实现静态工厂方法,thisanswer描述了一种明确的方法。返回值优化将使我们免于制作不必要的Object拷贝,从而使这种创建Object的方式与直接调用构造函数一样高效。在私有(private)构造函数中将i复制到id的开销可以忽略不计,因为它是一个小的int。但是,当Object包含作为类Foo实例的实例变量(需要复杂的初始化逻辑)时,问题和答案并未涵盖更复杂的情况)而不是一个小的原始类型。假设我想使用传递给Object的参数构造Foo。使用构造函数的解决方案如下所示:classObject{Foofoo;public:Object

c++ - 为什么 Sortable 概念需要完全有序的值类型,而 std::sort 只需要 "less than"可比较?

在latestpaperonconceptsN3701,有以下示例与sort算法:templaterequiresSortable()voidsort(Cont&cont)在哪里Sortable概念定义为templateconceptboolSortable(){returnPermutable_container()&&Totally_ordered>();}在哪里Totally_ordered,毫不奇怪,被定义为templateconstexprboolTotally_ordered(){returnWeakly_ordered()&&Equality_comparable();}

c++ - std::partial_sum 和 std::inclusive_scan 有什么区别?

在阅读std::inclusive_scan时,似乎没有任何例子。我觉得它与std::partial_sum非常相似.partial_sum:templateOutputItpartial_sum(InputItfirst,InputItlast,OutputItd_first);inclusive_scan:templateOutputItinclusive_scan(InputItfirst,InputItlast,OutputItd_first);有人可以详细说明他们的区别吗?我什么时候会选择其中之一? 最佳答案 std::i

c++ - C++ 中 std::is_trivially_copy_constructible 中的琐碎操作是什么

这是std::is_copy_constructible(1)和std::is_trivially_copy_constructible文档的摘录(2)关于cppreference.com:1)CheckswhetheratypeisCopyConstructible,i.e.hasanaccessibleexplicitorimplicitcopyconstructor.Iftherequirementismet,amemberconstantvalueequaltrueisprovided,otherwisevalueisfalse.2)Sameas(1),butthecopyco

c++ - GNU STL 字符串 : is copy-on-write involved here?

(免责声明:我不知道C++标准对此会说什么……我知道,我很糟糕)在处理非常大的字符串时,我注意到std::string正在使用写时复制。我设法编写了最小的循环来重现观察到的行为,例如,下面的循环运行得非常快:#includeusingstd::string;intmain(void){stringbasestr(1024*1024*10,'A');for(inti=0;i在循环体a_copy[1]='B';中添加写入时,显然发生了实际复制,并且程序在0.3秒内运行,而不是几毫秒。100次写入使其速度减慢了大约100倍。但后来变得很奇怪。我的一些字符串没有写入,只是读取,这没有反射(re

c++ - 复制构造函数 : deep copying an abstract class

假设我有以下情况(简化情况):classColor;classIColor{public:virtualColorgetValue(constfloatu,constfloatv)const=0;};classColor:publicIColor{public:floatr,g,b;Color(floatar,floatag,floatab):r(ar),g(ag),b(ab){}ColorgetValue(constfloatu,constfloatv)const{returnColor(r,g,b)}}classMaterial{private:IColor*_color;publ

c++ - 在 QObject 派生类中重复 Q_DISABLE_COPY

在Qt中有一个宏允许为类声明私有(private)复制构造和赋值运算符:http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#Q_DISABLE_COPY据说这个宏应该用于所有QObject(尤其是QWidget)的派生类。我了解它的工作原理以及它为何有用。我不明白的是:有什么理由在我的QObject派生类中重复Q_DISABLE_COPY而QObject已经包含Q_DISABLE_COPY并且通过这有效地防止我的派生类被复制? 最佳答案 尝试复制派生类时可能打印的错误消息可能是指

c++ - 错误 : 'sort' is not a member of 'std'

我只是想问一下这个错误是什么意思以及如何解决它谢谢!error:'sort'isnotamemberof'std'vectorresult;for(auto&i:numbers)result.push_back(std::stoi(i));std::sort(result.begin(),result.end()); 最佳答案 包括算法头。这就是出现错误的原因。#include 关于c++-错误:'sort'isnotamemberof'std',我们在StackOverflow上找到一

C++20 constexpr std::copy 运行时优化

cppreference.com说:Inpractice,implementationsofstd::copyavoidmultipleassignmentsandusebulkcopyfunctionssuchasstd::memmoveifthevaluetypeisTriviallyCopyable但是,该页面还指出,从C++20开始,不采用执行策略的重载将是constexpr。标准会禁止这些运行时优化(因为std::memmove不是constexpr)还是有办法为运行时优化constexpr函数? 最佳答案 我们也可以吃蛋