草庐IT

std-ranges

全部标签

c++ - 为什么 std::vector 需要 operator =

我有一个关于我们可以存储在vector中的类的问题。可以存储在vector中的要求是什么?似乎这样的类必须有赋值运算符。但我不确定是否仅此而已。让我举个例子。A类有constint成员。如果我不写operator=,它就不会编译。但是在这个例子中,这个操作符什么都不做。该程序正确显示10和20。看起来operator=是必需的,但实际上并没有使用。#include#includeclassA{public:A(inta):a_(a){}A&operator=(constA&a2){return*this;}//Withoutthis,compilefails.voidprint()co

c++ - C++11 可以判断 std::thread 是否处于事件状态吗?

令我惊讶的是,一个已完成执行但尚未加入的C++11std::thread对象仍然是considered一个活跃的执行线程。以下代码示例对此进行了说明(基于Xubuntu13.03和g++4.7.3)。有谁知道C++11标准是否提供了一种方法来检测std::thread对象是否仍在主动运行代码?#include#include#include#include#includeintmain(){autolambdaThread=std::thread([](){std::cout 最佳答案 不,我不认为这是可能的。我也会尝试考虑您的设计

c++ - 如何在 std::set 中存储指向对象的指针(或引用)

在C++11STL中是否有适当的方法将对象指针存储在std::set中?,并让它们按对象的operator正确排序方法?当然,我也可以自己编写Compare输入并将其传递给set作为它的第二个模板参数,但我想STL会提供一种更方便的方法。谷歌搜索显示std::reference_wrapper,在我看来应该允许这样的代码:#include#includestructT{intval;booloperatorval>s;Ta{5};s.insert(a);}但实际上,这会导致编译错误:clang++-std=c++11-Wall-Wextra-pedantictest.cpp-otest

c++ - std::unique_ptr::reset 检查托管指针是否为空?

我一直在阅读有关C++11智能指针的内容,以便在我的源代码中使用它们,我一直在阅读的文档是cppreference.com上的文档;在阅读std::unique_ptr时,在resetfunction上有一个文档对我来说似乎不正确(强调我的):Replacesthemanagedobject.Givencurrent_ptr,thepointerthatwasmanagedby*this,performsthefollowingactions,inthisorder:Savesacopyofthecurrentpointerold_ptr=current_ptr.Overwritest

c++ - Boost.Spirit 编译器无法识别 std::pair

#include#includenamespaceqi=boost::spirit::qi;intmain(){//thefollowingparses"1.02.0"intoapairofdoublestd::stringinput("1.02.0");std::string::iteratorstrbegin=input.begin();std::pairp;qi::phrase_parse(strbegin,input.end(),qi::double_>>qi::double_,//parsergrammarqi::space,//delimitergrammarp);//at

c++ - 为什么 std::allocator<>::deallocate() 有一个未使用的 size_type 参数?

使用std::allocator时,deallocate函数需要pointer参数,和一个size_type参数(std::allocator::deallocate(std::allocator::pointerp,std::allocator::size_type)。但是,没有使用size_type,也不是可选的。那么为什么它在那里?这让我很困惑,因为它应该是可选的,甚至不在那里,因为它没有在函数中使用.编辑:MSVC的分配器实现deallocatevoiddeallocate(pointer_Ptr,size_type){//deallocateobjectat_Ptr,igno

c++ - 为什么 std::function 的初始化器必须是 CopyConstructible?

根据http://en.cppreference.com/w/cpp/utility/functional/function/function,初始化器的类型,即形式(5)中的F,应该满足CopyConstructible的要求。我不太明白这个。为什么F不能只是MoveConstructible? 最佳答案 std::function在内部使用类型删除,因此F必须是CopyConstructible,即使您正在使用的特定std::function对象从未被复制。类型删除工作原理的简化:classFunction{structConc

c++ - std::shared_ptr<T>:指向 T 的右值指针的隐式构造函数

我非常支持制作std::shared_ptr的想法接受T*的构造函数明确的。当您正在寻找堆损坏的原因时,它有助于避免不眠之夜。ScottMeyers对此给出了很好的解释。但是……如果我给它一个rvalue这不是明确的指针吗?我可以做这样的事情:///(1)std::shared_ptrt=newT;或///(2)T*giveaway=newT;std::shared_ptrt=std::move(giveaway);或者现实生活中更痛苦的案例///(3)voidfoo(std::shared_ptrt);///...foo(newT);对于我来说,所有这些案例都足够明确了。案例(1)是

c++ - 插入从函数返回的 std::vector

我发现自己经常做这样的事情来连接从函数(可能是类函数)返回的几个vector:#include#includeusingnamespacestd;vectorv1;constvector&F1(){coutConcat;Concat.insert(Concat.end(),F1().begin(),F1().end());/*DosomethingwithConcat*/return0;}如我所料,F1()被调用了两次,如果它是一个昂贵的函数调用,这可能是不受欢迎的。另一种方法是将F1()的返回值复制到一个临时vector中,这只需要一次函数调用,但会导致复制操作,如果vector很大

C++ std::bind 重新绑定(bind)函数

如果我像这样绑定(bind)一个函数,在绑定(bind)时使用占位符std::bind(memberFunctionPointer,objectPointer,_1,_2);然后是否可以稍后“重新绑定(bind)”它以替换一些/所有占位符,但不调用该函数?我希望能够传入一些参数然后存储它,以便稍后调用。(延迟回调) 最佳答案 您可以再次绑定(bind):autof=std::bind(memberFunctionPointer,objectPointer,_1,_2);autog=std::bind(f,val1,val2);g()