与Java中的C ++ map.lower_bound等效 [英] Equivalent of C++ map.lower_bound in Java

查看:121
本文介绍了与Java中的C ++ map.lower_bound等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很基本,但是我自己找不到解决方案.

My question is very basic, but I couldn't find the solution myself.

我习惯于用C ++编写算法.在这里,我经常使用std::map结构及其提供的所有辅助方法.

I am used to writing algorithms in C++. There I very often use the std::map structure, together with all the auxiliary methods it provides.

此方法将迭代器返回到映射的第一个元素,其中键> =返回给定的键.示例:

This method returns iterator to the first element of the map with key >= to the key given as parameter. Example:

map<int, string> m;
// m = { 4 => "foo", 6 => "bar", 10 => "abracadabra" }
m.lower_bound(2); // returns iterator pointing to <4, "foo">
m.lower_bound(4); // returns iterator pointing to <4, "foo">
m.lower_bound(5); // returns iterator pointing to <6, "bar">

很棒的事情是C ++映射基于红黑树,因此查询是对数(O(log n)).

The cool thing is that the C++ map is based on red-black trees and so the query is logarithmic (O(log n)).

现在,我需要在Java中实现某种算法.我需要与我刚刚描述的功能类似的功能.我知道我可以使用在有序树中实现的TreeMap.但是我似乎没有找到与方法lower_bound等效的方法.有吗?

Now I need to implement a certain algorithm in Java. I need similar functionality as the one I just described. I know I can use TreeMap which is implemented in ordered tree. However I don't seem to find equivalent of the method lower_bound. Is there such?

非常感谢您的帮助.

推荐答案

我猜您在寻找

I guess that you're looking for TreeMap. Take a look at ceilingKey/Entry methods.

这篇关于与Java中的C ++ map.lower_bound等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆