如何更新C ++的指针集? [英] How to update set of pointers c++?

查看:97
本文介绍了如何更新C ++的指针集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一组TVertex对象才能通过不同的参数(id和flow)访问它们的排序.在O(1)时间中找到第k个元素,并在o(log2(N))时间中更新它们的属性.

I need a group of TVertex objects to have an access to their sortings by diffrent parameters (id and flow). To find the kth element in O(1) time and to update their properties in o(log2(N)) time.

为此,我使用TComparatorF和TComparatorI类.

To do this I use TComparatorF and TComparatorI classes.

它们都具有指向TVertex的指针,并且具有运算符>和<

They both have a pointer to TVertex and operators > and <

TComparatorF比较流

TComparatorF compares flows

TComparatorI比较ID

TComparatorI compares ids

TVertex()将指向自身的指针放到比较器中.然后将比较器放在一组比较器中.

TVertex() puts a pointer to itself to a comparator. And then puts the comparator to a set of comparators.

但是当我更新TVertex对象的属性时,相对于它的比较器的位置不会改变.

But when I update properties of TVertex object, position of relative to it comparator doesn't change.

是否有一种方法可以强制我的集合进行更新,或者有更好的主意来存储多个排序?

Is there a way to force my sets to update or a better idea for storing multiple sortings?

代码:

#include<stdio.h>
#include<set>
#include<stdlib.h>
using namespace std;
const int N=300000;

struct TVertex;
struct TComparatorF
{
    TVertex *cmp;
    bool operator >(const TComparatorF &other) const;
    bool operator <(const TComparatorF &other) const;  
};
struct TComparatorI
{
    TVertex *cmp;
    bool operator >(const TComparatorI &other) const;
    bool operator <(const TComparatorI &other) const;  
};
set <TComparatorF> flowset;
set <TComparatorI> idset;
struct TVertex
{
    int flow,id;  
    TVertex();
};
bool TComparatorF::operator >(const TComparatorF &other) const
{
    return !(*cmp).flow>(*(other.cmp)).flow;   
}
bool TComparatorF::operator <(const TComparatorF &other) const
{
    return !(*cmp).flow<(*(other.cmp)).flow;
}
bool TComparatorI::operator >(const TComparatorI &other) const
{
    return !(*cmp).id>(*(other.cmp)).id;   
}
bool TComparatorI::operator <(const TComparatorI &other) const
{
    return !(*cmp).id<(*(other.cmp)).id;
}
TVertex::TVertex()
{
    flow=0;
    TComparatorF comp;
    comp.cmp=this;    
    flowset.insert(comp);
    TComparatorI comp2;
    comp2.cmp=this;
    idset.insert(comp2);
}
TVertex ver[N];

int main()
{
    ver[0].flow=17;
    ver[0].id=6;
    ver[1].flow=100;
    ver[1].id=5;
    ver[2].flow=5798;
    ver[2].id=40;
    TComparatorF comp=*(flowset.begin());
    TComparatorI comp2=*(idset.begin());
    printf("%d %d\n",(*(comp.cmp)).flow,(*(comp2.cmp)).id);
    system("PAUSE");
    return 0;
}

我知道

17 6

17 6

代替

5798 40

5798 40

推荐答案

回答问题是否有一种方法可以强制我的集合进行更新,或者有更好的主意来存储多种排序?"

Answering the question "Is there a way to force my sets to update or a better idea for storing multiple sortings?"

容器不会随着条目的更改而更新自身,这就是为什么set条目和map键保持不变的原因.您可以包装手动完成(删除/插入)更新的集合,也可以使用增强

Containers don't update themselves upon changes of their entries, that is why set entries and map keys are constant. You could make a wrapper of the sets where the update is done manually (remove/insert) or you could use boosts multi index containers which have this already (see the modifiers)

这篇关于如何更新C ++的指针集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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