我有vector
:
std::vector<island> sea;
现在我想在另一个vector
中指向此vector
的所有元素。但只是为了个人练习,我想用一种奇特的C ++风格来做这件事,所以我输入std::vector<const island*> p_sea
而不是std::vector<std::reference_wrapper<const island>> r_sea;
:
vector
现在我想用引用填充这个新的std::transform(sea.begin(), sea.end(),
std::back_inserter(r_sea),
std::cref<island>
);
:
transform
我理解它的方式,从cppreference文章中,std::cref<island>
的第四个参数应该是一个函数,它从源范围获取元素的const引用并返回目标范围的元素;这正是const island&
所做的:它将std::reference_wrapper<const island>
作为参数并返回#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
struct island {
long long x,y; //coords
};
int main() {
std::vector<island> sea;
std::vector<std::reference_wrapper<const island>> r_sea;
std::transform(sea.begin(), sea.end(),
std::back_inserter(r_sea),
std::cref<island>
);
return 0;
}
。所以我相信这应该有用吗?
然而,它没有:
prog.cpp: In function ‘int main()’:
prog.cpp:19:5: error: no matching function for call to ‘transform(std::vector<island>::iterator, std::vector<island>::iterator, std::back_insert_iterator<std::vector<std::reference_wrapper<const island> > >, <unresolved overloaded function type>)’
);
^
In file included from /usr/include/c++/6/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/6/bits/stl_algo.h:4166:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
transform(_InputIterator __first, _InputIterator __last,
^~~~~~~~~
/usr/include/c++/6/bits/stl_algo.h:4166:5: note: template argument deduction/substitution failed:
prog.cpp:19:5: note: could not resolve address from overloaded function ‘cref<island>’
);
^
In file included from /usr/include/c++/6/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/6/bits/stl_algo.h:4203:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
transform(_InputIterator1 __first1, _InputIterator1 __last1,
^~~~~~~~~
/usr/include/c++/6/bits/stl_algo.h:4203:5: note: template argument deduction/substitution failed:
prog.cpp:19:5: note: could not resolve address from overloaded function ‘cref<island>’
);
这会导致以下编译错误:
SELECT department_id,
ROUND(AVG(c.cnumber),1)
FROM employees c
WHERE c.cnumber =
(SELECT COUNT(c.employee_id)
FROM employees c)
GROUP BY department_id;
我做错了什么?
......我回到邪恶的C指针。