c ++ 11的序列zip函数? [英] Sequence-zip function for c++11?

查看:167
本文介绍了c ++ 11的序列zip函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的基于范围的for循环,我们可以为(auto x:Y){} $编写代码

  b $ b 

哪个IMO是巨大的改进

  for(std :: vector< int> iter iterator x = Y.begin(); x!= Y.end ++ x){} 

可以用于循环两个同时循环,如Pythons zip function?对于不熟悉Python的人,代码如下:

  Y1 = [1,2,3] 
Y2 = [4 ,5,6,7]
for x1,x2 in zip(Y1,Y2):
print x1,x2

提供输出(1,4)(2,5)(3,6)


<如果输入容器的长度不一样,boost :: zip_iterator的Boost 1.48.0会导致未定义的行为。如果输入容器的长度不一样,boost :: zip_iterator会导致未定义的行为。 (可能会崩溃或重复超过结束)。






code>:

  #include< boost / iterator / zip_iterator.hpp> 
#include< boost / range.hpp>

template< typename ... T>
auto zip(const T& ... container) - > boost :: iterator_range< boost :: zip_iterator< decltype(boost :: make_tuple(std :: begin(containers)...))>>
{
auto zip_begin = boost :: make_zip_iterator(boost :: make_tuple(std :: begin(containers)...));
auto zip_end = boost :: make_zip_iterator(boost :: make_tuple(std :: end(containers)...));
return boost :: make_iterator_range(zip_begin,zip_end);
}

示例:

  #include< cstdio> 
#include< vector>
#include< list>

int main()
{
std :: vector< int> a {4,5,6};
double b [] = {7,8,9};
std :: list< std :: string> c {a,b,c};
for(auto tup:zip(a,b,c,a))
{
int x,w;
double y;
std :: string z;
boost :: tie(x,y,z,w)= tup;
printf(%d%g%s%d \\\
,x,y,z.c_str(),w);
}
}

这将打印

 
4 7 a 4
5 8 b 5
6 9 c 6


With the new range-based for loop we can write code like

for(auto x: Y) {}

Which IMO is a huge improvement from (for ex.)

for(std::vector<int>::iterator x=Y.begin(); x!=Y.end(); ++x) {}

Can it be used to loop over two simultaneous loops, like Pythons zip function? For those unfamiliar with Python, the code:

Y1 = [1,2,3]
Y2 = [4,5,6,7]
for x1,x2 in zip(Y1,Y2):
    print x1,x2

Gives as output (1,4) (2,5) (3,6)

解决方案

Warning: boost::zip_iterator as of Boost 1.48.0 will cause undefined behavior if the length of the input containers are not the same (it may crash or iterate beyond the end).


A variadic version of zip:

#include <boost/iterator/zip_iterator.hpp>
#include <boost/range.hpp>

template <typename... T>
auto zip(const T&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>>
{
    auto zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
    auto zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
    return boost::make_iterator_range(zip_begin, zip_end);
}

Example:

#include <cstdio>
#include <vector>
#include <list>

int main()
{
    std::vector<int> a {4, 5, 6};
    double b[] = {7, 8, 9};
    std::list<std::string> c {"a", "b", "c"};
    for (auto tup : zip(a, b, c, a))
    {
        int x, w;
        double y;
        std::string z;
        boost::tie(x, y, z, w) = tup;
        printf("%d %g %s %d\n", x, y, z.c_str(), w);
    }
}

This would print

4 7 a 4
5 8 b 5
6 9 c 6

这篇关于c ++ 11的序列zip函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆