1

我需要存储 Base 类型的对象,以及派生类型 BaseDerivedA 和 BaseDerivedB 的对象。这些对象需要在内存中对齐。我想提供一个迭代所有对象的迭代器。我想避免存储 Base 指针向量的内存开销。

为此,我构建了以下容器

struct Container {
    std::vector<Base> bases;
    std::vector<BaseDerivedA> derivedAs;
    std::vector<BaseDerivedB> derivedBs;

    // Iterator over the three vectors
    all_iterator<Base> all_begin(){ return all_iterator(bases[0],this); }
    all_iterator<Base> end_begin(){ return all_iterator(nullptr,this); }

    // Where all_iterator is defined as
    template < class T >
    struct all_iterator
    : public boost::iterator_facade< all_iterator<T>,
                                     T, boost::forward_traversal_tag>
    {
        all_iterator() : it_(0) {}
        explicit all_iterator(T* p, Container* c) // THIS JUST FEELS WRONG
        : it_(p), c_(c) { }

    private:
        friend class boost::iterator_core_access;
        T* it_;
        Container* c_;
        void increment() {
            if (it_ == static_cast<T*>(&(c_->bases[c_->bases.size()-1]))) {
                it_ = static_cast<T*>(&(c_->derivedAs[0]));
            } else if (it_ == static_cast<T*>(&(c_->derivedAs[ds_->derivedAs.size()-1]))) {
                it_ = static_cast<T*>(&(c_->derivedBs[0]));
            } else if (it_ == static_cast<T*>(&(c_->derivedBs[ds_->derivedBs.size()-1]))) {
                it_ = nullptr; // THIS DOES ALSO FEEL WRONG
            } else {
                ++it_;
            }
        }
        bool equal(all_iterator const& other) const {
            return this->it_ == static_cast<T*>(other.it_);
        }
        T& dereference() const { return *it_; }
    };

我使用 nullptr 作为过去的迭代器以及很多演员。我还向我的迭代器传递了一个指向数据结构的指针。

是否有更好的方法来迭代三个包含 Base 类型或从 base 派生的类型的向量?

4

4 回答 4

2

首先,我们应该注意,如果bases为空或调用任何大小的derivedBsif ,您的代码具有未定义的行为end_begin

您是否有理由不能BaseType*在单个容器中拥有或智能变体的更明显和正常的方法,并使用抽象接口而不是dynamic_cast/static_cast链来访问它?然后问题就完全消失了。

编辑:如果您出于某种原因需要每种类型的内存是连续的,并且您不insert经常将单个 s 放入容器中,只需创建一个BaseType指针容器,该容器指向派生对象容器内的每个对象。但我想请你退后一步,回顾一下为什么你需要这些对象是连续的(很可能有正当的理由)。

于 2012-07-23T14:23:25.997 回答
2

我假设 BaseType 是 DerivedA 和 DerivedB 的公共基础,并且您希望拥有一个包含 DerivedA 和 DerivedB 实例的容器,并让您能够迭代所有 DerivedA 实例、所有 DerivedB 实例以及所有实例BaseType(即 DerivedA 和 DerivedB 的并集)。你可以这样做:

class BaseType
{
public:
  virtual void doit() const = 0;

  virtual ~BaseType() { }
};

class DerivedA : public BaseType
{
public:
  void doit() const { std::cout << "DerivedA::doit()" << std::endl; }

  void a() const { std::cout << "DerivedA::a()" << std::endl; }
};

class DerivedB : public BaseType
{
public:
  void doit() const { std::cout << "DerivedB::doit()" << std::endl; }

  void b() const { std::cout << "DerivedB::b()" << std::endl; }
};

class Container
{
public:
  void insert(DerivedA const & a)
  {
    m_as.push_back(a);
    m_base.push_back(&m_as.back());
  }

  void insert(DerivedB const & b)
  {
    m_bs.push_back(b);
    m_base.push_back(&m_bs.back());
  }

  std::vector<DerivedA>::iterator begin_a() { return m_as.begin(); }
  std::vector<DerivedA>::iterator end_a() { return m_as.end(); }
  std::vector<DerivedB>::iterator begin_b() { return m_bs.begin(); }
  std::vector<DerivedB>::iterator end_b() { return m_bs.end(); }
  std::vector<BaseType *>::iterator begin_all() { return m_base.begin(); }
  std::vector<BaseType *>::iterator end_all() { return m_base.end(); }

protected:
private:
  std::vector<DerivedA> m_as;
  std::vector<DerivedB> m_bs;
  std::vector<BaseType *> m_base;
};
于 2012-07-23T14:43:26.373 回答
1

为了使您的迭代器正确,您必须知道当前正在遍历哪个向量,以便正确比较它。您可以通过枚举告诉您哪个是当前的来做到这一点:

void all_iterator::increment()
{
  switch (current_member) {
    case BasesMember:
      ++bases_iter;
      if (bases_iter==bases.end()) {
        current_member = DerivedAsMember;
      }
      return;
    case DerivedAsMember:
      ++derived_as_iter;
      if (derived_as_iter==derivedAs.end()) {
        current_member = DerivedBsMember;
      }
      return;
    case DerivedBsMember:
      ++derived_bs_iter;
      if (derived_bs_iter==derivedBs.end()) {
        current_member = EndMember;
      }
      return;
    case EndMember:
      assert(current_member!=EndMember);
      break;
  }
} 

bool all_iterator::equal(all_iterator const &other) const
{
  if (current_member!=other.current_member) return false;
  switch (current_member) {
    case BasesMember:
      return bases_iter==other.bases_iter;
      break;
    case DerivedAsMember:
      return derived_as_iter==other.derived_as_iter;
      break;
    case DerivedBsMember:
      return derived_bs_iter==other.derived_bs_iter;
      break;
    case EndMember:
      return true
  }
}

Base& all_iterator::dereference() const
{
  switch (current_member) {
    case BasesMember:     return *bases_iter;
    case DerivedAsMember: return *derived_as_iter;
    case DerivedBsMember: return *derived_bs_iter;
    case EndMember:
      assert(current_member!=EndMember);
      break;
  }
  return *bases_iter;
}
于 2012-07-23T14:19:55.267 回答
0

为什么重要的是什么derivedAs.end()?您永远不会通过derivedAs. 所以你根本不需要那个假设。

典型的代码是

for(auto it = derivedAs.begin(); it != derivedAs.end(); ++it) {
    *it = // do whatever, will never do *derivedAs.end()
}
于 2012-07-23T14:10:52.720 回答