草庐IT

find_one

全部标签

c++ - 普通数组上的 std::lower_bound 和 std::find

我喜欢在普通数组上尽可能使用std::algorithm。现在我有2个疑问;假设我想使用std::lower_bound如果找不到我作为参数提供的值会怎样?inta[]={1,2,3,4,5,6};int*f=std::lower_bound(a,a+6,20);我打印*f时的结果是20。如果我使用std::find,也会发生同样的情况。inta[]={1,2,3,4,5,6};int*f=std::find(a,a+6,20);我打印*f时的结果是20。返回值是否总是原始参数什么时候找不到?在性能方面,std::lower_bound比std::find表现更好,因为它实现了二进制搜

c++ - 未取消引用的迭代器是否超过了数组未定义行为的 "one past-the-end"迭代器?

给定intfoo[]={0,1,2,3};我想知道指向“过去的最后一个”的迭代器是否无效。例如:autobar=cend(foo)+1;在Stack溢出问题中有大量提示和警告称这是“未定义的行为”,例如:c++what'stheresultofiterator+integerwhenpast-end-iterator?不幸的是,唯一的来源是挥手。我在购买时遇到了越来越多的麻烦,例如:int*bar;未初始化,但肯定不会调用未定义的行为,并且经过足够的尝试,我确信我可以找到一个实例,其中未初始化bar中的值与cend(foo)+1具有相同的值.这里最大的困惑之一是我不是询问解除引用cen

c++ - STL find 的性能优于手工循环

我有一些问题。给定以下C++代码片段:#include#include#include#include#includestructincrementor{incrementor():curr_(){}unsignedintoperator()(){returncurr_++;}private:unsignedintcurr_;};templatecharconst*value_found(Vecconst&v,typenameVec::const_iteratori){returni==v.end()?"no":"yes";}templatetypenameVec::const_ite

C++模板: one list by class,如何分解代码?

假设我有这个类(class):classComponent1;classComponent2;//manydifferentComponentsclassComponent42;classMyClass{public:MyClass(void){};std::listcomponent1List;std::listcomponent2List;//onelistbycomponentstd::listcomponent42List;};我想创建一个具有以下签名的函数:templatevoidaddElement(Tcomponent);它应该执行以下操作:如果component是Com

c++ - 我使用 C++ 标准库的 find 有什么问题?

我正在尝试像这样使用C++标准库的find算法:templateconstunsignedintAdjacencyList::_index_for_node(conststd::vector&list,constT&node)throw(NoSuchNodeException){std::vector::iteratoriter=std::find(list.begin(),list.end(),node);}当我尝试编译时,出现以下错误:Infileincludedfrom../AdjacencyList.cpp:8:../AdjacencyList.h:Inmemberfuncti

c++ - 检查字符串是否以另一个字符串 : find or compare? 开头

如果您想知道一个字符串是否以另一个字符串开头,您将如何在C++/STL中做到这一点?Java中有String.startsWith,Python也有string.startwith,STL没有直接的方法。相反,有std::string::find和std::string::compare。直到现在我都使用了这两种方法,主要取决于我现在的心情:if(str1.compare(0,str2.length(),str2)==0)do_something();if(str1.find(str2)==0)do_something();当然,你也可以做到str.substr(0,str2.leng

c++ - CMake FIND_PACKAGE 成功但返回错误的路径

我正在尝试使用我的CMakeLists.txt中的以下代码将CMake2.8.6链接到boost::program_optionsFIND_PACKAGE(BoostCOMPONENTSprogram_optionsREQUIRED)INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})ADD_EXECUTABLE(segmentsegment.cpp)TARGET_LINK_LIBRARIES(segment${Boost_LIBRARIES})find命令似乎成功,但将错误的目录传递给链接器。包实际上在:`/usr/lib64/libboost_prog

Mongodb find 查询与 $near 和坐标不起作用

我正在尝试使用mongodb中的一些地理定位功能。使用带有$near的查找查询似乎不起作用!我的数据库中目前有这个对象:{"Username":"Deano","_id":{"$oid":"533f0b722ad3a8d39b6213c3"},"location":{"type":"Point","coordinates":[51.50998,-0.1337]}}我也设置了以下索引:{"v":1,"key":{"location":"2dsphere"},"ns":"heroku_app23672911.catchmerequests","name":"location_2dspher

Mongodb find 查询与 $near 和坐标不起作用

我正在尝试使用mongodb中的一些地理定位功能。使用带有$near的查找查询似乎不起作用!我的数据库中目前有这个对象:{"Username":"Deano","_id":{"$oid":"533f0b722ad3a8d39b6213c3"},"location":{"type":"Point","coordinates":[51.50998,-0.1337]}}我也设置了以下索引:{"v":1,"key":{"location":"2dsphere"},"ns":"heroku_app23672911.catchmerequests","name":"location_2dspher

c++ - std::vector 的 std::lower_bound 比 std::map::find 慢

我编写了一个类来充当顺序容器(std::vector/std::queue/std::list)的包装器,以具有std::map的接口(interface),用于使用少量小对象时的性能。考虑到已经存在的算法,编码非常简单。这段代码显然是高度从我的完整代码中删减的,但显示了问题。template,classundertype_=std::vector>>classassociative{public:typedeftraits_key_compare;typedefkey_key_type;typedefmapped_mapped_type;typedefstd::pairvalue_t