加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 哪些元素将从std :: multimap :: find返回?

发布时间:2020-12-16 06:44:16 所属栏目:百科 来源:网络整理
导读:很可能这个问题是重复的,但我找不到它的引用. 我正在看着std::multiset::find std::multimap::find的功能,我想知道如果多次插入一个特定的键,哪个元素将被返回? 从描述: Notice that this function returns an iterator to a single element (of the possi
很可能这个问题是重复的,但我找不到它的引用.

我正在看着std::multiset::find& std::multimap::find的功能,我想知道如果多次插入一个特定的键,哪个元素将被返回?

从描述:

Notice that this function returns an iterator to a single element (of
the possibly multiple equivalent elements)

是否保证单个元素是第一个插入或是随机的?

背景

我问的原因是我正在实现类似class的multipmap:

typedef std::vector<Item> Item_vector;

class Item
{
  string m_name;
};

class MyItemMultiMap
{
public:

  // forgive me for not checking if key exist in the map. it is just an example.

  void add_item( const Item& v ) { m_map[v.m_name].push_back(v); }

  // is returning the first item in the vector mimic std::multimap::find behavior?
  Item& get_item( const string& v ) { return m_map[v][0]; } 

private:
  std::map<string,Item_vector> m_map;
};

我希望get_item()正好像std :: multimap :: find一样工作.可能吗?如果是,如何实施?

解决方法

如果存在多个查找方法可能返回任意一个,尽管您的STL实现可能确实只是给出第一个.

使用’lower_bound’方法更安全,并从中重复(参见std::multimap::lower_bound).请注意,如果您要查找的内容不存在,那么’lower_bound’会返回另一个元素的引用!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读