c++ - 将几个 std::list 迭代器压缩在一起

标签 c++ iterator

使用 boost 库,可以使用 zip iterator 将已知数量的迭代器压缩在一起。 ,但是如果直到运行时才知道要压缩的迭代器的数量呢?

为了扩展一点,我有一个大小相同的列表列表,我需要将每个索引处的所有值组合在一起,并将它们提供给另一个操作。现在这都是手动的,我觉得应该有更好的方法。

示例:

假设我有 3 个列表:

  • [1, 2, 3, 4, 5]
  • [11, 12, 13, 14, 15]
  • [21, 22, 23, 24, 25]

我需要将这些列表转换成:

  • [1, 11, 12]
  • [2, 12, 22]
  • [3, 13, 23]
  • [4, 14, 24]
  • ...等等

直到运行时我才知道输入中有多少列表。

最佳答案

好吧,花了将近 1/2 小时后,我想出了这个 dynamic_zip_iterator 类,它可以进一步改进,使其看起来像 STL 类迭代器。到目前为止,它非常具体,因为我已经在其中硬编码了 std::list,您可以将其替换为 std::vector 或者可以使其更加通用:

无论如何,看看它:

template<typename T>
struct dynamic_zip_iterator
{
   typedef typename std::list<T>::iterator list_iterator;
   std::list<list_iterator> iterators;
   std::list<std::list<T>> * plists;
   dynamic_zip_iterator(std::list<std::list<T>> & lists, bool isbegin) : plists(&lists) 
   {
         auto it = plists->begin();
         for( ; it != plists->end(); ++it)
         {
           if ( isbegin )
                iterators.push_back(it->begin()); 
           else
                iterators.push_back(it->end()); 
         }
   }
   dynamic_zip_iterator(const dynamic_zip_iterator & zip) : 
          plists(zip.plists),iterators(zip.iterators) {}

   dynamic_zip_iterator operator++()
   { 
     auto it = iterators.begin();
     for( ; it != iterators.end(); ++it)
          ++(*it);
     return *this;
   }
   std::list<T> operator*() 
   { 
     std::list<T> lst;
     auto it = iterators.begin();
     for( ; it != iterators.end(); ++it)
          lst.push_back(*(*it));     
     return lst;
   }
   bool operator!=(dynamic_zip_iterator &zip)
   { 
     auto it1 = iterators.begin();
     auto it2 = zip.iterators.begin();
     return (*it1) != (*it2);
   }
   static dynamic_zip_iterator begin(std::list<std::list<T>> & lists)
   {
      return dynamic_zip_iterator<T>(lists, true);
   }
   static dynamic_zip_iterator end(std::list<std::list<T>> & lists)
   {
      return dynamic_zip_iterator<T>(lists, false);
   }
};

使用它你的问题可以简化为这个函数:

std::list<std::list<int>> create_lists(std::list<std::list<int>>& lists)
{
  std::list<std::list<int>> results;
  auto begin = dynamic_zip_iterator<int>::begin(lists);
  auto end = dynamic_zip_iterator<int>::end(lists);
  for( ; begin != end ; ++begin)
  {
     results.push_back(*begin);
  }
  return results;    
}

测试代码:

int main() {
        int a[] = {1, 2, 3, 4, 5}, b[] = {11, 12, 13, 14, 15}, c[] = {21, 22, 23, 24, 25};
        std::list<int> l1(a,a+5), l2(b,b+5), l3(c,c+5);
        std::list<std::list<int>> lists;
        lists.push_back(l1); 
        lists.push_back(l2);
        lists.push_back(l3);
        std::list<std::list<int>> newlists = create_lists(lists);
        for(auto lst = newlists.begin(); lst != newlists.end(); ++lst)
        {
                std::cout << "[";
                std::copy(lst->begin(), lst->end(), std::ostream_iterator<int>(std::cout, " "));
                std::cout << "]" << std::endl;
        }
        return 0;
}

输出:

[1 11 21 ]
[2 12 22 ]
[3 13 23 ]
[4 14 24 ]
[5 15 25 ]

在线演示:http://ideone.com/3FJu1

关于c++ - 将几个 std::list 迭代器压缩在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404526/

相关文章:

c++ - 将相邻的子串分组并找到计数

c++ - 创建一个连续的动态矩阵

c++ - 通过子方法 C++ 从父项设置值

java - 如何在另一个调用中正确返回 token ?

java - 尝试使用 IntStream.range() 并获取 "This static method can only be accessed..."

c++ - 使用 map 将多行 if 语句转换为单行

C++:链表表示

c++ - 指向索引处 vector 的指针与迭代器

java - 可迭代是如何工作的?

python - 遍历 Python 中的未知对象属性