场景:

  按DDX值倒序取前十的板块代码,用 map<float, string, greater<float> > mapBKDDX; 存储时,相同DDX值的板块只能存储第一个,有数据缺失。

解决方法:

  使用 multimap, 拥有等价关键的 关键-值 pair 的顺序就是插入顺序,且不会更改。(C++11 起)

#include <iostream>
#include <map>
#include <algorithm>
#include <functional>

int main()
{
	multimap<float, string, greater<float> > mapBKDDX;

    mapBKDDX.insert(make_pair(1.11, "880001"));
    mapBKDDX.insert(make_pair(1.12, "880001"));
    mapBKDDX.insert(make_pair(1.11, "880002"));
    mapBKDDX.insert(make_pair(1.10, "880001"));

    multimap<float, string, greater<float> >::iterator iter, iter_begin, iter_end;
    iter_begin = mapBKDDX.lower_bound(1.11);
    iter_end = mapBKDDX.upper_bound(1.11);

    for (iter = iter_begin; iter != iter_end; iter++)
    {
        cout << iter->second.c_str() << endl;
    }
	
	return 0;
}

  

相关文章:

  • 2022-01-09
  • 2021-10-08
  • 2021-07-16
  • 2021-08-07
  • 2021-09-09
  • 2021-07-18
  • 2021-11-11
  • 2022-01-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-06-10
  • 2022-12-23
  • 2021-12-05
  • 2021-12-16
相关资源
相似解决方案