【发布时间】:2017-12-03 17:19:43
【问题描述】:
我正在尝试编写一个小算法来查找两组的共同和独特部分,我想以通用方式编写它,所以我有这个小例子:
#include "boost/tuple/tuple.hpp"
#include <set>
template <typename InputIt, typename Value = typename std::iterator_traits<InputIt>::value_type>
boost::tuple<std::set<Value>, std::set<Value>, std::set<Value>>
findUniqueAndCommon(InputIt fbegin, InputIt fend, InputIt sbegin, InputIt send)
{
std::set<Value> setL(fbegin, fend);
std::set<Value> setR(sbegin, send);
std::set<Value> uniqueInCatalog1;
std::set<Value> uniqueInCatalog2;
std::set<Value> commonInBoth;
std::set_difference(setL.begin(), setL.end(), setR.begin(), setR.end(), uniqueInCatalog1.begin());
std::set_difference(setR.begin(), setR.end(), setL.begin(), setL.end(), uniqueInCatalog2.begin());
std::set_intersection(setL.begin(), setL.end(), setR.begin(), setR.end(), commonInBoth.begin());
return{ uniqueInCatalog1, uniqueInCatalog2, commonInBoth };
}
int main()
{
std::set<int> x = {1, 2, 3};
std::set<int> y = {4, 2, 3};
findUniqueAndCommon<std::set<int>::iterator>(x.begin(), x.end(), y.begin(), y.end());
}
我的问题是为什么这个函数编译失败?我尝试了 gcc、clang 和 MSVC,但都失败了。可以在此处查看 Clang 的错误消息:
https://godbolt.org/g/gFZyzo
非常感谢。
【问题讨论】:
-
那个链接只是说
<Compilation failed>。 -
如果您将鼠标悬停在显示错误的行上,您会看到错误消息@melpomene
-
不,我不知道。我得到的只是错误消息中间的一些“注释”。
-
set_*函数需要OutputIterator作为结果。 (请随意从您的示例中删除不相关的 boost 用法。) -
你忘了#include
标签: c++ c++11 stdset set-operations insert-iterator