查找< map>排名的最快方法 [英] Quickest way to find the rank of a <map>

查看:76
本文介绍了查找< map>排名的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




给出一张地图:

typedef map< long,string,greater< long> > mapOfFreq;


有没有更快的方法来找到元素的等级(即指数)

的长值为x?

目前我正在迭代地图并记住我什么时候点击它。


谢谢


史蒂夫

Hi,

Given a map:
typedef map<long, string, greater<long> > mapOfFreq;

Is there a quicker way to find the rank (i.e. index) of the elememt that
has the long value of x?
At the moment I''m iterating through the map and keeping count of when I
hit it.

Thanks

Steve

推荐答案

Steve Edwards写道:
Steve Edwards wrote:

给定一张地图:
typedef map< long,string,greater< long> > mapOfFreq;

有没有更快的方法来找到
具有长x值的元素的等级(即索引)?
目前我正在迭代通过地图并记录我何时点击它。
Hi,

Given a map:
typedef map<long, string, greater<long> > mapOfFreq;

Is there a quicker way to find the rank (i.e. index) of the elememt that
has the long value of x?
At the moment I''m iterating through the map and keeping count of when I
hit it.




查看std :: map的成员函数,特别是find。


你的算法是O(n),成员函数可能是O(log2 n)。


Ben Pope

-

我不只是一个号码。对很多人来说,我被称为字符串......



Check out the member functions of std::map, notably, find.

Your algorithm is O(n), the member function is probably O(log2 n).

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


文章< 44 **************** ****** @taz.nntpserver.com>,

Ben Pope< be *************** @ gmail.com>写道:
In article <44**********************@taz.nntpserver.com>,
Ben Pope <be***************@gmail.com> wrote:
Steve Edwards写道:
Steve Edwards wrote:


给出一张地图:
typedef map< long,string ,更大< long> > mapOfFreq;

有没有更快的方法来找到
具有长x值的元素的等级(即索引)?
目前我正在迭代通过地图并记录我什么时候点击它。
Hi,

Given a map:
typedef map<long, string, greater<long> > mapOfFreq;

Is there a quicker way to find the rank (i.e. index) of the elememt that
has the long value of x?
At the moment I''m iterating through the map and keeping count of when I
hit it.



查看std :: map的成员函数,特别是find。

你的算法是O(n),成员函数可能是O(log2 n)。

Ben Pope



Check out the member functions of std::map, notably, find.

Your algorithm is O(n), the member function is probably O(log2 n).

Ben Pope




谢谢,但是那个返回元素的迭代器。我不确定如何帮助我确定其索引。


Steve



Thanks, but that returns an iterator to the element. I''m not sure how
this helps me determine its index.

Steve


Steve Edwards写道:
Steve Edwards wrote:


给出一张地图:
typedef map< long,string,greater< long> > mapOfFreq;

有没有更快的方法来找到
具有长x值的元素的等级(即指数)?
Hi,

Given a map:
typedef map<long, string, greater<long> > mapOfFreq;

Is there a quicker way to find the rank (i.e. index) of the elememt that
has the long value of x?




std :: map<>是一个关联容器,元素基于

一些比较运算符排序。换句话说,它并不意味着

任何可以找到索引的东西。 std :: map<>中的元素 - (你不能

真的在给定位置的地图中插入一个元素,不像说,

std :: vector<>,你可以在哪里) 。


如果用index这个词你实际上是指迭代器对于元素,

然后成员函数find()就可以了。这个函数需要

a键并将迭代器返回到带有该键的元素(如果没有这样的元素,则返回

迭代器到end())



std::map<> is an associative container with elements sorted based on
some comparison operator. In other words, it doesnot really mean
anything to find "index" of an element in std::map<> - (You cannot
really insert an element in a map at a "given position", unlike say,
std::vector<>, where you can).

If by the word "index" you actually mean the "iterator" to the element,
then the member function find() will do the trick. This function takes
a key and returns the iterator to the element with that key (and the
iterator to end() otherwise if no such element exists)


这篇关于查找&lt; map&gt;排名的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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