草庐IT

hash-function

全部标签

c++ - boost 日志 : How to prevent the output will be duplicated to all added streams when it uses the add_file_log() function?

我使用add_file_log()函数来初始化一个日志接收器,它将日志记录存储到一个文本文件中。当我定义多个接收器时,我观察到:为每个接收器创建一个文件。输出被复制到所有文件。这是我的记录器:classlogger{public:logger(constlogger&)=delete;logger(logger&&)=delete;logger&operator=(constlogger&)=delete;logger&operator=(logger&&)=delete;staticlogger&get_instance(conststd::string&file,boolconso

c++ - 哪个更合适 : getters and setters or functions?

如果替代函数名称使API更明显,是否应该放弃getter和setter的“getMyValue()”和“setMyValue()”模式?例如,假设我在C++中有这个类:publicclassSomeClass{private:boolmIsVisible;public:voiddraw();voiderase();}我可以像这样添加函数来获取/设置“mIsVisible”:boolgetVisible(){returnmIsVisible;};voidsetVisible(boolvisible){if(!mIsVisible&&visible){draw();}elseif(mIsV

c++ - 在 std::function 上强制执行 "noexcept"?

此代码编译并运行,抛出int:#includevoidr(std::functionf){f();}voidfoo(){throw1;}intmain(){r(foo);}但是我希望编译器拒绝r(foo);行,因为r应该只传递一个noexcept函数。noexcept说明符似乎被忽略了。有什么办法可以实现吗?编辑:这个问题不同于Isknowledgeaboutnoexcept-nesssupposedtobeforwardedwhenpassingaroundafunctionpointer?因为我要求补救措施,特别是在std::function的情况下。

c++ - 为什么 std::function 构造函数或赋值之间存在差异?

std::function类型删除构造函数定义为:templatefunction(Ff);赋值运算符定义为:templatefunction&operator=(F&&f);(来源cppreference)为什么构造函数通过值获取f,而operator=通过转发引用获取f? 最佳答案 我只能猜测,但我猜这是因为它被添加到C++而右值引用和转发引用被添加到语言中。因此其API的某些部分获得了转发引用,而另一些则没有。有一个小优点:如果F的复制构造函数可以扔而移动不能,std::function(F)可以保证不抛出,而std::fun

c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?

以下代码无法在g++7.2.0中编译templateclassRequest{intcontent=0;public:friendvoidsetContent(inti,void*voidptr){Request*ptr=(Request*)voidptr;ptr->content=i;}intgetContent(){returncontent;}};intmain(){Requestreq;setContent(4,&req);returnreq.getContent();}有错误test.cpp:Ininstantiationof‘voidsetContent(int,void*

c++ - boost::hash_combine 与简单的异或运算

当使用boost库时,函数boost::hash_combine的工作方式如下:seed^=hash_value(v)+0x9e3779b9+(seed>2);http://www.boost.org/doc/libs/1_46_1/doc/html/hash/reference.html#boost.hash_combine与简单的异或运算相比,这种方法有什么优势?通过XOR-ing,甚至可以使用散列函数将无序容器用作键,而这个容器是顺序相关的。 最佳答案 有很多有序的容器,比如列表。如果您要使用XOR,那么您基本上会说[0,1]

c++ - Google 的 dense_hash_map 在 set_empty_key() 函数中崩溃

我正在尝试使用googledense_hash_map来存储键值数据而不是std:map。当我使用(int,int)对进行测试时,我设置了set_empty_key(mymap,-2)并且它起作用了。但是,现在当我将它与我的(hash,value)对一起使用时,我设置了set_empty_key(mymap-2)或set_empty_key(mymap,some_random_hash),在这两种情况下我的程序都会在set_empty_key();中崩溃。有人可以指导我吗?我该如何修复此崩溃?谢谢。 最佳答案 我不知道您遇到的崩溃的

c++ - 如何将 C++ unordered_set 用于自定义类?

如何在unordered_set中存储类的对象?我的程序需要经常检查此unordered_set中是否存在某个对象,如果存在,则对该对象进行一些更新。我在网上查过如何使用unordered_set,但遗憾的是大多数教程都是关于在int或string类型上使用它的。但是我怎样才能在类里面使用它呢?我如何定义一个散列函数来使以下示例中的node_id成为unordered_set的键?#include#includeusingnamespacestd;//HowcanIdefineahashfunctionthatmakes'node'use'node_id'askey?structnod

C++:莫名其妙的 "pure virtual function call"错误

我在使用MicrosoftVisualC++2015时遇到了一些困难,但能够用一个小程序重现该问题。给定以下类:classBaseClass{public:BaseClass():mValue(0),mDirty(true){}virtual~BaseClass(){}virtualintgetValue()const{if(mDirty)updateValue();returnmValue;}protected:virtualvoidupdateValue()const=0;mutableboolmDirty;mutableintmValue;};classDerivedClass:

c++ - "function"类型的优点是什么(不是 "pointer to function")

阅读C++标准,我看到有“函数”类型和“函数指针”类型:typedefintfunc(int);//functiontypedefint(*pfunc)(int);//pointertofunctiontypedeffunc*pfunc;//sameasabove我从未见过在示例之外使用的函数类型(或者我可能没有意识到它们的用法?)。一些例子:funcincrease,decrease;//declarestwofunctionsintincrease(int),decrease(int);//sameasaboveintincrease(intx){returnx+1;}//cann