草庐IT

c++ - 使用 Boost ptree 将 JSON 数组解析为 std::string

我有这段代码,我需要解析/或获取JSON数组作为std::string以在应用程序中使用。std::stringss="{\"id\":\"123\",\"number\":\"456\",\"stuff\":[{\"name\":\"test\"}]}";ptreept2;std::istringstreamis(ss);read_json(is,pt2);std::stringid=pt2.get("id");std::stringnum=pt2.get("number");std::stringstuff=pt2.get("stuff");需要的是像这样检索的“东西”std::s

c++ - 这是正确的行为吗? std::map 迭代器失效

#include#includeintmain(intargc,char**argv){std::mapmap;map.emplace(1,1);autoreverse_iter=map.rbegin();std::coutfirstsecondfirstsecond打印出来:1,12,2根据标准,这真的应该发生吗?我没有接触reverse_iter但它指向的值正在改变。我认为std::map中的迭代器应该可以安全地防止插入。然而,它似乎决定reverse_iter不再指向我告诉它的值,而是指向“此时map末尾发生的任何事情”。更新:更多信息,以防万一:前向迭代器似乎不会发生这种情况(

c++11 std::hash 函数对象类线程安全

在c++11中是在中声明的散列函数类对象线程安全?例如,从多个线程调用此函数是否安全?size_thash1(conststd::string&s){std::hashstr_hash;returnstr_hash(s);}或者,如果有一个全局对象std::hashstr_hash_global;,那么从多个线程调用第二个函数是否安全?size_thash2(conststd::string&s){returnstr_hash_global(s);} 最佳答案 标准库promise,如果您只在标准库对象上调用const限定的成员函数

c++ - 连接到 Poloniex Push-API

我想连接到PushAPIofPoloniex.在他们的页面上,他们写了以下内容:InordertousethepushAPI,connecttowss://api.poloniex.comandsubscribetothedesiredfeed.wss=WebSocket安全->SSL保护他们还给出了Node.js和Autobahn|JS的例子:varautobahn=require('autobahn');varwsuri="wss://api.poloniex.com";varconnection=newautobahn.Connection({url:wsuri,realm:"r

c++ - 为构造的 std::vector 中的每个元素调用默认构造函数

有没有办法构造std::vector通过为每个元素调用默认构造函数来处理N个元素?来自size_type的构造函数只需调用C的构造函数一次,然后对其余元素使用其复制构造函数。 最佳答案 Theconstructorfromsize_typejustcallsC'sconstructoronceandthenusesitscopyconstructorfortherestoftheelements.自C++11以来不正确.看std::vector::vectordocumentation:...vector(size_typecoun

c++ - 舍入整数例程

教程中的整数运算让我感到困惑。准确的说,整数除法。看似首选的方法是将除数转换为float,然后将float四舍五入为最接近的整数,然后将其转换回整数:#includeintround_divide_by_float_casting(inta,intb){return(int)std::roundf(a/(float)b);}然而,这就像用右手挠左耳一样。我使用:intround_divide(inta,intb){returna/b+a%b*2/b;}虽然没有什么突破,但不标准的事实让我怀疑我是否遗漏了什么?尽管我进行了(尽管是有限的)测试,但我找不到两种方法给我不同结果的任何情况。有

c++ - 在模板类中声明的 friend 模板函数导致 undefined symbol 链接错误

几天来我一直在努力反对这个问题,查找它并在开源项目中寻找类似的代码:无法真正找到我做错了什么。基本上,给定以下代码(提炼出其本质):#includeusingstd::cout;usingstd::endl;usingstd::string;templateclassNode{Tvalue_;public:Node(constT&value):value_(value){}Tconstvalue()const{returnvalue_;}friendstd::ostream&operator&node);Nodeoperator+(constNode&other){returnNode

C++ this怎么不是成员函数指针?

根据以下测试:std::cout::value不是成员函数指针,而是普通函数,而这个:std::cout::value计算结果为真。我用gcc和msvc都试过了。这两个声明有什么区别?这些结果正确吗?为什么A::*周围的括号很重要? 最佳答案 intA::*()是函数类型,它返回A的int成员,不带任何参数。所以它不是成员函数指针,更不是函数指针。std::cout::value::value::value括号改变了优先级,int(A::*)()是A的成员函数指针类型,返回int并且不带任何参数。

C++11 线程卡在锁定互斥锁上

使用C++11std::thread、std::mutex,我正在编写一个简单的工作线程。但是,我在锁定std::mutex时遇到了一个奇怪的挂起问题,看起来两个线程(主线程和工作线程)都试图锁定互斥锁,但都被阻止了。完整代码#include#include#include#include#includestd::condition_variablecv;std::mutexm;std::threadt;boolshouldExit=false;std::listjobs;voidthread_func(){std::unique_locklock(m);while(!shouldEx

c++ - 无法从 std::bind 推断出 std::function 的模板参数

我正在尝试找到一种方法来调用多个类成员函数,每个函数都有不同的参数,并且在调用前后会发生某些已知功能。这个包装函数是我试过的,但是例如对它的最终调用不会编译错误:'boolWrapper(Work*,std::function,Args&&...)':couldnotdeducetemplateargumentfor'std::function'from'std::_Bind,Work*const>'classWork{public:voidDoWork(inta,doubleb,stringc);private:voidPre(){};voidPost(){};boolStep1()