草庐IT

c++ - 将 std::bind 与重载函数一起使用

我无法找到如何使用std::bind将参数绑定(bind)到重载函数。std::bind无法推断出重载类型(对于其模板参数)。如果我不重载函数,一切正常。代码如下:#include#include#includeusingnamespacestd;usingnamespacestd::placeholders;doublef(doublex){returnx;}//std::bindworksifthisoverloadediscommentedoutfloatf(floatx){returnx;}//wanttobindto`f(2)`,forthedouble(double)ver

c++ - std::array<char, N> 的大小是多少?

这个问题在这里已经有了答案:Isthesizeofstd::arraydefinedbystandard(1个回答)关闭8年前。C++标准对sizeof(std::array)有何规定?应该是(对于某个常量N)?在commenttoadifferentquestion中,有人提到std::array并不总是“堆栈分配”。该评论是对另一条评论的回应,该评论推测为std::array设置了一个太大的常量。声明为局部变量的变量可能会由于“堆栈分配”变量的资源不足而导致程序中止。我假设后续评论意味着std::array是可能的以某种方式切换到动态分配模式。我可以想象,可能会有某种SFINAE可

c++ - 使用 std::prev(vector.begin()) 或 std::next(vector.begin(), -1) 像 some_container.rend() 作为反向哨兵是否安全?

我写了一些采用迭代器但必须以相反顺序进行比较的代码,templateboolfunc(ConstBiIterseq_begin,ConstBiIterseq_end){ConstBiIterlast=std::prev(seq_end);while(--last!=std::prev(seq_begin))//-->Ineedtocomparethebeginningdata{......}returntrue;}在VS2013中,在Debug模式下运行时,--last!=std::prev(seq_begin)将导致调试器断言失败并显示错误消息Expression:stringite

c++ - 如何在不复制的情况下使用 std::string?

我有一个类说,classFoo{public:voidProcessString(std::string&buffer){//performoperationsonstd::string//callotherfunctionswithinclass//whichusesamestd::stringstring}voidBar(std::string&buffer){//performotheroperationson"std::string"buffer}voidBaz(std::string&buffer){//performotheroperationson"std::string

c++ - std::map 可以在调用 const 函数期间重新平衡吗?

我有一个conststd::map>成员变量和函数conststd::vector*foo().我希望此函数有时返回指向此map元素的指针。但我担心map可能会重新平衡-即使在std::map期间也是如此标记为const的功能-使我返回的指针无效。我知道对map的任何后续修改都会使我的指针无效,但由于我已将成员变量标记为const,所以这种情况不会发生。.我有时无法返回引用,foo需要返回nullptr.我所做的安全吗? 最佳答案 标准很明确:唯一可以无效的映射的迭代器或指针或引用正在删除它指向的元素。您甚至可以插入其他元素不会使您

c++ - std::大小为零的数组

std::array是什么意思?,大小为零的数组?在发布之前,我已经在SO中解决了类似的问题,所有这些问题是关于简单数组类型和C语言的,他们中的大多数人说这是非法的。但在C++中array是允许的。根据cppreference.comThereisaspecialcaseforazero-lengtharray(N==0).Inthatcase,array.begin()==array.end(),whichissomeuniquevalue.Theeffectofcallingfront()orback()onazero-sizedarrayisundefined.为什么不定义为非法

c++ - std::move 与 std::make_pair

有什么区别:std::map>m;Tt1,t2;m.emplace(1,std::make_pair(t1,t2));和:std::map>m;Tt1,t2;m.emplace(1,std::move(std::make_pair(t1,t2)));std::move在这里是多余的吗?std::map::emplace和perfectforwarding是否负责直接在std::中分配std::pairmap? 最佳答案 std::make_pair(...)和std::move(std::make_pair(...))都是右值表达式

c++ - 为什么 std::queue 在弹出元素后不收缩内存?

我写了一个使用std::queue的小程序queuethe_queue;for(inti=0;i我在printf("Donepushing\n");和printf("Donepopping\n");处设置了2个断点,并检查程序的内存使用情况(显示在任务管理器中)当遇到断点时。在Donepushing时,内存使用量约为34MB,但在Donepopping时,内存使用量仍约为34MB。这让我很吃惊!这是为什么?有什么办法可以克服这个问题吗? 最佳答案 基本上std::queue是一个AdapterContainer-它不是一个单独的容器

c++ - C++11 中的 UTF 转换函数

我正在寻找用于在C++11中执行UTF字符转换的函数集合。它应该包括与utf8、utf16和utf32之间的任何转换。识别字节顺序标记的函数也会有帮助。 最佳答案 更新:此处列出的函数在GitHub存储库中维护,.hpp,.cpp和tests.某些UTF-16函数已被禁用,因为它们无法正常工作。utf.test.cpp文件中的“banana”测试证明了这个问题。还包括用于识别字节顺序标记的“read_with_bom”函数。#if_MSC_VER==1900//workaroundforbuginMSVisualC++2015htt

C++ vector emplace_back 调用复制构造函数

这是一个演示类(class)。我不希望我的类被复制,所以我删除了复制构造函数。我希望vector.emplace_back使用此构造函数“MyClass(Typetype)”。但是这些代码不会编译。为什么?classMyClass{public:typedefenum{e1,e2}Type;private:Type_type;MyClass(constMyClass&other)=delete;//nocopypublic:MyClass():_type(e1){};MyClass(Typetype):_type(type){/*theconstructorIwanted.*/};};