使用具有增强范围的变换算法

时间:2013-01-21 09:44:11

标签: stl-algorithm boost-range

您好我正在尝试使用boost.range添加两个std向量,但我收到了一堆错误。

这有效:

  std::transform(a.begin(),a.end(),b.begin(),a.begin(),std::plus<double>());

这不是:

  boost::transform(a,b,a,std::plus<double>());

有错误:

In file included from /usr/local/include/boost/range/algorithm.hpp:80:0,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.h:15,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:1:
/usr/local/include/boost/range/algorithm/transform.hpp: In function ‘OutputIterator boost::range_detail::transform_impl(SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator2, SinglePassTraversalReadableIterator2, OutputIterator, BinaryFunction) [with SinglePassTraversalReadableIterator1 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, SinglePassTraversalReadableIterator2 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, OutputIterator = std::vector<double>, BinaryFunction = std::plus<double>]’:
/usr/local/include/boost/range/algorithm/transform.hpp:90:33:   instantiated from ‘OutputIterator boost::range::transform(const SinglePassRange1&, const SinglePassRange2&, OutputIterator, BinaryOperation) [with SinglePassRange1 = std::vector<double>, SinglePassRange2 = std::vector<double>, OutputIterator = std::vector<double>, BinaryOperation = std::plus<double>]’
/home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:9:45:   instantiated from here
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: error: no match for ‘operator*’ in ‘*out’
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: note: candidates are:
/usr/include/c++/4.6/complex:399:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const std::complex<_Tp>&)
/usr/include/c++/4.6/complex:390:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const _Tp&)
/usr/include/c++/4.6/complex:381:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const std::complex<_Tp>&)
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: error: no match for ‘operator++’ in ‘++out’
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: note: candidate is:
/usr/local/include/boost/iterator/iterator_facade.hpp:722:3: note: template<class I, class V, class TC, class R, class D> typename boost::detail::postfix_increment_result<I, V, R, TC>::type boost::operator++(boost::iterator_facade<Derived, V, TC, R, D>&, int)

并且都没有:

  std::vector<double> c;
  boost::transform(a,b,c,std::plus<double>());

有错误:

In file included from /usr/local/include/boost/range/algorithm.hpp:80:0,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.h:15,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:1:
/usr/local/include/boost/range/algorithm/transform.hpp: In function ‘OutputIterator boost::range_detail::transform_impl(SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator2, SinglePassTraversalReadableIterator2, OutputIterator, BinaryFunction) [with SinglePassTraversalReadableIterator1 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, SinglePassTraversalReadableIterator2 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, OutputIterator = std::vector<double>, BinaryFunction = std::plus<double>]’:
/usr/local/include/boost/range/algorithm/transform.hpp:90:33:   instantiated from ‘OutputIterator boost::range::transform(const SinglePassRange1&, const SinglePassRange2&, OutputIterator, BinaryOperation) [with SinglePassRange1 = std::vector<double>, SinglePassRange2 = std::vector<double>, OutputIterator = std::vector<double>, BinaryOperation = std::plus<double>]’
/home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:8:45:   instantiated from here
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: error: no match for ‘operator*’ in ‘*out’
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: note: candidates are:
/usr/include/c++/4.6/complex:399:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const std::complex<_Tp>&)
/usr/include/c++/4.6/complex:390:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const _Tp&)
/usr/include/c++/4.6/complex:381:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const std::complex<_Tp>&)
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: error: no match for ‘operator++’ in ‘++out’
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: note: candidate is:
/usr/local/include/boost/iterator/iterator_facade.hpp:722:3: note: template<class I, class V, class TC, class R, class D> typename boost::detail::postfix_increment_result<I, V, R, TC>::type boost::operator++(boost::iterator_facade<Derived, V, TC, R, D>&, int)

1 个答案:

答案 0 :(得分:3)

你的问题是boost::transform的第三个参数不是范围,而是用于将结果写入的(输出)迭代器。

以下代码为我编译:

boost::transform ( a, b, a.begin(), std::plus<double>());