草庐IT

std-function

全部标签

c++ - std::string == 运算符不工作

多年来,我一直在Windows和Linux上使用std::string的==运算符。现在我正在linux上编译我的一个库,它大量使用==。在linux上,以下函数失败,因为即使字符串相等(区分大小写,相等)==也返回falseconstData*DataBase::getDataByName(conststd::string&name)const{for(unsignedinti=0;igetName()==name){returnm_dataList.get(i);}}returnNULL;}getName()方法声明如下virtualconststd::string&getName

c++ - 使用 boost::random 从要删除元素的 std::list 中进行选择

查看此relatedquestion更通用地使用BoostRandom库。我的问题涉及从std::list中选择一个随机元素,执行一些操作,这可能包括从列表中删除元素,然后选择另一个随机元素,直到满足某些条件满意。boost代码和for循环大致如下所示://createandinsertelementsintoliststd::listmyList;//[...]//selectuniformlyfromlistindicesboost::uniform_intindices(0,myList.size()-1);boost::variate_generator>selectIndex

c++ - 如何使用 bind 基于::second pair 成员排序的 std::pair 创建集合

我知道我可以使用以下内容:templatestructComparePairThroughSecond:publicstd::unary_function{booloperator()(constPair&p1,constPair&p2)const{returnp1.second,ComparePairThroughSecond>somevar;但想知道是否可以用boost::bind来完成 最佳答案 下一个怎么样。我正在使用boost::function来“删除”比较器的实际类型。比较器是使用boost:bind本身创建的。typ

c++ - std::map 按数据排序?

有没有办法按数据而不是键对std::map进行排序?现在我的代码将整个map复制到一个数组中只是为了做到这一点。 最佳答案 据我所知,std::map将为您提供迭代器,该迭代器将遍历按键排序的项目。要按值遍历已排序项目并仍然使用映射,唯一的方法是将整个集合重写为另一个映射,键和值颠倒。 关于c++-std::map按数据排序?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/399

c++ - 如何定义和使用 boost::function with "optional arguments"?

我正在使用一个需要某种回调方法的类,所以我正在使用boost::function来存储函数指针。我需要回调有一个可选参数,但我发现boost::function不允许我定义可选参数类型,所以我尝试了以下代码并成功了..//thesecondargumentisoptionaltypedefboost::functionmyHandler;classA{public://handlerwith2argumentsintfoo(intx,char*a){printf("%s\n",a);return0;};//handlerwith1argumentintboo(intx){return1

C++ 从可变长度参数列表中提取 std::string

大家好!我正在尝试制作一个返回格式化字符串的sprintf的简单拷贝,但我遇到了一个小问题......显然,使用可变长度参数列表您不能传递std::string实例。我已经让解析器正确处理int、double、float、char、constchar*、char*...我还没有让字符串工作。:\如果您想知道,这是我得到的编译错误:/root/learncpp/StringFormat/main.cpp:8:warning:cannotpassobjectsofnon-PODtype'structstd::string'通过'...';调用将在运行时中止我这样做的主要原因是我可以方便地格

c++ - 为什么 std::queue 允许冗余类型规范?

STL库类std::queue的前向声明如下:namespacestd{template>classqueue}这意味着我们可以像这样声明一个具有不同类型规范的队列类型的对象:std::queue>string_queue;为什么这是可能的?像这样声明队列不是更安全吗:templateclassqueue_base{private:implementationm_impl;/*-----------------------------------------------------------*/public:typedefimplementationcontainer_type;ty

c++ - 在 boost 中,如何将 boost 迭代器传递给以某种方式转换为 std::string 的函数

请在以下代码末尾将特定问题作为注释查看。std::strings("mysamplestring\"withquotes\"");boost::escaped_list_separatorels("","","\"\'");boost::tokenizer>::iteratoritr;boost::tokenizer>tok(s,els);itr=tok.begin();if(itr!=tok.end())fn_that_receives_pointer_to_std_string(itr);// 最佳答案 boost::token

c++ - 调用 std::map::clear 的段错误

几个月来我一直在与段错误作斗争,现在我来这里寻求帮助。当我调用以下函数时出现段错误voidfoo(...,std::map&x){if(!x.empty())x.clear();...}ClassA{private:map_N;public:voidf(...){foo(...,_N);...}};//inmainroutine,thefunctioniscalledinaloopAa;while(...){a.f(...);}使用gdb,我将错误定位到调用clear()函数的行,它显示“doublefreeorcorruption”错误,程序在调用c++/4.1.2/ext/new_

c++ - "std::map with mutexes"与 "libcds maps (Michael Hashmap and Split Order List)"并行插入、查找、删除之间是否有任何速度测试?

所以我真的很想看到一些并行的速度测试(比如从100到10000个并行线程),其中每个线程至少在3种类型的并发映射上插入、查找、删除-std::map(有一些互斥锁)与libcds(ConcurrentDataStructures)...例如,如果这样的比较尚不存在,请帮助我创建一个。直接相关:LibCds:MichaelHashmapandSplitOrderList假设我们有#include#include#includeclassTestDs{public:virtualboolcontainsKey(intkey)=0;virtualintget(intkey)=0;virtua