std::set::equal_range 有什么用? [英] What is std::set::equal_range for?

查看:32
本文介绍了std::set::equal_range 有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 std::set 不能包含重复元素并且总是排序的,std::set::equal_range 将总是返回没有或 1 个元素的范围.从技术上讲,是的,这仍然是一个范围,但是这个算法的目的是什么?对于 std::set 来说,这似乎是不必要的.

Since std::set cannot contain duplicate elements and is always sorted, std::set::equal_range will always return the range that has either none or 1 element. Technically, yes, that's still a range, but what is the purpose of this algorithm? For std::set it seems pretty unnecessary.

推荐答案

我只是猜测.但是,就像 count(),当你在模板中时它有一些价值,并且不想确定您是在 std::set 还是其他关联容器上操作.

I'm only guessing. But, like count(), it has some value when you're in a template and don't want to have to determine whether you're operating on a std::set or some other associative container.

基本上,这是为了一致性.该函数确实像宣传的那样执行,只是它有问题对比,例如 find() 之类的东西,如果你孤立地看待它.如果您确实想要一个开箱即用的半开范围,它确实可能为您节省一个手动迭代器增量.:P

Basically, it's for consistency. The function does perform as advertised, it's just that yes it has questionable use versus something like find() if you take it in isolation. It does potentially save you one manual iterator increment if you really do want a half-open range out of the box though. :P

这篇关于std::set::equal_range 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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