草庐IT

sort_list

全部标签

c++ - std::forward_list::remove_if 谓词的要求

考虑这段代码:structT{boolstatus;UsefulDatadata;};std::forward_listlst;lst.remove_if([](T&x)->bool{returnx.status=!x.status;});即一次性切换状态和删除非事件元素。根据cppreference上面的代码似乎是未定义的行为(强调我的):templatevoidremove_if(UnaryPredicatep);p-unarypredicatewhichreturnstrueiftheelementshouldberemoved.Thesignatureofthepredicat

c++ - 为什么 std::sort 构造对象?

这个问题在这里已经有了答案:std::sortdoesnotalwayscallstd::swap(3个答案)关闭5年前。我创建了以下类来理解std::sort的行为:classX{public:X(inti):i_(i){}X(X&&rhs)noexcept:i_(std::move(rhs.i_)){mc_++;}X&operator=(X&&rhs)noexcept{i_=std::move(rhs.i_);ao_++;return*this;}voidswap(X&rhs)noexcept{std::swap(i_,rhs.i_);sw_++;}friendbooloperat

c++ - LibCds:Michael Hashmap 和 Split Order List

我正在使用libcds他们实现了MichaelHashMap和Splitorderlist。根据我从文档中收集到的信息,我是如何实现它们的:包括:#include#includeusingnamespacecds;代码:classTestDs{public:virtualboolcontainsKey(intkey)=0;virtualintget(intkey)=0;virtualintput(intkey,intvalue)=0;virtualintremove(intkey)=0;virtualintsize()=0;virtualconstchar*name()=0;virtu

c++ - 如何使用 std::forward_list 在恒定时间内进行范围拼接?

我想拼接范围[first,last],包括两个端点。我有元素beforefirst和last的迭代器。我可以使用splice_after()来完成,但只能在线性时间内完成。我相信这个拼接可以在恒定时间内完成。我如何使用std::forward_list完成它?如果问题不清楚,这里是显示我的问题的示例代码:LiveWorkSpace上的代码#include#include#include#includeusingnamespacestd;intmain(){forward_listtrg{'a','b','c'};forward_listsrc{'1','2','3','4'};auto

c++ - 如何从 std::list 实现 O(1) 删除

问题是使用std::list实现O(1)列表项删除的推荐方法是什么?通常,当我选择一个双向链表时,我希望能够在O(1)时间内从一个列表中删除一个元素,然后在O(1)时间内将它移动到另一个列表中。如果该元素有自己的prev和next指针,那么就没有真正的技巧来完成这项工作。如果列表是双向链接的循环列表,则删除不一定需要知道包含该项目的列表。根据Iteratorinvalidationrules,std::list迭代器非常耐用。因此,在我看来,在我自己的项目上使用std::list时,我想要得到的行为是在我的类和包含列表中存储一个迭代器。classItem{typedefstd::sha

c++ - 使用 std::sort 对字符串进行排序,使大写字母位于小写字母之后

我想对一个vector进行排序,使大写字母跟在小写字母之后。如果我有类似的东西ThisisatestthisisatestCatscatsthisthing我希望输出是catsCatsthisisatestThisisatestthisthing标准库排序会输出CatsThisisatestcatsthisisatestthisthing我想将谓词传递给std::sort,以便它比较我作为参数传递的字符串的小写版本。boolcompare(std::stringx,std::stringy){returnlowercase(x)我尝试降低函数中的每个字符,然后进行比较,但没有成功。我想

c++ - 从括号内的 initializer_list 构造时调用了错误的重载

我一直认为当我使用初始化列表C++语法时:something({...});编译器总是很清楚我想调用采用std::initializer_list的重载,但对于MSVC2015似乎不太清楚。我测试了这个简单的代码:#include#includenamespacetesting{templatestructTest{Test(){printf("Test::Test()\n");}explicitTest(size_tcount){printf("Test::Test(int)\n");}Test(std::initializer_listinit){printf("Test::Tes

c++ - 将 std::sort 限制为随机访问迭代器

我只是想知道,既然你只能将随机访问迭代器传递给std::sort,为什么不首先通过只为随机访问迭代器定义它来强制执行该限制?#include#includetemplatetypenamestd::enable_if::iterator_category,std::random_access_iterator_tag>::value,void>::typesort(ForwardIteratorbegin,ForwardIteratorend){//...}我发现单行错误消息比在实现过程中因类型错误导致的一页又一页的错误消息更容易阅读。您可以对其他算法执行相同的操作。标准的C++核心语

c++ - 运算符== 和 list::remove()

测试.h#ifndefTEST_H#defineTEST_H#includetemplatebooloperator==(conststd::weak_ptr&wp1,conststd::weak_ptr&wp2){std::shared_ptrsp1;if(!wp1.expired())sp1=wp1.lock();std::shared_ptrsp2;if(!wp2.expired())sp2=wp2.lock();returnsp1==sp2;}#endif测试.cpp#include"Test.h"#includeintmain(){typedefstd::list>intLi

c++ - push_back() "new"一个对象在添加到 c++ 中的 std::list 之前

我是C++标准库的新手。我想使用std::list。我知道如果我自己创建一个列表而不是使用STL,我应该为一个新对象分配内存,然后将它添加到列表中。A类的C风格列表:A*ptrA=newA();ptrA->setElement(value);ptrA->next=null;currentPositionMyCstyleList->next=ptrA;ptrA->prev=currentPositionMyCstyleList;如果我使用STL,是否有必要“新建”一个对象?push_back()在添加到c++中的std::list之前是否“新建”了一个对象?下面的代码是否正确?AaObj