本文介绍了是否有一个跨std :: multimap中的唯一键的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的或标准的方法来拥有一个多重迭代器迭代多重映射中的唯一键?

Is there a simple or standard way to have a multimap iterator which iterate across unique keys in a multimap?

对于如下的集合: {1,a},{1,lemon},{2,peacock},{3,angel}
一个迭代器,从 {1,a} 开始,然后增量将指向 {2,peacock} ,然后再次递增将指向 {3,angel}

i.e. for a set that looks like: {1, "a"}, {1, "lemon"}, {2, "peacock"}, {3, "angel"}an iterator which would start at {1, "a"} then incrementing would point to {2, "peacock"} and then incrementing again would point to {3, "angel"}?

推荐答案

您可以使用 upper_bound 增加迭代器位置,而不是 ++

You can use upper_bound to increment the iterator position instead of ++:

#include <map>
#include <string>
#include <iostream>

using namespace std;

int main()
{
  multimap<int,string> mm;
  mm.insert(make_pair(1, "a"));
  mm.insert(make_pair(1, "lemon"));
  mm.insert(make_pair(2, "peacock"));
  mm.insert(make_pair(3, "angel"));

  for( auto it = mm.begin(), end = mm.end();
       it != end;
       it = mm.upper_bound(it->first)
  )
    cout << it->first << ' ' << it->second << endl;
  return 0;
}

这篇关于是否有一个跨std :: multimap中的唯一键的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:32