双映射插入问题

Double map insertion problem

本文关键字:问题 插入 映射      更新时间:2023-10-16

我有一个stl::map,其中键定义为我定义的对象,以及int。地图的用途如下:我有一个特定物体的列表我想数一下有多少相同的物体。所以我把对象插入到映射中。如果对象已经存在于地图中,我就增加它的值(因此计数器)。对象定义了所有基本操作符。对象由5个字符串组成。==运算符定义为对所有5个字符串进行比较,在逻辑上在上下文中是有意义的。问题是运算符

class Example
{
private:
    string one;
    string two;
    string three;
    string four;
    string five;
public:
    inline Example (string a_one,string a_two, string a_four, string a_five) :
        one(a_one),two(a_two),three(a_three),four(a_four),five(a_five)
        {}
    inline bool operator == (const Example& other) const
    {
        if (one == other.one)
        {
            if (two == other.two)
            {
                if (three == other.three)
                {
                    if (four == other.four)
                    {
                        if (five == other.five)
                        {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    inline bool operator < (const Example& other) const
    {
        if (one < other.one)
        {
            return true;
        }
        else if (two < other.two)
        {
            return true;
        }
        else if (three < other.three)
        {
            return true ;
        }
        else if (four < other.four)
        {
            return true;
        }
        else if (five < other.five)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
void CountExample(Example& example,std::map<Example,int>& counters);
void main()
{
    std::map<Example,int> counters;
    std::list<Example> examples = GetExamples();
    //GetExamples defined elsewhere, and initializes examples with a long list of instances of Example
    std::list<Example>::const_iterator Iter;
    for (Iter = examples.begin();Iter != examples.end();Iter++)
    {
        CountExample(*Iter);
    }
    PrintCounters(counters);//PrintCounters defined elsewhere and prints the map to a file
}
void CountExample(Example& example,std::map<Example,int>& counters)
{
    std::map<Example,int>::const_iterator Iter;
    Iter = counters.find(example);
    if (Iter ==counters.end()) //means the specific Example is not in the map
    {
        counters.insert(std::pair<Example,int>(example,1));
    }
    else
    {
        counters[example] += 1;
    {
}

如果您有一个相当现代的编译器,那么可以用两个std::tie() 'd元组之间的单个比较来替换这个比较阶梯:

#include <tuple>
...
bool operator== (const Example& other) const
{
    return std::tie(one, two, three, four, five)
        == std::tie(other.one, other.two, other.three, other.four, other.five);
}
bool operator < (const Example& other) const
{
    return std::tie(one, two, three, four, five)
         < std::tie(other.one, other.two, other.three, other.four, other.five);
}
顺便说一下,使用std::multiset来计算特定元素在关联容器中存储的次数可能更简单,这将CountExample简化为一行
void CountExample(const Example& example, std::multiset<Example>& counters)
{
    counters.insert(example);
}

尽管打印变得有点棘手:

void PrintCounters(const std::multiset<Example>& counters)
{
    for(auto i=counters.begin(); i!=counters.end(); i = counters.upper_bound(*i))
            std::cout << *i << ":" << counters.count(*i) << 'n';
}

Test on ideone: http://ideone.com/uA7ao

要与多个元素进行比较,您比较的每个元素将具有三个结果:小于、大于或相等。你必须考虑到所有这些情况。

bool LessThan(const MyClass & left, const MyClass right)
{
    if (left.one < right.one)
        return true;
    else if (right.one < left.one)
        return false;
    // equivalent in one
    if (left.two < right.two)
        return true;
    else if (right.two < left.two)
        return false;
    // equivalent in one and two
        ...
    return false;
}

您需要为您的类型提供operator<。这可能是相当乏味的编写,但你可以简单地使用Boost.Tuple -这样,元组处理比较,使您的代码更容易阅读,编写和理解。

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <string>
struct Object
{
    std::string a;
    std::string b;
    std::string c;
};
bool operator<(const Object& obj1, const Object& obj2)
{
    return (boost::tie(obj1.a, obj1.b, obj1.c) < 
        boost::tie(obj2.a, obj2.b, obj2.c));
}

编辑:在对这个问题进行了更多的思考之后,我决定删除我的旧答案,因为它似乎与当前遇到的问题无关。您的operator<方法似乎确实满足了严格弱排序的要求,所以我认为问题在于其他地方,所以我在下面留下以下替代解决方案…

似乎你在为你的地图创建总顺序时遇到了问题,所以你可能想看看std::unordered_map作为一种替代方案,它将直接应用你的operator==来检测相等性,而不是使用你的operator<来进行严格的弱排序……您必须为您的类提供一个散列函数,但除此之外,基于散列表的std::unordered_map容器的使用非常直接。