异构类型迭代器的C ++迭代器

时间:2014-02-20 14:54:51

标签: c++ vector stl iterator standard-library

我有一个包含不同类型矢量的结构。我想创建一个迭代每个向量的每个元素的迭代器,并根据特定的规则,使用*运算符提供特定的值。我已经为迭代器的迭代器创建了一个玩具示例:

/*--------------------------------------------------------------------*/
/*  Copyright (C) Inria 2014

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Project:    MixtComp
 *  Created on: Feb 20, 2014
 *  Author:     Vincent KUBICKI <vincent.kubicki@inria.fr>
 **/

#include <iostream>
#include <iterator>
#include <vector>

class MyIterator : public std::iterator<std::input_iterator_tag, int>
{
  typedef std::vector<int>::const_iterator it   ;
  typedef std::vector<it>                  vecit;
  typedef std::vector<it>::iterator        itit ;
public:
  MyIterator(it itA ,
             it itB ,
             it endA,
             it endB)
  {
    vec_it_.push_back(itA);
    vec_it_.push_back(itB);
    vec_end_.push_back(endA);
    vec_end_.push_back(endB);

    it_vec_it_ = vec_it_.begin();
    it_vec_end_ = vec_end_.begin();
    if (itA == endA)
    {
      ++it_vec_it_;
      ++it_vec_end_;
    }
  }

  MyIterator(const MyIterator& mit) :
    vec_it_(mit.vec_it_),
    vec_end_(mit.vec_end_),
    it_vec_it_(mit.it_vec_it_),
    it_vec_end_(mit.it_vec_end_)
  {}

  MyIterator& operator++()
  {
    if (*it_vec_it_ != *it_vec_end_)
    {
      ++(*it_vec_it_);
      if (*it_vec_it_ == *it_vec_end_)
      {
        ++it_vec_it_;
        ++it_vec_end_;
      }
    }
    return *this;
  }

  MyIterator operator++(int)
  {
    MyIterator tmp(*this);
    operator++();
    return tmp;
  }

  bool operator==(const MyIterator& rhs)
  {
    return (*it_vec_it_ == *(rhs.it_vec_it_));
  }

  bool operator!=(const MyIterator& rhs)
  {
    return (*it_vec_it_ != *(rhs.it_vec_it_));
  }

  const int operator*()
  {
    return *(*it_vec_it_);
  }

private:
  vecit vec_it_; // vector of current iterators
  vecit vec_end_; // vector of end iterators

  itit it_vec_it_; // iterator on vec_it_
  itit it_vec_end_; // iterator on vec_end_
};

int main () {
  std::vector<int> dataA {1, 3, 4, 9};
  std::vector<int> dataB {11, 34, 43};

  MyIterator from(dataA.begin(),
                  dataB.begin(),
                  dataA.end(),
                  dataB.end());
  MyIterator until(dataA.end(),
                   dataB.end(),
                   dataA.end(),
                   dataB.end());

  for (MyIterator it = from; it != until; ++it)
  {
    std::cout << *it << std::endl;
  }
  return 0;
}

但是,在此实现中,dataAdataB都是相同类型的向量。假设我想要不同类型的向量,例如std::vector<std::pair<int, int>>std::vector<std::pair<int, std::vector<int>>>

是否有合理(简单)的方法来实现这一目标?

我的第一个想法是将it类型替换为BaseDerived迭代器的组合。 itit将是std::vector<Base>上的迭代器。为了比较Base迭代器,我将使用void指针来比较它们各自指向的对象的地址。 operator*()将在Derived类中定义,并且都会提供相同类型的结果,例如int

1 个答案:

答案 0 :(得分:0)

我们要做的是用itit结构替换switch / case。因此整数将跟踪“当前”it对象。