STL std :: map动态排序 [英] STL std::map dynamic ordering

查看:103
本文介绍了STL std :: map动态排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个愚蠢的问题。但stil我有一个混乱。
W.r.t std :: map。我为地图的dymanic排序写了一个自定义谓词,

I know this may be a silly question. but stil i have a confusion. W.r.t std::map. i have written a custom predicate for dymanic ordering of map,

enum OrderingType 
{
    ASCENDING, 
    DESCENDING 
};

template <class T>
class Ordering
{
    OrderingType m_order;

public:
    Ordering(OrderingType order) : m_order(order) { }

    bool operator() (const T &obj1, const T &obj2)
    {
        if( m_order == ASCENDING )
            return obj1 < obj2;

        if( m_order == DESCENDING )
            return obj1 > obj2;
    } 
};

优势是


  1. 我们可以在某些条件下决定地图中数据元素的排序

  1. We can decide the ordering of data elements in the map on some conditions

OrderType type =(condition?ASCENDING:DESCENDING);
CUSTOMMAP m(type);

OrderType type = (condition ? ASCENDING : DESCENDING ); CUSTOMMAP m(type);

我们可以使用相同的正向迭代器,在以下代码中按降序排序的映射

We can use same forward iterator for both ascending & descending ordered map

。地图的排序工作正常在升序&降序(amp1& map2)。但是在赋值map2 = map1时,map2的顺序与内容一起改变。我期望只复制内容,而不是改变排序。

in the below code. map's ordering works fine in both ascending & descending order ( amp1 & map2 ). But on assignment map2 = map1, the ordering of map2 changes along with the content. I was expected to copy only content, not the change in the ordering. further inserts on map2 ( which was declared as descending) will be in ascending order.

任何建议或任何建议理念..?或定义映射的双向排序谓词是坏主意。

typedef map<int, int, Ordering<int> >  CUSTOMMAP;
typedef CUSTOMMAP::iterator       CUSTOMMAP_ITER;
typedef CUSTOMMAP::const_iterator CUSTOMMAP_CONST_ITER;

ostream& operator <<(ostream& out, const CUSTOMMAP& mapobj)
{
    CUSTOMMAP_CONST_ITER citer = mapobj.begin();
    for( ; citer != mapobj.end(); ++citer )
    {
        out << citer->first << "   " << citer->second << endl;
    } 
    out << "==========" << endl;
    return out;
}

int main()
{
    CUSTOMMAP map1(ASCENDING);     //instantiate a map with ascending sorting
    CUSTOMMAP map2(DESCENDING);    //instantiate a map with descending sorting

    map1.insert( make_pair(1, 0));
    map1.insert( make_pair(2, 0));
    cout << map1;                  // prints data in ascnding manner 

    map2.insert( make_pair(5, 0));
    map2.insert( make_pair(6, 0));
    cout << map2;                  // prints data in descending manner 

    map2 = map1;

    cout << map2;                  //copys contents of map1 to map2 & changes 
                                   //map2's ordering predicate
    return 0;
}


推荐答案

c> map2 = map1 实际上是复制整个对象,而不仅仅是元素。您可能想要做的是

Well setting map2 = map1 is going to in fact copy the whole object, not just the elements. What you probably want to do then is

map2.clear();
map2.insert(map1.begin(), map1.end());

我相信我的头脑,两个步骤的复杂性将是 O(n log n),但不要引用我。

I believe off the top of my head that the complexity of the two steps together is going to be O(n log n), but don't quote me on that.

>

Edit

更好( O(n)):

map2.clear();
map2.insert(map1.rbegin(), map1.rend());

对于第三个版本(insert(first,last)),Nlog一般(其中N是第一个和最后一个之间的距离,并且大小插入之前的容器的大小),但是如果第一和最后之间的元素已经根据容器使用的相同排序标准排序,则是线性的。 (http://cplusplus.com/reference/stl/map/insert)

"For the third version ( insert (first,last) ), Nlog(size+N) in general (where N is the distance between first and last, and size the size of the container before the insertion), but linear if the elements between first and last are already sorted according to the same ordering criterion used by the container." (http://cplusplus.com/reference/stl/map/insert)

这篇关于STL std :: map动态排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆