序列压缩函数用于c++ 11

Sequence-zip function for C++11?

本文关键字:c++ 用于 压缩函数      更新时间:2023-10-16

使用新的基于范围的for循环,我们可以编写如下代码:

for(auto x: Y) {}

在我看来,这是一个巨大的改进(例如)

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

它可以用来在两个同时循环,像Python的zip函数?对于那些不熟悉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_iteratorboost::combine作为Boost 1.63.0(2016年12月26日)将导致未定义的行为,如果输入容器的长度不相同(它可能崩溃或迭代超过结束)。


从Boost 1.56.0(2014年8月7日)开始,您可以使用boost::combine(该功能在早期版本中存在,但未记录):

#include <boost/range/combine.hpp>
#include <vector>
#include <list>
#include <string>
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 : boost::combine(a, b, c, a)) {    // <---
        int x, w;
        double y;
        std::string z;
        boost::tie(x, y, z, w) = tup;
        printf("%d %g %s %dn", x, y, z.c_str(), w);
    }
}

输出

<>之前4 7 a 45 8 b 56 9 c 6之前

在早期版本中,您可以自己定义一个范围,如下所示:

#include <boost/iterator/zip_iterator.hpp>
#include <boost/range.hpp>
template <typename... T>
auto zip(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);
}

用法相同。

std::transform可以简单地执行此操作:

std::vector<int> a = {1,2,3,4,5};
std::vector<int> b = {1,2,3,4,5};
std::vector<int>c;
std::transform(a.begin(),a.end(), b.begin(),
               std::back_inserter(c),
               [](const auto& aa, const auto& bb)
               {
                   return aa*bb;
               });
for(auto cc:c)
    std::cout<<cc<<std::endl;

如果第二个序列较短,我的实现似乎给出默认初始化值。

所以当我无聊的时候我写了这个zip,我决定发布它,因为它不同于其他的,它不使用boost,看起来更像c++标准库。

template <typename Iterator>
    void advance_all (Iterator & iterator) {
        ++iterator;
    }
template <typename Iterator, typename ... Iterators>
    void advance_all (Iterator & iterator, Iterators& ... iterators) {
        ++iterator;
        advance_all(iterators...);
    } 
template <typename Function, typename Iterator, typename ... Iterators>
    Function zip (Function func, Iterator begin, 
            Iterator end, 
            Iterators ... iterators)
    {
        for(;begin != end; ++begin, advance_all(iterators...))
            func(*begin, *(iterators)... );
        //could also make this a tuple
        return func;
    }

示例:

int main () {
    std::vector<int> v1{1,2,3};
    std::vector<int> v2{3,2,1};
    std::vector<float> v3{1.2,2.4,9.0};
    std::vector<float> v4{1.2,2.4,9.0};
     zip (
            [](int i,int j,float k,float l){
                std::cout << i << " " << j << " " << k << " " << l << std::endl;
            },
            v1.begin(),v1.end(),v2.begin(),v3.begin(),v4.begin());
}

range-v3:

#include <range/v3/all.hpp>
#include <vector>
#include <iostream>
namespace ranges {
    template <class T, class U>
    std::ostream& operator << (std::ostream& os, common_pair<T, U> const& p)
    {
      return os << '(' << p.first << ", " << p.second << ')';
    }
}
using namespace ranges::v3;
int main()
{
    std::vector<int> a {4, 5, 6};
    double b[] = {7, 8, 9};
    std::cout << view::zip(a, b) << std::endl; 
}

输出:

[(4,7),(5,8),(6,9)]

参见<redi/zip.h>zip函数,该函数与基于范围的for一起工作,并接受任意数量的范围,可以是右值或左值,可以是不同的长度(迭代将在最短范围结束时停止)。

std::vector<int> vi{ 0, 2, 4 };
std::vector<std::string> vs{ "1", "3", "5", "7" };
for (auto i : redi::zip(vi, vs))
  std::cout << i.get<0>() << ' ' << i.get<1>() << ' ';

打印0 1 2 3 4 5

您可以使用基于boost::zip_iterator的解决方案。创建一个伪容器类,维护对容器的引用,并从beginend成员函数返回zip_iterator。现在你可以写

for (auto p: zip(c1, c2)) { ... }

示例实现(请测试):

#include <iterator>
#include <boost/iterator/zip_iterator.hpp>
template <typename C1, typename C2>
class zip_container
{
    C1* c1; C2* c2;
    typedef boost::tuple<
        decltype(std::begin(*c1)), 
        decltype(std::begin(*c2))
    > tuple;
public:
    zip_container(C1& c1, C2& c2) : c1(&c1), c2(&c2) {}
    typedef boost::zip_iterator<tuple> iterator;
    iterator begin() const
    {
         return iterator(std::begin(*c1), std::begin(*c2));
    }
    iterator end() const
    {
         return iterator(std::end(*c1), std::end(*c2));
    }
};
template <typename C1, typename C2>
zip_container<C1, C2> zip(C1& c1, C2& c2)
{
    return zip_container<C1, C2>(c1, c2);
}

我把可变的版本留给读者作为一个很好的练习。

如果您喜欢操作符重载,这里有三种可能性。前两个分别使用std::pair<>std::tuple<>作为迭代器;第三个扩展到基于范围的for。请注意,并非所有人都喜欢这些操作符的定义,因此最好将它们保存在单独的名称空间中,并在希望使用这些操作符的函数(而不是文件!)中设置using namespace

#include <iostream>
#include <utility>
#include <vector>
#include <tuple>
// put these in namespaces so we don't pollute global
namespace pair_iterators
{
    template<typename T1, typename T2>
    std::pair<T1, T2> operator++(std::pair<T1, T2>& it)
    {
        ++it.first;
        ++it.second;
        return it;
    }
}
namespace tuple_iterators
{
    // you might want to make this generic (via param pack)
    template<typename T1, typename T2, typename T3>
    auto operator++(std::tuple<T1, T2, T3>& it)
    {
        ++( std::get<0>( it ) );
        ++( std::get<1>( it ) );
        ++( std::get<2>( it ) );
        return it;
    }
    template<typename T1, typename T2, typename T3>
    auto operator*(const std::tuple<T1, T2, T3>& it)
    {
        return std::tie( *( std::get<0>( it ) ),
                         *( std::get<1>( it ) ),
                         *( std::get<2>( it ) ) );
    }
    // needed due to ADL-only lookup
    template<typename... Args>
    struct tuple_c
    {
        std::tuple<Args...> containers;
    };
    template<typename... Args>
    auto tie_c( const Args&... args )
    {
        tuple_c<Args...> ret = { std::tie(args...) };
        return ret;
    }
    template<typename T1, typename T2, typename T3>
    auto begin( const tuple_c<T1, T2, T3>& c )
    {
        return std::make_tuple( std::get<0>( c.containers ).begin(),
                                std::get<1>( c.containers ).begin(),
                                std::get<2>( c.containers ).begin() );
    }
    template<typename T1, typename T2, typename T3>
    auto end( const tuple_c<T1, T2, T3>& c )
    {
        return std::make_tuple( std::get<0>( c.containers ).end(),
                                std::get<1>( c.containers ).end(),
                                std::get<2>( c.containers ).end() );
    }
    // implement cbegin(), cend() as needed
}
int main()
{
    using namespace pair_iterators;
    using namespace tuple_iterators;
    std::vector<double> ds = { 0.0, 0.1, 0.2 };
    std::vector<int   > is = {   1,   2,   3 };
    std::vector<char  > cs = { 'a', 'b', 'c' };
    // classical, iterator-style using pairs
    for( auto its  = std::make_pair(ds.begin(), is.begin()),
              end  = std::make_pair(ds.end(),   is.end()  ); its != end; ++its )
    {
        std::cout << "1. " << *(its.first ) + *(its.second) << " " << std::endl;
    }
    // classical, iterator-style using tuples
    for( auto its  = std::make_tuple(ds.begin(), is.begin(), cs.begin()),
              end  = std::make_tuple(ds.end(),   is.end(),   cs.end()  ); its != end; ++its )
    {
        std::cout << "2. " << *(std::get<0>(its)) + *(std::get<1>(its)) << " "
                           << *(std::get<2>(its)) << " " << std::endl;
    }
    // range for using tuples
    for( const auto& d_i_c : tie_c( ds, is, cs ) )
    {
        std::cout << "3. " << std::get<0>(d_i_c) + std::get<1>(d_i_c) << " "
                           << std::get<2>(d_i_c) << " " << std::endl;
    }
}
// declare a, b
BOOST_FOREACH(boost::tie(a, b), boost::combine(list_of_a, list_of_b)){
    // your code here.
}

我独立地遇到了同样的问题,并且不喜欢上面任何一个的语法。因此,我有一个简短的头文件,其功能与boost zip_iterator相同,但有一些宏,使语法更容易理解:

https://github.com/cshelton/zipfor

例如

vector<int> a {1,2,3};
array<string,3> b {"hello","there","coders"};
zipfor(i,s eachin a,b)
    cout << i << " => " << s << endl;

主要的语法优点是我可以命名每个容器中的元素。我还包含了一个"mapfor",它做同样的事情,但对于映射(命名为"。首先是"answers"。第二个"元素")。

如果你有一个c++ 14兼容的编译器(例如gcc5),你可以使用Ryan Haining在cppitertools库中提供的zip,这看起来真的很有前途:

array<int,4> i{{1,2,3,4}};
vector<float> f{1.2,1.4,12.3,4.5,9.9};
vector<string> s{"i","like","apples","alot","dude"};
array<double,5> d{{1.2,1.2,1.2,1.2,1.2}};
for (auto&& e : zip(i,f,s,d)) {
    cout << std::get<0>(e) << ' '
         << std::get<1>(e) << ' '
         << std::get<2>(e) << ' '
         << std::get<3>(e) << 'n';
    std::get<1>(e)=2.2f; // modifies the underlying 'f' array
}

c++ 23,我们可以迭代std::views::zip。下面是一个简单的例子:

#include <iostream>
#include <ranges>
#include <vector>
 
int main() {
    std::vector<int> x {4, 5, 6};
    double y[] = {7, 8, 9};
    for (auto [elem1,elem2] : std::views::zip(x, y))        
        std::cout << "[" << elem1 << "," << elem2 << "]" << " ";
}

可以在下面验证输出(一个在线编译器)。不确定链接存在多少天

https://godbolt.org/z/KjjE4eeGY

对于我正在编写的c++流处理库,我正在寻找一种不依赖第三方库并与任意数量的容器一起工作的解决方案。我得到了这个解。它类似于使用boost的公认解决方案(如果容器长度不相等,也会导致未定义的行为)

#include <utility>
namespace impl {
template <typename Iter, typename... Iters>
class zip_iterator {
public:
  using value_type = std::tuple<const typename Iter::value_type&,
                                const typename Iters::value_type&...>;
  zip_iterator(const Iter &head, const Iters&... tail)
      : head_(head), tail_(tail...) { }
  value_type operator*() const {
    return std::tuple_cat(std::tuple<const typename Iter::value_type&>(*head_), *tail_);
  }
  zip_iterator& operator++() {
    ++head_; ++tail_;
    return *this;
  }
  bool operator==(const zip_iterator &rhs) const {
    return head_ == rhs.head_ && tail_ == rhs.tail_;
  }
  bool operator!=(const zip_iterator &rhs) const {
    return !(*this == rhs);
  }
private:
  Iter head_;
  zip_iterator<Iters...> tail_;
};
template <typename Iter>
class zip_iterator<Iter> {
public:
  using value_type = std::tuple<const typename Iter::value_type&>;
  zip_iterator(const Iter &head) : head_(head) { }
  value_type operator*() const {
    return value_type(*head_);
  }
  zip_iterator& operator++() { ++head_; return *this; }
  bool operator==(const zip_iterator &rhs) const { return head_ == rhs.head_; }
  bool operator!=(const zip_iterator &rhs) const { return !(*this == rhs); }
private:
  Iter head_;
};
}  // namespace impl
template <typename Iter>
class seq {
public:
  using iterator = Iter;
  seq(const Iter &begin, const Iter &end) : begin_(begin), end_(end) { }
  iterator begin() const { return begin_; }
  iterator end() const { return end_; }
private:
  Iter begin_, end_;
};
/* WARNING: Undefined behavior if iterator lengths are different.
 */
template <typename... Seqs>
seq<impl::zip_iterator<typename Seqs::iterator...>>
zip(const Seqs&... seqs) {
  return seq<impl::zip_iterator<typename Seqs::iterator...>>(
      impl::zip_iterator<typename Seqs::iterator...>(std::begin(seqs)...),
      impl::zip_iterator<typename Seqs::iterator...>(std::end(seqs)...));
}

对aaronman解的改进:

  • 仍然c++ 11。
  • 没有递归的模板展开。
  • 支持容器压缩
  • 采用了Sean Parent著名的for_each_arg()方法。
// Includes only required for the example main() below!
#include <vector>
#include <iostream>
namespace detail {
struct advance {
    template <typename T> void operator()(T& t) const { ++t; }
};
// Adaptation of for_each_arg, see:
// https://isocpp.org/blog/2015/01/for-each-argument-sean-parent
template <class... Iterators>
void advance_all(Iterators&... iterators) {
    [](...){}((advance{}(iterators), 0)...);
}
} // namespace detail
template <typename F, typename Iterator, typename ... ExtraIterators>
F for_each_zipped(
    F func, 
    Iterator begin, 
    Iterator end, 
    ExtraIterators ... extra_iterators)
{
    for(;begin != end; ++begin, detail::advance_all(extra_iterators...))
        func(*begin, *(extra_iterators)... );
    return func;
}
template <typename F, typename Container, typename... ExtraContainers>
F for_each_zipped_containers(
    F func,
    Container& container, 
    ExtraContainers& ... extra_containers)
{
    return for_each_zipped(
        func, std::begin(container), std::end(container), std::begin(extra_containers)...);
}
int main () {
    std::vector<int>   v1 {  1,   2,   3};
    std::vector<int>   v2 {  3,   2,   1};
    std::vector<float> v3 {1.2, 2.4, 9.0};
    std::vector<float> v4 {1.2, 2.4, 9.0};
    auto print_quartet = 
        [](int i,int j,float k,float l) {
            std::cout << i << " " << j << " " << k << " " << l << 'n';
        };
    std::cout << "Using zipped iterators:n";
    for_each_zipped(print_quartet, v1.begin(), v1.end(), v2.begin(), v3.begin(), v4.begin());
    std::cout << "nUsing zipped containers:n";
    for_each_zipped_containers(print_quartet, v1, v2, v3, v4);
}

看到它在GodBolt上工作。

我推荐这个。我发现它非常优雅,正是我(和你)所需要的。

https://github.com/CommitThis/zip-iterator

以防万一这里有一个代码副本。注意,它是在MIT许可下发布的,也不要忘记写上作者的名字。

zip.hpp

/***
 * MIT License
 * Author: G Davey
 */
#pragma once
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <typeinfo>
namespace c9 {
template <typename Iter>
using select_access_type_for = std::conditional_t<
    std::is_same_v<Iter, std::vector<bool>::iterator> ||
    std::is_same_v<Iter, std::vector<bool>::const_iterator>,
    typename Iter::value_type,
    typename Iter::reference
>;

template <typename ... Args, std::size_t ... Index>
auto any_match_impl(std::tuple<Args...> const & lhs,
    std::tuple<Args...> const & rhs,
    std::index_sequence<Index...>) -> bool
{
    auto result = false;
    result = (... | (std::get<Index>(lhs) == std::get<Index>(rhs)));
    return result;
}

template <typename ... Args>
auto any_match(std::tuple<Args...> const & lhs, std::tuple<Args...> const & rhs)
    -> bool
{
    return any_match_impl(lhs, rhs, std::index_sequence_for<Args...>{});
}

template <typename ... Iters>
class zip_iterator
{
public:
    using value_type = std::tuple<
        select_access_type_for<Iters>...
    >;
    zip_iterator() = delete;
    zip_iterator(Iters && ... iters)
        : m_iters {std::forward<Iters>(iters)...}
    {
    }
    auto operator++() -> zip_iterator&
    {
        std::apply([](auto && ... args){ ((args += 1), ...); }, m_iters);
        return *this;
    }
    auto operator++(int) -> zip_iterator
    {
        auto tmp = *this;
        ++*this;
        return tmp;
    }
    auto operator!=(zip_iterator const & other)
    {
        return !(*this == other);
    }
    auto operator==(zip_iterator const & other)
    {
        auto result = false;
        return any_match(m_iters, other.m_iters);
    }
    auto operator*() -> value_type
    {
        return std::apply([](auto && ... args){
                return value_type(*args...);
            }, m_iters);
    }
private:
    std::tuple<Iters...> m_iters;
};

/* std::decay needed because T is a reference, and is not a complete type */
template <typename T>
using select_iterator_for = std::conditional_t<
    std::is_const_v<std::remove_reference_t<T>>,
    typename std::decay_t<T>::const_iterator,
    typename std::decay_t<T>::iterator>;

template <typename ... T>
class zipper
{
public:
    using zip_type = zip_iterator<select_iterator_for<T> ...>;
    template <typename ... Args>
    zipper(Args && ... args)
        : m_args{std::forward<Args>(args)...}
    {
    }
    auto begin() -> zip_type
    {
        return std::apply([](auto && ... args){
                return zip_type(std::begin(args)...);
            }, m_args);
    }
    auto end() -> zip_type
    {
        return std::apply([](auto && ... args){
                return zip_type(std::end(args)...);
            }, m_args);
    }
private:
    std::tuple<T ...> m_args;
};

template <typename ... T>
auto zip(T && ... t)
{
    return zipper<T ...>{std::forward<T>(t)...};
}
}

#include "zip.hpp"
#include <vector>
std::vector<int> a, b, c;
void foo() {
    for (auto && [x, y] : zip(a, b))
        c.push_back(x + z);
}

迭代器有zip_iterator可以使用(示例在文档中)。它不能使用range for,但您可以使用std::for_each和lambda。

下面是一个不需要boost的简单版本。它不会特别有效,因为它创建临时值,它不能泛化除列表以外的容器,但它没有依赖关系,它解决了最常见的压缩情况。

template<class L, class R>
std::list< std::pair<L,R> >  zip(std::list<L> left, std::list<R> right)
{
auto l = left.begin();
auto r = right.begin();
std::list< std::pair<L,R> > result;
  while( l!=left.end() && r!=right.end() )
    result.push_back( std::pair<L,R>( *(l++), *(r++) ) );
  return result;
}

尽管其他版本更加灵活,但通常使用列表操作符的目的是编写简单的一行代码。这个版本的优点是common-case很简单。