【发布时间】:2014-12-02 19:50:53
【问题描述】:
有任何方法可以使用 STL 在 C++ 中搜索项目、属性或变量。
我们可以使用任何提供 Searching time as less as possible 的 STL 容器。容器包含 pair<int,int> 。我想搜索一对p(a,x),它应该返回所有对X,其p.first == Xi.first and p.second != Xi.second for all i。
例如
让容器是 unordered_set 。
unordered_set< pair<int , int > > myset =
{{1,2},{1,5},{1,6},{2,4},{3,5},{4,6},{6,7},{6,8}};
if i search for p(1,5) then it should return pair(1,2),(1,6)
if i search for p(2,4) or (3,5),(6,7) then it should return NULL i.e. nothing
if i search for p(6,7) then it should return pair(6,8)
【问题讨论】:
-
参加
multimap或unordered_multimap;使用equal_range查找具有给定键的范围,然后遍历该范围。
标签: c++ search stl stl-algorithm