草庐IT

c++ - 如何在STL中使用unordered_set?

我需要一个C++(STL)中的hash_map类。主要操作是将对放入集合中,然后检查它是否存在。我找不到示例代码来判断我声明的内容是否正确。#include#includeusingnamespacestd;usingnamespace__gnu_cxx;typedefpairpis;structeqpis{booloperator()(pisp1,pisp2)const{if(p1==p2)returntrue;returnfalse;}};intmain(){hash_map,eqpis>map;}这个编译。但是,如果我添加以下行:map[pis(10,"你好")]=10;然后它给

c++ - std::map 中的元素是否保证有序?

这只是实现的副作用(红黑树)还是顺序由c++标准保证? 最佳答案 有序迭代不是实现细节;它由C++标准保证。它是所有关联容器的基本属性(C++03§23.1.2/9):Thefundamentalpropertyofiteratorsofassociativecontainersisthattheyiteratethroughthecontainersinthenon-descendingorderofkeyswherenon-descendingisdefinedbythecomparisonthatwasusedtoconstr

c++ - 为什么在遍历此字符串时会出现段错误?

我正在尝试用C++实现基本的移位密码。在找出导致段错误的原因之前,我无法继续前进。我使用gdb单步执行了代码,问题似乎出在迭代器上。1#include2#include34std::stringencrypt(std::stringplain,intkey);56intmain()7{8std::stringplaintext;9std::getline(std::cin,plaintext,'\n');10encrypt(plaintext,3);11}1213std::stringencrypt(std::stringplain,intkey)14{15std::string::i

c++ - Std::deque 直到程序退出才释放内存

在linux上,std::deque直到程序退出才释放内存。完整代码如下。任何帮助将不胜感激!#include#include#include#include#include#include#include#include#includetypedefboost::shared_ptr>VecPtr;typedefstd::dequeQueueType;charbuf[1024];charline[1024];intmain(){{intv=0;QueueTypedeq;for(inti=0;i);deq.push_back(p);}std::cout0){deq.pop_front(

c++ - 为 STL 容器传递模板化迭代器

对于我的C++类的练习(尚未涵盖Boost),我在编写模板化方法来接受两个迭代器以对STL容器中的数值求和时遇到问题。考虑以下示例:#include#include#includetemplatedoubleSum(constT&c){return42.0;//implementationstubbed}//needhelpwritingthismethodsignaturetoaccepttwoiteratorstemplatedoubleSum(consttypenameT::const_iterator&begin,consttypenameT::const_iterator&e

c++ - 将 `std::vector` 替换为 `std::array`

我有一个代码如下:intn;intget_the_number();voidsome_computations();intmain(){n=get_the_number();some_computations()return(0);}get_the_number函数获取一些输入并返回整数n,它在调用后不会被修改。在some_computation函数中有如下代码std::vectormy_array;for(inti=0;i问题:由于my_array的大小是先验已知的,是否可以用替换std::vector>std::array?此外,在肯定的情况下,我是否应该期望在效率方面有所提高?我

c++ - std::shared_ptr 的唯一拷贝

我有一个对象(我们称它为X),其他对象可以通过std::shared_ptr访问它。然而,在某些时候,这些对象需要创建一个唯一的、非共享的X拷贝,因为它想要修改它。这在某种程度上类似于写时复制,但由于一些其他细节而不完全相同。基本上我想要这样的语义:structFoo{std::shared_ptrbar;voidgo(){//bar.use_count()>=1bar.make_this_object_unique();//bar.use_count()==1}} 最佳答案 如果你只是想复制对象,并得到一个指向新对象的共享指针,那

c++ - 在 vector 之间使用 std::swap 还是 vector::swap?

给定两个std::vectorv1,v2。我想知道使用std::swap(v1,v2)比v1.swap(v2)有什么好处。我已经实现了一个关于性能观点的简单测试代码(我不确定它是否相关):#include#include#include#include#include#defineN100000templatestructTimer{templatestatictypenameTimeT::repexec(Ffunc,Args&&...args){autostart=std::chrono::steady_clock::now();func(std::forward(args)...)

c++ - 如何在编译时检测类型是否为 shared_ptr

我想获得一种模板化的方法来查找类型是否为shared_ptr,并基于此我想对函数进行新的特化。示例主要功能是,templateinlinevoidCEREAL_LOAD_FUNCTION_NAME(RelaxedJSONInputArchive&ar,NameValuePair&t){std::cout如果t.value是shared_ptr那么我想要一个不同的函数特化。我已经在下面尝试过,templateinlinetypenamestd::enable_if::value,void>::typeCEREAL_LOAD_FUNCTION_NAME(RelaxedJSONInputAr

c++ - 复制一个 std::function 有多昂贵?

虽然std::function是可移动的,但在某些情况下这是不可能的或不方便的。复制它会受到严重处罚吗?它是否可能取决于捕获变量的大小(如果它是使用lambda表达式创建的)?是否依赖于实现? 最佳答案 std::function通常实现为值语义、小缓冲区优化、虚拟调度、类型删除类。这意味着如果您的状态很小,则复制将不涉及堆分配(除了在状态的复制构造函数内)和一些间接(找到如何复制此特定状态)。如果您的状态很大(例如,在当前MSVC上大于两个std::string),则需要额外的堆分配来存储状态。这不是您想要在每帧每个像素的基础上执