草庐IT

Hash_class

全部标签

c++ - 实现Matlab的冒号: operator in C++ expression templates class

我正在实现一个C++表达式模板库。我已经设置了一个适当的SubMatrixExpr类来收集矩阵中的元素,启用类似的语法B=SubMatrix(A,1,3,2,10);相当于Matlab的B=A(1:3,2:10);当然,Matlab的语法比我的要舒服得多。所以我的问题是有没有可能在C++中设置Matlab的冒号:运算符?非常感谢您。 最佳答案 简短的回答:没有。冒号不是有效的C++运算符,因此不能重载。即使可以,它仍然不可能轻松实现您的需求,因为它肯定会优先于逗号运算符,这将使您的表达式位于A((1:3),(2:10))行中。,如果

C++ 仿函数和模板 : error: declaration of 'class List<T>'

我在模板类中有一个嵌套模板,用于名为List::find()的方法。此方法获取一个仿函数作为输入,即:“函数条件”。templateclassList{....templateIteratorfind(Functioncondition)const;....};templatetypenameList::IteratorList::find(Functioncondition)const{List::Iteratorit=this->begin();for(;it!=this->end();++it){if(condition(*it)){break;}}returnit;}错误是:.

c++ - 专门为私有(private)类(class)的功能?

有没有办法为私有(private)类专门化一个函数(比如,std::swap)?例如,当我测试这个时:#includeclassOuter{structInner{inta;voidswap(Inner&other){usingstd::swap;swap(this->a,other.a);}};public:staticvoidtest();};namespacestd{templatevoidswap(Outer::Inner&a,Outer::Inner&b){a.swap(b);}}voidOuter::test(){usingstd::swap;Innera,b;swap(a

c++ - 与 hash_combine 发生太多冲突

我将boost::unordered_map与自定义结构一起使用,该结构或多或少是一个整数vector,并具有如下所示的自定义哈希函数:std::size_tseed=0;for(inti=0;i当myvec的大小为3并且我用1M元素1:100x1:100x1:100填充散列(所以myvec的每个元素都是一个从1到100的整数)我得到大约330,000次碰撞。发生这么多次碰撞是否正常?我该怎么做才能避免这种情况? 最佳答案 你是对的。Boost的hash_combine函数对这个数据集的表现很差。您可以使用thiscode进行测试对

c++ - 嵌套类声明 : template vs non-template outer class

我有一个C++模板类,里面有一个嵌套类,比如:templateclassOuter_t{public:classInner;Inneri;};templateclassOuter_t::Inner{public:floatx;};intmain(){Outer_to_t;//3oranyarbitraryinto_t.i.x=1.0;return0;}编译没有任何问题。然而,一旦我声明了一个类似的非模板类,就像这样:classOuter_1{public:classInner;Inneri;};classOuter_1::Inner{public:floatx;};intmain(){

c++ - 为什么 boost::hash_combine 中的魔数(Magic Number)是十六进制指定的

本例中的魔数(MagicNumber)是0x9e3779b9,以10为基数是2654435769。代码有什么原因吗seed^=hash_value(v)+0x9e3779b9+(seed>2);使用十六进制表示而不是base-10表示?如果将代码中的0x9e3779b9替换为2654435769,功能是否会保持不变? 最佳答案 字面量就是字面量,同一字面量的不同表示形式……字面上相同。但是,表达式(文字或非文字)也有一个类型。等效的字面量应该是2654435769u(注意类型后缀使其成为unsigned)。看看这个简单的测试Live

c++ - Gtest : Expected Class-Name Before '{'

我正在尝试将Gtest下的测试用例转换为使用测试夹具,以便在添加更多测试时可以使用通用设置。但是,这会导致错误:test_integrate.cc:4:47:error:expectedclass-namebefore'{'tokenclassIntegratorTest:public::testing::test{这种失败是我无法理解的,因为根据我的经验,它通常是由循环导入引起的,并且导入与工作代码相比没有变化。完整代码如下:#include"gtest/gtest.h"#include"utils/integrate.hpp"classIntegratorTest:public::

c++ - 为什么一个 const Class& 可以被初始化为自身?

今天我遇到了一个非常愚蠢但难以检测的错误。相关代码如下:classVector;classPointIterator{constVector&x;constVector&yv;PointIterator(constVector&xv,constVector&yvo):x(xv),yv(yv){;};//^^hereiswrong};为什么这样的代码是合法的C++?在任何情况下都可以使用yv变量吗?我知道关于intx=x+1;的类似问题,(请参阅thisquestion)但后者未正确初始化,您仍然可以使用x变量,而在上面的代码中,我认为您不能使用yv。奖励点:是否有任何编译选项可以让我检

c++ - C++ 中的 class a() 和 class a = class() 有什么区别?

来自Java和C#世界,一直喜欢用someclassa=someclass();代替someclassa();在C++中初始化一个类变量。但是,我的编译器有时会提示ErrorC2280:Attemptingtoreferenceadeletedfunction它们之间有什么区别吗?哪个更好? 最佳答案 Isthereanydifferencebetweenthem?一个大的:someclassa();isdeclaringafunction!someclassa=someclass();,在C++17'scopyellision之前

c++ - g++ 链接器错误:获取 std::hash 的 undefined reference 错误

我在我的代码中使用了TR1实现的unordered_map,链接器给出了我什至无法破译的奇怪错误:BPCFG.o:Infunction`std::__detail::_Hash_code_base,std::_Select1st>,eqDottedRule,std::hash,std::__detail::_Mod_range_hashing,std::__detail::_Default_ranged_hash,false>::_M_hash_code(DottedRuleconst&)const':BPCFG.cpp:(.text._ZNKSt8__detail15_Hash_co