gpt4 book ai didi

c++ - 如何让迭代器进入 boost fusion map?

转载 作者:太空狗 更新时间:2023-10-29 21:07:54 25 4
gpt4 key购买 nike

我正在尝试获取 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

最佳答案

查看 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)

关于c++ - 如何让迭代器进入 boost fusion map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4211481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com