草庐IT

iter_swap

全部标签

c++ - 在 Visual Studio 中调用 std::swap 时的 std::bad_function_call

我正在尝试将我的代码从Linux移植到Windows。但是,对于VisualStudio,我的代码因以下错误而崩溃:MicrosoftC++exception:std::bad_function_callatmemorylocation这是我的代码:#includeclassFoo{public:Foo(int):m_deleter{[](){}}{}Foo(constFoo&)=delete;Foo(Foo&&)=default;Foo&operator=(constFoo&)=delete;Foo&operator=(Foo&&)=default;~Foo(){m_deleter(

c++ - 将 initializer_list 插入 std::vector 时出现 "Invalid iterator range"

此代码在Ideone上按预期编译并运行良好:#include#include#includeintmain(){std::vectorstrVec;strVec.insert(strVec.end(),{L"black",L"white",L"red"});strVec.insert(strVec.end(),{L"blue",L"green"});//STLexceptionfor(auto&i:strVec){std::wcout但是,在MSVC(VisualStudio2013)中因“无效的迭代器范围”而失败。有什么见解吗?顺便说一句,插入更多元素是可行的,例如在第二个插入中,这

c++ - 当您无法测量差异时如何处理 iterator::difference_type?

我正在为第3方C库编写C++包装器。该库提供了一些用于迭代一系列对象的函数。我想编写一个迭代器来包装此行为,以便迭代更容易,但我想不出我将如何提供强制性的“差异”类型,因为迭代对象没有有意义的相对顺序和API我正在使用不提供提前查找可用对象数量的方法。我无法在迭代时对对象进行计数,因为尽管这可以解决单个迭代器,但它会导致end()迭代器和其他迭代器之间的差异未定义。 最佳答案 C++中的所有迭代器都需要提供某种difference_type。该类型是否有意义或有用是一个完全不同的问题。根据您的描述,您似乎正在使用输入迭代器,它使您能

c++ - 为什么会出现运行时错误 : Vector erase iterator outside range

我遇到了以下代码的奇怪运行时错误:#include#includeusingstd::vector;structData{intid;};intmain(){vectormylist;Datam;m.id=10;mylist.push_back(m);mylist.erase(std::remove_if(mylist.begin(),mylist.end(),[](constData&m){returnm.id>100;}));return0;}错误说:Vectoreraseiteratoroutsiderange我不是在解决了类似Ref1的问题之后,Ref2但意识到问题的原因以及我

c++ - 如何从CGAL中的Edge_iterator获取源点和目标点

我对某些点进行了Delaunay三角剖分,并希望按长度升序迭代其中的所有边,以构建最小跨度线程。我试过以下方法,但无法编译:typedefCGAL::Exact_predicates_inexact_constructions_kernelK;typedefCGAL::Delaunay_triangulation_2T;typedefK::Point_2P;typedefT::Vertex_handleVh;typedefT::Vertex_iteratorVi;typedefT::Edge_iteratorEi;boolsortFunction(Eia,Eib){K::FTla,lb

c++ - copy-and-swap 习语在 self 分配期间如何工作?

我正在阅读优秀的copy-and-swapidiom问题和答案。但是我没有得到一件事:在self分配的情况下它是如何工作的?例子中提到的对象other不会释放分配给mArray的内存吗?那么,自分配的对象不会以拥有无效指针而告终吗? 最佳答案 ButonethingIamnotgettinghowdoesitworkincaseofselfassignment?让我们看一个简单的例子:classContainer{int*mArray;};//CopyandswapContainer&operator=(Containerconst

c++ - 为什么我不能用 const_iterator 调用模板基类构造函数?

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。由于某些原因,下面的代码给出了错误Symbol'TemplateBase'couldnotberesolved.:templateclassTemplateBase{TemplateBase(std::map::const_iteratoranIterator){}};classSubClass:publicTemplateBase{SubClass(std::map::const_iteratoranIterator)

c++ - std::swap 是否保证通过 ADL 找到非成员交换?

这个问题在这里已经有了答案:DoesC++11changethebehaviorofexplicitlycallingstd::swaptoensureADL-locatedswap'sarefound,likeboost::swap?(4个答案)关闭6年前。TorstenT.Will关于C++11的教科书说,自C++11起,std::swap将使用ADL找到的非成员swap,如果定义了这样一个非成员函数,因此模式usingstd::swap;swap(obj1,obj2);总是可以被简单的替换std::swap(obj1,obj2);很遗憾,我在其他任何地方都没有找到这样的声明。什么

C++,重载 std::swap,编译器错误,VS 2010

我想在我的模板类中重载std::swap。在下面的代码中(简化)#ifndefPoint2D_H#definePoint2D_HtemplateclassPoint2D{protected:Tx;Ty;public:Point2D():x(0),y(0){}Point2D(constT&x_,constT&y_):x(x_),y(y_){}....public:voidswap(Point2D&p);};templateinlinevoidswap(Point2D&p1,Point2D&p2){p1.swap(p2);}namespacestd{templateinlinevoidsw

c++ - 如何转发声明 boost::ptree::iterator

我想在我的项目中使用boostptree,但由于ptree.hpp导致包含另外1000个头文件,这大大增加了编译时间(例如从1秒到7秒),并且因为它在20多个不同的cpp文件中需要这是NotAcceptable(预编译的header不会改善太多)。所以我正在考虑将boostptree封装在我自己的类中,比如//myptree.h#includeclassmyptree{private:boost::property_tree::ptree*m_tree;public:...//addingnew(singlevalue)memberstothethetreevoidput(consts