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 から派生した型を含む 3 つのベクトルを反復処理するより良い方法はありますか?