草庐IT

img_find

全部标签

c++ - 为什么 std::map find() 没有声明为 noexcept?

C++14标准定义std::map的find()成员函数如下:iteratorfind(constkey_type&x);const_iteratorfind(constkey_type&x)const;为什么这些函数没有定义为noexcept?内部可能出现什么问题,这将需要抛出异常或产生未定义的行为(除了找不到元素,在这种情况下函数返回end迭代器并且无论如何都不需要抛出异常)? 最佳答案 find()基于map的Compare()方法,这可能会引发异常(想象一下复杂键可能不正确的情况).因此,我们不能确定find()不会引发异常

c++ - 为什么 set::find 不是模板?

使用来自的模板函数你可以这样做structfoo{intbar,baz;};structbar_less{//comparefoowithfoobooloperator()(constfoo&lh,constfoo&rh)const{returnlh.bar//comparesomeTwithfoobooloperator()(Tlh,constfoo&rh)const{returnlh//comparefoowithsomeTbooloperator()(constfoo&lh,Trh)const{returnlh.barbaz;}在std::set类似find的方法你必须传递一个s

c++ - 我如何构造一个仿函数以用于像 boost 的 brent_find_minima 这样的算法?

我试图理解为什么不能将具有构造函数的仿函数传递给算法,而没有构造函数的仿函数却可以。对于算法boost-brent_minima。当仿函数没有构造函数时,示例代码工作正常:#includestructfuncdouble{doubleoperator()(doubleconst&x){//return(x+3)*(x-1)*(x-1);//(x+3)(x-1)^2}};intbits=std::numeric_limits::digits;std::pairr=brent_find_minima(funcdouble(),-4.,4./3,bits);std::cout.precisi

c++ - m.find(...) == m.end() - 使用的是 iterator 或 const_iterator

std::mapfind/end都提供const_iterator和迭代器,例如iteratorend();const_iteratorend()const出于好奇,如果我有一个std::map,它将在这里被调用/比较,一个迭代器或一个const_iterator?:if(m.find(key)!=m.end()){...}我应该关心吗? 最佳答案 如果m是const,则返回一个const_iterator;否则将返回一个迭代器。如果您所做的只是测试map中是否存在某个元素,那么使用哪个元素并不重要。

c++ - 如何 std::find 使用比较对象?

我对std::find的接口(interface)感到困惑。为什么它不用Compare对象来告诉它如何比较两个对象?如果我可以传递一个Compare对象,我可以使下面的代码工作,我想在其中按值进行比较,而不是直接比较指针值:typedefstd::vectorVec;Vecvec;std::string*s1=newstd::string("foo");std::string*s2=newstd::string("foo");vec.push_back(s1);Vec::const_iteratorfound=std::find(vec.begin(),vec.end(),s2);//

c++ - 理论上,find_end 是可并行化的吗?

我目前正在研究open-stdproposal为我正在处理的项目带来并行功能,但我遇到了find_end的障碍。现在find_end可以描述为:Analgorithmthatsearchesforthelastsubsequenceofelements[s_first,s_last)intherange[first,last).Thefirstversionusesoperator==tocomparetheelements,thesecondversionusesthegivenbinarypredicatep.它的要求由cppreference列出.现在我并行化find/findi

c++ - std::find 和 std::map.find 都是 O(logN) 吗?

std::find和std::map.find都是O(logN)吗?如果是,std::find如何在对数时间内实现对std::map的搜索?std::find的实现是否专门用于std::map用例? 最佳答案 不,std::find是O(N),与容器无关。它不知道“容器”,没有针对std::map的专门化。std::find仅使用迭代器,它没有关于底层容器的信息。根据cppreference.com,实现等同于:templateInputItfind(InputItfirst,InputItlast,constT&value){fo

C++ - 错误 E2285 : Could not find a match for 'tolower(char *)' in function parseInput(fstream &)

给定以下代码:voidparseInput(fstream&inputFile){constintLENGTH=81;charline[LENGTH];while(!inputFile.fail()){inputFile.getline(line,LENGTH);line=tolower(line);cout编译时出现这个错误:ErrorE2285:Couldnotfindamatchfor'tolower(char*)'infunctionparseInput(fstream&)我知道它返回一个int,而不是int[],这是否意味着我不应该使用getline而应该将输入字符转换为字符

c++ - 我怎样才能使 std::find_if 和 std::map 使用一些 boost 库协同工作?

这个问题的灵感来自anothertopic这提出了这个问题:Findthefirstvaluegreaterthanuserspecifiedvaluefromamapcontainer可以通过多种方式解决。典型的C++03解决方案定义了一个专用函数(或仿函数)并将其传递给std::find_if作为第三个参数。在C++11中,可以避免定义专用函数(或仿函数),而是可以使用lambda作为:autoit=std::find_if(m.begin(),mp.end(),[n](conststd::pair&x)->bool{returnx.second>n;});这是theaccepte

c++ - CMake find_path 包含目录前缀

我正在为OpenNI编写一个最小的Find*.cmake。找到我写的头文件find_path(OPENNI_INCLUDE_PATHXnOS.h)按预期工作(OPENNI_INCLUDE_PATH的值为/usr/include/ni)。但是,在我的文件中,我必须包含标题#include我怎样才能去掉ni前缀,这样我就可以写了#include第一个包含的问题是包含了XnCppWrapper.h,并且此文件再次包含一些Xn*.hheader,但没有ni前缀。这会导致编译器错误。 最佳答案 总是有您用于find_path的路径匹配您的#i