有什么方法可以使用STL在C++中搜索项目或属性或变量。
我们可以使用STL提供的任何容器Searching time as less as possible
。容器包含pair<int,int>
。我想搜索一对p(a,x)
哪个应该返回所有对的X,其p.first == Xi.first and p.second != Xi.second for all i
。
例如
让容器是unordered_set。在C++中使用STL搜索变量/项目/属性?
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'来找到给定键的范围,然后遍历该范围。 – 2014-10-08 08:20:07