如何将迭代器转换为boost融合图?

时间:2010-11-18 03:39:21

标签: c++ boost boost-fusion boost-iterators iterator-range

我正在尝试为boost::fusion::map获取迭代器,但我遗漏了一些东西。

以下适用于boost::fusion::vector

typedef vector<int,char,double> vector_t;
vector_t vec(20,'B',14.5);
typedef result_of::begin<vector_t>::type vb;
typedef result_of::end<vector_t>::type ve;

vb vb_(vec);
ve ve_(vec);
iterator_range<vb,ve> range(vb_,ve_);
std::cout << range << std::endl;

输出:

(20 B 14.5)

我想用fusion::map做类似的事情,但它无法编译:

typedef map< pair<int,char>, pair<double, std::string> > map_t;
typedef result_of::begin<map_t>::type b;

map_t m( pair<int,char>('C'), pair<double, std::string>("CEE") );

//b b_(m);    // this fails to compile

我包括这些:

#include <iostream>
#include <string>

#include <boost/fusion/support/pair.hpp>
#include <boost/fusion/include/pair.hpp>

#include <boost/fusion/container/map.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/container/map/map_fwd.hpp>
#include <boost/fusion/include/map_fwd.hpp>

#include <boost/fusion/sequence/intrinsic/begin.hpp>
#include <boost/fusion/include/begin.hpp>

#include <boost/fusion/sequence/intrinsic/end.hpp>
#include <boost/fusion/include/end.hpp>

#include <boost/fusion/view/iterator_range.hpp>
#include <boost/fusion/include/iterator_range.hpp>

#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/include/io.hpp>

using namespace boost::fusion;

错误:

    main.cpp: In function 'int main()':
    main.cpp:83: error: no matching function for call to 'boost::fusion::basic_iterator<boost::fusion::map_iterator_tag, 
boost::fusion::map<boost::fusion::pair<int, char>, boost::fusion::pair<double, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>::category, boost::fusion::map<boost::fusion::pair<int, char>, 
boost::fusion::pair<double, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>, 0>::basic_iterator(main()::map_t&)'
    /home/bimbo/local/linux/include/boost-
1_43_0/boost/fusion/iterator/basic_iterator.hpp:125: note: candidates are: 
boost::fusion::basic_iterator<Tag, Category, Seq, Index>::basic_iterator(Seq&, int) [with 
Tag = boost::fusion::map_iterator_tag, Category = 
boost::fusion::map<boost::fusion::pair<int, char>, boost::fusion::pair<double, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>::category, Seq = boost::fusion::map<boost::fusion::pair<int, char>, 
boost::fusion::pair<double, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>, int Index = 0]
    /home/bimbo/local/linux/include/boost-
1_43_0/boost/fusion/iterator/basic_iterator.hpp:43: note:                 
boost::fusion::basic_iterator<boost::fusion::map_iterator_tag, 
boost::fusion::map<boost::fusion::pair<int, char>, boost::fusion::pair<double, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>::category, boost::fusion::map<boost::fusion::pair<int, char>, 
boost::fusion::pair<double, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>, 0>::basic_iterator(const 
boost::fusion::basic_iterator<boost::fusion::map_iterator_tag, 
boost::fusion::map<boost::fusion::pair<int, char>, boost::fusion::pair<double, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>::category, boost::fusion::map<boost::fusion::pair<int, char>, 
boost::fusion::pair<double, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_>, 0>&)
    scons: *** [main.o] Error 1

1 个答案:

答案 0 :(得分:4)

查看fusion::for_each,它使用fusion::begin()fusion::end()来获取迭代器。 当我申请fusion::map时,它有效:

typedef map< pair<int,char>, pair<double, std::string> > map_t;
typedef result_of::begin<map_t>::type b;
typedef result_of::end<map_t>::type e;

map_t m( pair<int,char>('C'), pair<double, std::string>("CEE") );

b b_(begin(m));
e e_(end(m));

iterator_range<b,e> range(b_,e_);

std::cout << "map_t: \n" << range << std::endl;

或内联:

iterator_range<b,e> range(begin(m),end(m));

输出:

map_t:
(C CEE)