使用std :: get<>()和std :: tie<>()以及boost构造有什么选择?
例:
我想使用基于范围的for循环来迭代几个容器.我可以实现zip函数,它使用boost :: zip_iterator.
#include <boost/iterator/zip_iterator.hpp> #include <boost/range.hpp> template <typename... TContainer> auto zip(TContainer&... 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:list<int> a; std::vector<double> b; ... for (auto t : zip(a,b)) { // access elements by boost::get<0>(t),boost::get<1>(t) // or use boost::tie(a_element,b_element) }
当我调用其他方法时出现问题,这需要std :: tuple或std :: pair – 我必须转换),因为其余代码使用std :: tuples,或者模板化代码使用std :: get<> ()和/或std :: tie().
我找到了一些为zip_iterator添加std :: tuple支持的补丁,但这些补丁不适用于我的版本(我使用的是Boost 1.54).
我错过了什么吗?我有什么选择强制zip_iterator返回std :: tuple或make std :: get,std :: tie等可用于boost类型?
解决方法
你有没有尝试过
#include <boost/iterator/zip_iterator.hpp> #include <boost/range.hpp> #include <tuple> template <typename... TContainer> auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(std::make_tuple(std::begin(containers)...))>> { ^^^ auto zip_begin = boost::make_zip_iterator(std::make_tuple(std::begin(containers)...)); ^^^ auto zip_end = boost::make_zip_iterator(std::make_tuple(std::end(containers)...)); ^^^ return boost::make_iterator_range(zip_begin,zip_end); }
原文地址:https://www.jb51.cc/c/118406.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。