如何获得 multimap的中的key的数目 (**)

如何获得multimap的中的key的数目

问题解决——multimap中统计key的种类

  https://www.cnblogs.com/wlsandwho/p/4202686.html

c++ - 如何获取 multimap 中的所有唯一键

----------------------------------------------------

不重复的 key总数,以及第一和最后一个key (***)

uniqueKeys / firstKey ~lastKey

    qDebug()<<" uniqueKeys :: "<<multiMap.uniqueKeys();
    qDebug()<<" lastKey :: "<<multiMap.lastKey();
    qDebug()<<" firstKey :: "<<multiMap.firstKey();

参考:

如何获得 multimap的中的key的数目 (**)
https://blog.csdn.net/ken2232/article/details/132061050

QHash & QMap 的顺序问题 (***)
https://blog.csdn.net/ken2232/article/details/132051508

==============================

c++ - 如何获取 multimap 中的所有唯一键

我试过了,效果很好

for(  multimap<char,int>::iterator it = mymm.begin(), end = mymm.end(); it != end; it = mymm.upper_bound(it->first))
  {
      cout << it->first << ' ' << it->second << endl;
  }

关于c++ - 如何获取 multimap 中的所有唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11554932/

How can I get all the unique keys in a multimap

c++ - How can I get all the unique keys in a multimap - Stack Overflow

easiest way would be to put the keys of multimap in an unordered_set

unordered_multimap<string, string> m;

//insert data in multimap

unordered_set<string> s;         //set to store the unique keys

for(auto it = m.begin(); it != m.end(); it++){
    if(s.find(it->first) == s.end()){
        s.insert(it->first);
        auto its = m.equal_range(it->first);
        for(auto itr=its.first;itr!=its.second;itr++){
            cout<<itr->second<<" ";
        }
    }
}

 问题解决——multimap中统计key的种类

  https://www.cnblogs.com/wlsandwho/p/4202686.html

由于之前很少很少用STL的东西,所以,只能凭个人爱好,大致的写写。

自己写了一个简单的类,自然的,会有一个multimap作为成员变量,然后再添加一个set用来辅助multimap做统计。

就是这么简单。这也应该是最简单的解决方法

如何遍历 multimap 中所有的 Key

October 19, 2006

typedef std::multimap<int ,std::string> MyMap;
typedef std::set<int> MySet;
void main()
{
MyMap map;
MySet set;
map.insert(std::make_pair(1,"12889898989"));
map.insert(std::make_pair(2,"343434323232"));
map.insert(std::make_pair(1,"2323232323"));
map.insert(std::make_pair(2,"2344423232323"));
}

请问我如何才能做出一个循环,打印出multimap中的键为12

google 以下, 此为答案:

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;
}

p = c.upper_bound(k) 的作用是:

p points to the first element with key > k or c.end(), ordered container only

multimap 就是 ordered 的, 所以 upper_bound 可以逐个的跳过重复的 key

C++ 中还有 unordered_multimap, 这种情况下就不能使用 upper_bound 了, 只能一个个去过滤了

C++ 中的 setmap 可以结合不同的属性, plain/unordered, plain/multi, plain 可以省略, 因此默认是 ordered, plain 属性

因此 C++ 中有这么多的 set|map

set   multiset  unordered_set unordered_multiset
map   multimap  unordered_map unordered_multimap

如何获得multimap的中的key的数目

在multimap中,同一个键关联的元素必然相邻存放。基于这个事实,就可以将某个键对应的值一一输出。

1、使用find和count函数。count函数求出某个键出现的次数,find函数返回一个迭代器,指向第一个拥有正在查找的键的实例。

2、使用lower_bound(key)和upper_bound(key) lower_bound(key)返回一个迭代器,指向键不小于k的第一个元素upper_bound(key)返回一个迭代器,指向键不大于k的第一个元素

3、使用equat_range(key) 返回一个迭代器的pair对象,first成员等价于lower_bound(key),second成员等价于upper_bound(key)

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    multimap<string,int> m_map;
    string s("中国"),s1("美国");
    m_map.insert(make_pair(s,50));
    m_map.insert(make_pair(s,55));
    m_map.insert(make_pair(s,60));

    //
    m_map.insert(make_pair(s1,30));
    m_map.insert(make_pair(s1,20));
    m_map.insert(make_pair(s1,10));

   
    //方式1
    int k;
    multimap<string,int>::iterator m;
    m = m_map.find(s);
    for(k = 0;k != m_map.count(s);k++,m++)
        cout<<m->first<<"--"<<m->second<<endl;

   
    //方式2
    multimap<string,int>::iterator beg,end;
    beg = m_map.lower_bound(s1);
    end = m_map.upper_bound(s1);
    for(m = beg;m != end;m++)
        cout<<m->first<<"--"<<m->second<<endl;

   
    //方式3
    beg = m_map.equal_range(s).first;
    end = m_map.equal_range(s).second;
    for(m = beg;m != end;m++)
        cout<<m->first<<"--"<<m->second<<endl;
    return 0;
}

  如何获得multimap的中的key的数目_百度知道

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值