草庐IT

set_charset

全部标签

c++ - 如何告诉 std::set 到 'refresh' 它的顺序?

如果集合中元素的值发生变化,排序可能不再正确。如这个小程序所示:#include#include#include#includestructComp{booloperator()(conststd::string*lhs,conststd::string*rhs){return*lhsMySet;MySetmySet;std::string*a=newstd::string("a");mySet.insert(a);std::string*c=newstd::string("c");mySet.insert(c);std::string*b=newstd::string("b");my

C++: STL: set: 存储值常量

具有以下代码:#include#include#include#includeusingnamespacestd;classEmployee{//...int_id;string_name;string_title;public:Employee(intid):_id(id){}stringconst&name()const{return_name;}voidsetName(stringconst&newName){_name=newName;}stringconst&title()const{return_title;}voidsetTitle(stringconst&newTitl

c++ - std::set_new_handler 如何提供更多可用内存?

来自std::set_new_handlernew-handler函数是分配函数在内存分配尝试失败时调用的函数。它的预期目的是三件事之一:提供更多可用内存终止程序(例如通过调用std::terminate)抛出类型为std::bad_alloc或派生自std::bad_alloc的异常下面的重载能保证什么吗?void*operatornew(std::size_tsize)throw(std::bad_alloc){while(true){void*pMem=malloc(size);if(pMem)returnpMem;std::new_handlerHandler=std::set

c++ - std::set 为数组中的每个元素

我需要为数组中的每个元素提供std::set功能。我怎样才能实现这个功能?我开始在C++中分配标准集的动态数组,如下所示:set*entry;接着是分配:entry=(set*)malloc(sizeof(set)*32);没有编译问题,但是当访问任何元素时运行时失败并出现段错误:entry[0].insert(23);非常感谢任何帮助。 最佳答案 关于#include#includeintmain(){std::vector>entry(32);//std::vectorconstructormakes32callstostd::

c++ - 插入期间容器 std::set 的行为?

插入集合时,集合内部是否多次删除了某些对象?我试图在下面的程序中插入两个MyClass类型的对象,但令我惊讶的是它调用了具有初始插入值的类的析构函数2次!我无法理解这背后的任何逻辑。任何人都可以对输出给出一些想法吗?(突出显示为粗体)#include#include#includeusingnamespacestd;structMyClass{doublenum;~MyClass(){printf("Destructorcalled..forval:%lf\n",num);}};typedefint(*fun_comp)(MyClass,MyClass);intcomp(MyClass

c++ - 如何在实现文件中初始化一个 static const set<string>?

我对staticconstset的初始化似乎不正确,我很感激你的指导方针:obj.h:classobj{...private:staticconstsetkeywords;...}obj.cpp:conststringkw[]={"GTR","LTR","LEQ","GEQ","NEQ","SQRT","sqrt"};constsetobj::keywords=(kw,kw+sizeof(kw)/sizeof(kw[0]));但这会产生错误:error:conversionfrom‘conststring*{akaconststd::basic_string*}’tonon-scala

c++ - Visual Studio 2017 "Object reference not set to an instance of an object."的 cppcheck 插件问题

我正在尝试将cppcheck插件与visualstudio2017一起使用。但是,当我选择工具/使用cppcheck检查当前项目时,它总是说“对象引用未设置为对象的实例。”有什么建议吗?我正在使用MicrosoftVisualStudioProfessional2017,版本15.7.6CPPCheckPlugin1.3.5CMake3.12.1Windows10专业版64位谢谢。 最佳答案 对我来说,在我编辑项目并选中“EnableCodeAnalysisonBuild”后问题就解决了。然后我可以取消选中此选项,但它离开了fals

c++ - std::set 中的函数和方法指针

如所述inanothermessageofmine,不可能将2个成员函数指针与"进行比较。或者至少,这会导致未定义的行为。我刚刚成功地用VisualC++和GCC编译了这段代码:templateclassEventReceiver:publicIFunction{protected:std::settest;std::settest2;...据我所知,要制作任何东西的std::map或std::set,必须可以将集合的值与“ 最佳答案 好吧,编译示例代码实际上误导。事实上,这些套装无法使用。每次尝试向其中插入数据都会产生预期的错误。

c++ - 如何测试使用我可以使用的 MPI 调用 : is there any set of mock functions, 的 C\C++ 代码?

#includedoublef(){doubletimer=MPI_Wtime();returntimer;}我要搜索的是一组模拟MPI函数,所以我可以#include而不是mpi.h并在不链接到MPI库的情况下编译它。它也可能类似于#defineMOCK这将在mpi.h中触发此行为。 最佳答案 据我所知,没有任何模拟可以让您直接#include并完成。但是,无论您选择使用哪种模拟框架,您自己编写所需的函数应该没什么大不了的。只需从一个空header开始,看看您的编译器提示了哪些函数。

c++ - 使用 lambda 表达式作为 std::set 的比较,当它在 vector 中时

我想使用lambda表达式作为std::整数集的自定义比较。这个网站上有很多答案解释了如何做到这一点,例如https://stackoverflow.com/a/46128321/10774939.事实上,#include#include#includeintmain(){autodifferent_cmp=[](inti,intj)->bool{returnjintegers(different_cmp);integers.insert(3);integers.insert(4);integers.insert(1);for(intinteger:integers){std::cou