使用map自动排序的特性获取最大值最小值以及key值对应的value

map在插入键值对时会按照key值从小到大自动排序,现在需要获取最大key对应的value;
或者最小key对应的value

最大key对应的valuemap最后一组数据,使用迭代器iterator或者反向迭代器reverse_iterator获取
最小key对应的valuemap第一组数据

初始化一个map

#include <map>
...
map<int,int> map_test;
map_test.insert(pair<int, int>(2,3));
map_test.insert(pair<int, int>(1,4));
map_test.insert(pair<int, int>(0,5));

1. 获取最大key对应的value

(1)使用迭代器获取最后一组数据

map<int,int>::iterator it1 = map_test.end();
it1--;
int max_value = it1->second;

(2)使用反向迭代器

map<int,int>::reverse_iterator it2 = map_test.rbegin();
max_value = it2->second;
...

2. 获取最小key对应的value

map<int,int>::iterator it1 = map_test.begin();
int max_value = it1->second;
...
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设有一个输入文件input.txt,其每行包含一个整数,编写MapReduce程序来计算总和、平均(取整数部分)、最大值最小值以及最大的三个数字。 Mapper阶段: ``` public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private static final String SUM = "Sum"; private static final String COUNT = "Count"; private static final String MAX = "Max"; private static final String MIN = "Min"; private static final String TOP3 = "Top3"; private static final int TOP_NUM = 3; private int sum = 0; private int count = 0; private int max = Integer.MIN_VALUE; private int min = Integer.MAX_VALUE; private PriorityQueue<Integer> top3 = new PriorityQueue<>(TOP_NUM, Collections.reverseOrder()); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { int num = Integer.parseInt(value.toString()); sum += num; count++; max = Math.max(max, num); min = Math.min(min, num); top3.offer(num); if (top3.size() > TOP_NUM) { top3.poll(); } } @Override protected void cleanup(Context context) throws IOException, InterruptedException { context.write(new Text(SUM), new IntWritable(sum)); context.write(new Text(COUNT), new IntWritable(count)); context.write(new Text(MAX), new IntWritable(max)); context.write(new Text(MIN), new IntWritable(min)); for (Integer num : top3) { context.write(new Text(TOP3), new IntWritable(num)); } } } ``` Reducer阶段: ``` public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private static final String SUM = "Sum"; private static final String COUNT = "Count"; private static final String MAX = "Max"; private static final String MIN = "Min"; private static final String TOP3 = "Top3"; private static final int TOP_NUM = 3; @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; int count = 0; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; PriorityQueue<Integer> top3 = new PriorityQueue<>(TOP_NUM, Collections.reverseOrder()); for (IntWritable value : values) { int num = value.get(); sum += num; count++; max = Math.max(max, num); min = Math.min(min, num); top3.offer(num); if (top3.size() > TOP_NUM) { top3.poll(); } } if (key.toString().equals(SUM)) { context.write(new Text(SUM), new IntWritable(sum)); } else if (key.toString().equals(COUNT)) { context.write(new Text(COUNT), new IntWritable(count)); } else if (key.toString().equals(MAX)) { context.write(new Text(MAX), new IntWritable(max)); } else if (key.toString().equals(MIN)) { context.write(new Text(MIN), new IntWritable(min)); } else if (key.toString().equals(TOP3)) { for (Integer num : top3) { context.write(new Text(TOP3), new IntWritable(num)); } } } } ``` 驱动程序: ``` public class MyDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "My MapReduce"); job.setJarByClass(MyDriver.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 运行: ``` hadoop jar myjar.jar MyDriver input.txt output ``` 输出: ``` Count 10 Max 99 Min 1 Sum 505 Top3 97 Top3 98 Top3 99 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值