草庐IT

Rolling_std

全部标签

c++ - 相同的结果 qsort 与 std::sort

请原谅消息的简洁。我有一组记录。我想按w.r.t.降序排序。其中一个关键。记录的键不是唯一的。qsort的比较函数:intcmp(constrecord*rec1,constrecord*rec2){returnrec2->key-rec1->key;}std::sort的比较函数:booloperator()(constrecord&rec1,constrecord&rec2){returnrec1.key>rec2.key;}两个版本会给出相同的结果吗?我不确定当键相等时sort/qsort的行为是否相同。 最佳答案 没有这样的

c++ - std::lock 仍然导致死锁

std::lock是用来防止死锁的吧?但是在我的测试中,它仍然导致死锁。你能检查一下我的测试代码,看看我是否使用错误吗?std::mutexm1;std::mutexm2;voidfunc1(){std::unique_locklock1(m1,std::defer_lock);printf("func1lockm1\n");std::this_thread::sleep_for(std::chrono::seconds(2));std::unique_locklock2(m2,std::defer_lock);printf("func1lockm2\n");std::lock(m1,

c++ - std::allocator 是否处理 C++17 中的过度对齐类型?

C++17引入了std::aligned_alloc和对齐感知new可以进行过度对齐分配,但是std::allocator?它是否处理过度对齐的类型? 最佳答案 在N4659(C++17DIS)中,23.10.9.1[allocator.members],bullet2T*allocate(size_tn);Returns:Apointertotheinitialelementofanarrayofstorageofsizen*sizeof(T),alignedappropriatelyforobjectsoftypeT.与C++1

c++ - 在这个例子中,std::variant 是如何变成 valueless_by_exception 的?

这是受cppreference中示例启发的示例structS{operatorint(){throw42;}};intmain(){variantv{12.f};//OKcout(S());//vmaybevalueless}catch(...){}cout对于一个编译器,我试过它的输出false,true意味着emplace导致变体变得毫无值(value)我不明白这是怎么发生的。特别是我根本不明白为什么emplace被调用,我希望程序甚至不会调用它,因为从S到int参数的转换会抛出。 最佳答案 注意相关std::variant::

c++ - c++11 严格别名规则是否允许通过 char *、char(&)[N]、甚至 std::array<char, N>& 使用 -fstrict-aliasing -Wstrict-aliasing=2 访问 uint64_t?

根据this关于C++11/14严格别名规则的stackoverflow回答:Ifaprogramattemptstoaccessthestoredvalueofanobjectthroughaglvalueofotherthanoneofthefollowingtypesthebehaviorisundefined:thedynamictypeoftheobject,acv-qualifiedversionofthedynamictypeoftheobject,atypesimilar(asdefinedin4.4)tothedynamictypeoftheobject,atypet

c++ - 如何在不使用 std 库的情况下将数组值分配给构造函数中的类成员数组?

试图了解将值复制到类成员数组中的正确方法。目前,我获取数组的每个值并将它们复制到成员数组的相应元素中:structIPAddress{IPAddress(constunsignedcharvalues[4]):values{values[0],values[1],values[2],values[3]}{}constunsignedcharvalues[4];};intmain(intargc,char**argv){unsignedcharvalues[]={10,0,0,1};IPAddressaddress(values);return0;}这行得通,但是有没有办法“自动”复制构

c++ - 当类没有 constexpr 构造函数时简化冗余 std::array 初始化

我有以下代码的更复杂版本:#include#includeusingnamespacestd;classDummy{public:Dummy(constdoublea,constdoublef){//Somecomplexcalculations}};constexprdoublevalues[]{0.1,0.2,0.3,0.4};constexprautoN=sizeof(values)/sizeof(values[0]);staticconstarraydummies{Dummy(10*values[0],M_PI*0),Dummy(10*values[1],M_PI*1),Dum

c++ - 类模板中 std::array 的大小取决于模板参数

我有一个下面的类模板templateconstexprintarraySize(){returnarraySize()+N;}templateconstexprintarraySize(){return0;}templateclassMyClass{public:std::array()>arr;};intmain(){MyClasscls;std::cout一切正常,但我想要calculateArraySize()作为成员函数。我尝试了以下方法:templateclassMyClass{public:staticconstexprintarraySize();std::array::

c++ - 实际上,C++11 中 std::atomic 的内存占用是多少?

我正在编写的一个程序需要在ram中存储大量数据(几千兆字节)以供多个线程原子访问。std::atomic似乎是一种合理的方式来做到这一点,因为它的访问可能比将所有访问包装在一个或多个std::mutex中更有效。s,因为,最坏的情况下,它将在内部使用互斥量并且是等效的。我的数据组织为一组Chunk对象,除其他外,它们有一个包含大部分数据的数组成员。现在,我正在考虑将其定义为std::array,SOME_CONSTANT_HERE>,但这只有在内存占用为std::atomic时才会有效在内置类型上,例如unsignedint不比unsignedint差本身,因为根据我的计算,以我需要存

c++ - std::priority_queue::pop 什么时候可以抛出异常

pop()std::priority_queue的方法未声明为noexcept,因此理论上可以抛出异常。但它什么时候会抛出异常,这些异常可能是什么? 最佳答案 它可以被标记为nothrow,但不是。为什么std::priority_queue::pop可以*不抛出voidpop();Removesthetopelementfromthepriorityqueue.Effectivelycallsstd::pop_heap(c.begin(),c.end(),comp);c.pop_back();c默认是一个std::vector。[