简体   繁体   English

C ++ std ::从std :: deque复制到std:; set

[英]C++ std::copy from std::deque to std:;set

I have a class with "array of array" private member represented as: 我有一个带有“数组数组”私有成员的类,表示为:

std::deque<std::deque<SomeClass> > someArray_;

Also this class have a public method which allows to receive all unique SomeClass instances, containing in someArray_ . 此类还具有一个公共方法,该方法允许接收包含在someArray_所有唯一SomeClass实例。 Unique for SomeClass instances means different by at least one of several class members. SomeClass实例的唯一性意味着至少有几个类成员不同。 I decided to use std::set for that purpose. 我决定为此目的使用std::set This method has prototype like following: 此方法具有如下原型:

std::set<SomeClass> getAllUniqueInstances() const;

In this method implementation I use following construction to populate std::set : 在此方法实现中,我使用以下构造来填充std::set

std::set<SomeClass> allUniqueInstances;
for(auto it = std::begin(someArray_); it != std::end(someArray_); ++it){
    std::copy((*it).begin(), 
              (*it).end(),
              std::inserter(allUniqueInstances, allUniqueInstances.end()));
}

operator<() is defined for SomeClass class. operator<()是为SomeClass类定义的。 As a result my std::set is populated, but huge amount of instances is missed. 结果,我的std::set被填充,但是遗漏了大量实例。 Modyfing operator<() for SomeClass class, alters situation, but breaks desirable sorting order. SomeClass类的modyfing operator<()会改变情况,但会破坏所需的排序顺序。 How in this case std::copy determines whether considerable instance is unique? 在这种情况下, std::copy如何确定重要实例是否唯一?

UPD : source code for SomeClass UPDSomeClass源代码

class SomeClass{
    private:
        uint32_t from_;
        uint32_t to_;
        double capacity_;
        double flow_;
    public:        
        ...
        bool operator<(const SomeClass& rhs) const;
        ...
    };

I want SomeClass instances to be ordered in set by from_ member: 我希望SomeClass实例按from_成员的顺序排序:

bool SomeClass::operator<( const SomeClass& rhs ) const{
    if(this->from_ < rhs.from_)
        return true;    
    return false;    
}

It is not std::copy who decides whether instances are unique, but std::set . 不是std::copy决定实例是否唯一,而是std::set The logic is something like 逻辑有点像

(A < B is false) and (B < A is false) (A <B为假)和(B <A为假)

So the criterion that defines the ordering also defines the "uniqueness". 因此,定义排序的标准也定义了“唯一性”。 It seems like std::set is the wrong data structure for this problem, or your ordering criteria are either incorrect (as in not implementing strict weak ordering), or too broad to suit the problem (as in, you order based on a small number of attributes when you could use more). 似乎std::set是针对此问题的错误数据结构,或者您的排序标准不正确(例如,未实施严格的弱排序),或者过于宽泛以致于无法解决问题(例如,您基于小量排序可以使用更多的属性数量)。

Here is an example of a lexicographical comparison using more attributes than you currently have: 这是一个字典比较示例,它使用的属性比当前更多:

#include <tuple> // for std::tie

bool SomeClass::operator<( const SomeClass& rhs ) const
{
  return std::tie(from_, to_, capacity_, flow_) < std::tie(rhs.from_, rhs.to_, rhs.capacity_, rhs.flow_);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM