在C ++中为数百万个对象创建一个映射

时间:2015-03-19 05:07:59

标签: c++ data-structures

我有一个名为Object的抽象类,它有一些虚函数,其中一个函数将检索id的{​​{1}}。

目前,我使用Object来存储数千万个这些对象。不幸的是,添加,复制或删除它是非常缓慢的。

我想创建一个可能以std::vector<Object>为键的哈希映射,也可能将对象本身作为值?或者是否有某种类型的数据结构可以像Object->id那样轻松插入和删除,但对于数千万个对象来说会更快?

我希望课程最终看起来像这个大纲:

std::vector

处理大量这些对象有什么特别快的,或者是否有任何可能使用来自对象的无符号整数id处理数据的速度非常快?

此外,我的类将被预先排序,因此每个对象在被添加到容器之前将按ID排序。然后我会通过ID对数据进行二进制搜索。

2 个答案:

答案 0 :(得分:2)

您可能可以使用std::set(如果id - s有一些订单并且对于它是唯一的)或std::unordered_set我建议您将其作为容器的一个组件,不从中派生你的容器。您最好只使用Object来构建本地假冒id ...

class Object {
   friend class BigContainer;
   unsigned long _id;
   // other fields;
   // your constructors
  public:
    unsigned long id() const { return _id; };
  private:
   Object(unsigned long pseudoid); // construct a fake object
};

struct LessById {
  bool operator () (const Object &ob1, const Object& ob2) 
    { return ob1.id() < ob2.id(); };
  bool operator () (const Object &ob, unsigned long idl)
    { return ob1.id() < idl;
};

class BigContainer {
   std::set<Object,LessById> set;
public:
   // add members, constructors, destructors, etc...
   bool contains(unsigned long id) const {
     Object fakeobj{id};
     if (set.find(fakeobj) != set.end()) return true;
     return false;
   };
   const Object* find_by_id(unsigned long id) const {
     Object fakeobj{id};
     auto p = set.find(fakeobj);
     if (p != set.end()) return &(*p);
     return nullptr;
   };

   bool contains(const Object& ob) const {
      if (set.find(ob) != set.end()) return true;
      return false;
   };
   void add(const Object&ob) const {
     Object fakeobj{id};
     auto p = set.find(fakeobj);
     if (p == set.end()) set.insert(ob);
   }
   void remove(unsigned long id) const {
     Object fakeobj{id};
     auto p = set.find(fakeobj);
     if (p != set.end()) set.erase(p);
   }
};

如果你想要一组指针,请使用一组smart pointers并调整上面的方案。

如果Object很大并且您无法定义为给定I​​D有效构建本地虚假对象的方法,请定义一个超级struct BoxedId { unsigned long id; BoxedId(unsigned long l): id(l) {}; },在内部声明std::set<std::shared_ptr<BoxedId>,BoxedLessById> make { {1}}等等......

顺便说一下,由于class Object : public BoxedIdObject方法,你可能会将它子类化,你需要有一组指针。您需要定义指针策略(是virtualObject - 的子类的每个实际实例)并使用一些智能指针....您需要定义谁负责Container - 你的delete - s(谁拥有指针)。是唯一的Object

阅读C++11 rule of five

答案 1 :(得分:1)

请查看此网站:http://www.cs.northwestern.edu/~riesbeck/programming/c++/stl-summary.html

它显示了每个STL的每个操作的时间复杂度。

首先明确您的要求,然后通过比较上面链接中显示的时间复杂度,明智地选择特定的STL。

相关问题