這不構建,我不明白編譯錯誤。爲什麼我不能std ::分區這個std :: unordered_map?
#include <unordered_map>
#include <algorithm>
int main()
{
std::unordered_map<int, size_t> occurences = { { 10, 2 }, { 20, 5 }, { 30, 0 }, { 40, 5 }, { 50, 0 }, { 100, 9 } };
auto newEnd = std::partition(occurences.begin(), occurences.end(), [](const std::pair<int, size_t> &p)
{
return p.second == 0;
});
return 0;
}
g ++抱怨如下。 VS2013更加神祕。
/usr/local/include/c++/6.3.0/bits/stl_pair.h: In instantiation of 'void std::pair<_T1, _T2>::swap(std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = long unsigned int]': /usr/local/include/c++/6.3.0/bits/stl_pair.h:473:7: required from 'void std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = long unsigned int]' /usr/local/include/c++/6.3.0/bits/stl_algobase.h:148:11: required from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = std::__detail::_Node_iterator, false, false>; _ForwardIterator2 = std::__detail::_Node_iterator, false, false>]' /usr/local/include/c++/6.3.0/bits/stl_algo.h:1500:20: required from '_ForwardIterator std::__partition(_ForwardIterator, _ForwardIterator, _Predicate, std::forward_iterator_tag) [with _ForwardIterator = std::__detail::_Node_iterator, false, false>; _Predicate = main()::&)>]' /usr/local/include/c++/6.3.0/bits/stl_algo.h:4524:30: required from '_BIter std::partition(_BIter, _BIter, _Predicate) [with _BIter = std::__detail::_Node_iterator, false, false>; _Predicate = main()::&)>]' main.cpp:12:4: required from here /usr/local/include/c++/6.3.0/bits/stl_pair.h:416:6: error: no matching function for call to 'swap(const int&, const int&)' swap(first, __p.first);
至於我可以告訴這個地圖上符合上市cppreference.com在std ::分區類型要求,讓我很爲難。我的問題是爲什麼它沒有建立?
你想要做什麼?一個'std :: map'總是排序的。一個'std :: unordered_map'沒有固定的順序概念。無論哪種情況,分區都沒有意義。 – jtbandes
@jtbandes我想這樣做的原因是,這是一個最小的例子,其中實際的代碼有一個映射作爲適當的容器出於其他原因。我在這裏的用法是我想要的int(索引)有零個發生的一小部分。我可以通過std解決::將內容複製到一個向量中。 – acraig5075
您錯過了迭代器上的[ValueSwappable](http://en.cppreference.com/w/cpp/concept/ValueSwappable)要求。 – molbdnilo