草庐IT

Union-Find

全部标签

c++ - 什么是正确的 std::set_union 代码?

Thissite声称set_union等效于以下代码:templateOutputIteratorset_union(InputIterator1first1,InputIterator1last1,InputIterator2first2,InputIterator2last2,OutputIteratorresult){while(true){if(*first1但这看起来很奇怪:如果其中一个范围为空,会不会崩溃(或导致其他未定义的行为)?这两个if子句不应该在while循环的开头,而不是结尾吗? 最佳答案 我同意它看起来完全坏

c++ - 在 C++ 中使用 .find() 和 struct 作为映射中的键

我无法访问BOOST或STL;我的结构和map看起来类似于以下伪装:structs_map_key{inta;intb;booloperatormyMap;for(inti=0;i::iteratorx=myMap.find(smk);if(x!=myMap.end()){std::coutfirst.afirst.b我想做的是在我的多重映射中搜索A=2、B=2或A&B=2的所有情况。我不太确定,但我想我需要在我的结构中创建谓词对于“发现”。想法? 最佳答案 operator这就是find所需要的或其他任何东西。但是,您的实现有一个

c++ - std::find 用于继承对象 C++

1.所以我有:ClassA;ClassB:publicA;ClassC:publicB;2.还有一个B类型的指针vector:vectorvec;3.然后:C*ptr=newC();vec.push_back(ptr);那么问题来了,这样用std::find靠谱吗?std::find(vec.begin(),vec.end(),prt);此外,使用this->指针进行搜索可以吗?std::find(vec.begin(),vec.end(),this);//insideofatypeCobject提前致谢。 最佳答案 是的,这是安全

c++ - 将 std::array<POD, N> 放入 union 中是否安全?

我有一个这样声明的union:union{intall[4];struct{inta,b,c,d;};};点allarray只是为了简化4个字段的迭代。为了让它更简单,我想用std::array替换它.那会使我暴露于nasaldemons吗?? 最佳答案 首先,重要的是要注意,union中只有两个不同类型的对象永远不会是未定义的。未定义的是写入一个并从另一个读取,但有一个异常(exception):[C++11:9.5/1]:[Note:Onespecialguaranteeismadeinordertosimplifytheuse

c++ - 为包含 union 的结构定义 `swap` 方法;怎么做?

我正在调整一些C++03代码以利用C++11的新可能性,特别是以C++11的方式引入移动语义。但是我遇到了一个struct,这让我很头疼,因为它包含一个匿名union。它具有全局形式structtree{//dataof|tree|enum{tag0,tag1,tag2,...}kind;union{type1field1;type2field2;...};//constructorstree():kind(tag0){}//default,emptystatetree(const&type1x):kind(tag1),field1(x){}//variant1tree(const&t

c++ - 松散常量表达式中 union 非事件成员的左值到右值转换的解决方法

触发union的非事件成员的左值到右值转换不是常量表达式。也就是说,给定union:templateunionA{constexprA(Tt):t_{t}{}constexprA(Uu):u_{u}{}Tt_;Uu_;};和constexpr函数foo:templateconstexprautofoo(){Aa(T{});returna.u_;}以下程序:intmain(){constexprautotest=foo();return0;}失败并显示错误消息:error:constexprvariable'test'mustbeinitializedbyaconstantexpress

c++ - 在常量表达式中更改 union 的事件成员

玩constexpr和union我发现,我无法更改union的活跃成员在constexpr.只有一个异常(exception):union空类。constexprboolt(){structA{};structB{};unionU{Aa;Bb;}u{};u.a=A{};u.b=B{};returntrue;}static_assert(t());constexprboolf(){structA{charc;};structB{charc;};unionU{Aa;Bb;}u{};u.a=A{};u.b=B{};//errororiginatingfromherereturntrue;}s

C++ 默认构造函数与具有非平凡默认构造函数的变体成员 union

我最近阅读了union体默认构造函数的描述:DefaultConstructor有如下规律:BlockquoteDeletedimplicitly-declareddefaultconstructor:[...]Tisaunionwithatleastonevariantmemberwithnon-trivialdefaultconstructor,andnovariantmemberofThasadefaultmemberinitializer.[...]然后我决定做一个练习来验证规则。structMember{public://Member():mX(0){}virtualintG

c++ - 在 std::find 中使用来自不同命名空间的运算符

我有以下自动生成的代码:#include#includenamespacefoo{structS{};namespaceinner{booloperator==(constS&,constS&){returntrue;}}}namespacebar{voidfunc();}我现在想使用STL的find算法在容器中搜索S对象:voidbar::func(){std::vectorv;foo::Ss;std::find(v.begin(),v.end(),s);}但是我得到这个错误:/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/bit

c++ - union 的内存对齐问题

如果我们在堆栈中创建这种类型的对象,是否可以保证该对象的内存将正确对齐?unionmy_union{intvalue;charbytes[4];};如果我们在堆栈中创建charbytes[4]然后尝试将其转换为整数,则可能存在对齐问题。我们可以通过在堆中创建它来避免这个问题,但是,union对象有这样的保证吗?逻辑上应该有,但我想确认一下。谢谢。 最佳答案 嗯,这取决于你的意思。如果你的意思是:Willboththeintandchar[4]membersoftheunionbeproperlyalignedsothatImayus