一起压缩几个std :: list迭代器 [英] Zip several std::list iterators together
问题描述
Using the boost library it is possible to zip together a known number of iterators using a zip iterator, but what about when the number of iterators to be zipped is not known until runtime?
要稍微扩展一下,我有一个大小相同的列表列表,我需要将每个索引处的所有值组合在一起,然后将它们输入另一个操作中.现在这都是手动的,我觉得应该有更好的方法.
To expand a little bit, I have a list of lists that are all the same size, and I need to group together all the values at each each index and feed them into another operation. Right now this is all manual, and I feel like there should be a better way.
示例:
说我有3个列表:
- [1、2、3、4、5]
- [11、12、13、14、15]
- [21,22,23,24,25]
我需要将这些列表转换为:
I need to transform these lists into:
- [1,11,12]
- [2,12,22]
- [3,13,23]
- [4,14,24]
- ...等等
在运行之前,我不知道输入中有多少个列表.
I do not know how many lists are in the input until runtime.
推荐答案
花费了近1/2个小时之后,我想到了可以进一步改进的dynamic_zip_iterator
类,使其看起来像类似于STL的迭代器.到目前为止,它非常具体,因为我已经在其中硬编码了std::list
,您可以将其替换为std::vector
,或者可以使其更通用:
Alright after spending almost 1/2 hour, I came up with this dynamic_zip_iterator
class which can be further improved, to make it look like STL-like iterators. As of now, it's very specific, as I've hardcoded std::list
in it which you can replace with std::vector
or can make even more generic:
无论如何,看看它:
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);
}
};
使用它,您的问题将减少到此功能:
Using it your problem reduces to this function:
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
这篇关于一起压缩几个std :: list迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!