Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 支持优先级、更新、推送和pop的最佳数据结构是什么_C++_Algorithm_C++11_Data Structures - Fatal编程技术网

C++ 支持优先级、更新、推送和pop的最佳数据结构是什么

C++ 支持优先级、更新、推送和pop的最佳数据结构是什么,c++,algorithm,c++11,data-structures,C++,Algorithm,C++11,Data Structures,撰写申请书: 其中,我需要维护一个数据结构“X-Files”,该结构存储大量对象并支持以下功能: Pop-删除值最小的对象(名为-'Area51'的对象属性之一) Push-将对象插入到数据结构中 检查-给定对象是否在数据结构“X-Files”中 更新-更新对象的内容(包括更新属性“Area51”及其保证的更新值将严格低于当前值) 支持这些需求最合适的数据结构是什么 A应该做这项工作 虽然您的问题可能类似于使用std::priority_队列,但不要落入陷阱,因为普通的priority_队列不支

撰写申请书: 其中,我需要维护一个数据结构“X-Files”,该结构存储大量对象并支持以下功能:

  • Pop-删除值最小的对象(名为-'Area51'的对象属性之一)
  • Push-将对象插入到数据结构中
  • 检查-给定对象是否在数据结构“X-Files”中
  • 更新-更新对象的内容(包括更新属性“Area51”及其保证的更新值将严格低于当前值)
  • 支持这些需求最合适的数据结构是什么

    A应该做这项工作


    虽然您的问题可能类似于使用
    std::priority_队列
    ,但不要落入陷阱,因为普通的
    priority_队列
    不支持更新现有元素(头部除外),也不能有效地检查元素是否存在。

    如果优先级和键是不同的属性,则可以使用2索引。快速和肮脏的例子如下:

    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/ordered_index.hpp>
    #include <boost/multi_index/hashed_index.hpp>
    #include <boost/multi_index/member.hpp>
    
    using namespace boost::multi_index;
    
    struct element
    {
     int key;
     int area51;
    };
    
    using x_files_base=multi_index_container<
      element,
      indexed_by<
        ordered_non_unique<member<element,int,&element::area51>>,
        hashed_unique<member<element,int,&element::key>>
      >
    >;
    
    class x_files:x_files_base
    {
    public:
      using x_files_base::iterator;
      using x_files_base::const_iterator;
    
      using x_files_base::x_files_base;
      using x_files_base::begin;
      using x_files_base::end;
    
      void pop(){erase(begin());}
      bool push(const element& x){return insert(x).second;}
      bool check(int key){return project<0>(get<1>().find(key))!=end();}
    
      void update(int key,int area51)
      {
        auto it=project<0>(get<1>().find(key));
        if(it!=end())modify(it,[=](element& x){x.area51=area51;});
      }
    };
    
    #include <iostream>
    #include <string>
    
    void dump(const x_files& xf)
    {
      std::string delim="";
      for(const element& x:xf){
        std::cout<<delim<<"["<<x.key<<","<<x.area51<<"]";
        delim=",";
      }
      std::cout<<"\n";
    }
    
    int main()
    {
      x_files xf={{100,0},{80,1},{90,2},{95,3}};
    
      dump(xf);
      xf.pop();
      dump(xf);
      xf.push({70,4});
      dump(xf);
      std::cout<<(xf.check(70)?"true":"false")<<"\n";
      xf.update(70,0);
      dump(xf);
    }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间boost::multi_索引;
    结构元素
    {
    int键;
    国际区域51;
    };
    使用x_文件\u基=多索引\u容器<
    元素,
    索引<
    有序非唯一,
    哈希_唯一
    >
    >;
    类x_文件:x_文件\u库
    {
    公众:
    使用x_文件\u base::迭代器;
    使用x_文件\u base::const_迭代器;
    使用x_文件\u库::x_文件\u库;
    使用x_文件\u base::begin;
    使用x_文件\u base::end;
    void pop(){erase(begin());}
    bool-push(const-element&x){返回insert(x).second;}
    bool check(int key){返回项目(get().find(key))!=end();}
    无效更新(int键,int区域51)
    {
    autoit=project(get().find(key));
    如果(it!=end())修改(it,[=](元素&x){x.area51=area51;});
    }
    };
    #包括
    #包括
    无效转储(常量x_文件和xf)
    {
    std::string delim=“”;
    用于(常量元素&x:xf){
    
    std::我认为堆栈数据结构可以达到这个目的。@A2H:如何检查堆栈中所有现存的值?我在查阅std::set的文档。对我来说,问题是,键与优先级为的属性不同。下一个是std::set不允许重复键。我可能不知道如何使用。我希望您能详细说明一下。@skn2我不确定我是否在遵循。只需确保您的
    比较
    (第二个模板参数)for
    std::set
    正在按元素的优先级进行检查。如果要允许具有相同优先级的多个元素,请确保它根据对象的其他属性断开连接。但是,
    std::set
    也不支持修改键。更新必须作为删除更新插入序列进行。@Angew非常容易实现,而且非常有效,如果你也使用
    emplace\u hint()
    。OP没有提到boost。没有提到他不能使用它,either@JoaquínMLópezMuñoz:太棒了。谢谢。我没有安装boost,但现在这迫使我安装它。 [100,0],[80,1],[90,2],[95,3] [80,1],[90,2],[95,3] [80,1],[90,2],[95,3],[70,4] true [70,0],[80,1],[90,2],[95,3]