STL地图:排序选项? [英] STL Map: Sorting options?

查看:47
本文介绍了STL地图:排序选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用带字符串类型键的地图,类型为int的值。


typedef map< string,int,less< string> >一致性;


我在某些文字中找到了单词,并记录了他们的

频率。

我我是STL的新手,但我刚读过标题,看不到按价值对结果进行排序的

方式。 (也许有一种方法可以修改较少的<>?)


或者...我应该创建第二个多图(副本)并将int作为

键,字符串作为值,然后按照惯例保持排序。

或者我只是使用不适当的容器类型开始?


由于我正在处理非常大的文本文件,速度也是一个问题。


谢谢


Steve

Hi,

I''m using a map with the key of type string, and value of type int.

typedef map<string, int, less<string> >concordance;

I''m finding words within some text and keeping a count of their
frequency.
I''m new to STL, but I''ve just read through the headers and can''t see a
way to sort the results by value. (Maybe there''s a way to modify less<>?)

Or... Should I create a second multimap (a copy) and have the int as the
key, and the string as the value, and then trivaially keep that sorted.
Or am I just using inappropriate container types to start with?

As I''m dealing with very large text files, speed is a concern too.

Thanks

Steve

推荐答案



Steve Edwards写道:

Steve Edwards wrote:


我'使用带有string类型的键的映射,以及int类型的值。

typedef map< string,int,less< string> >一致性;

我在某些文字中找到了单词并记录了它们的频率。

我是STL的新手,但是我刚读过标题,看不到按值对结果进行排序的方法。 (也许有一种方法可以修改更少的<>?)


"结果"这里有一个误导性的词。 std :: map< Key,Value>用于

保持

每个键的一个(复合)值。它是一个容器,容器没有


结果。函数有结果=返回值。

或者......我应该创建第二个多图(副本)并将int作为
键,并将字符串作为值,然后平凡地保持那种排序。


这是一个解决方案,如果你经常需要那个代表,那么



和价值观'不要改变。 (基本缓存模式)。

或者我刚开始使用不适当的容器类型?


这是确定int值时使用的好容器,但它可能不是
可能不是
是最好的容器保持这些int值。

由于我正在处理非常大的文本文件,速度也是一个问题。
Hi,

I''m using a map with the key of type string, and value of type int.

typedef map<string, int, less<string> >concordance;

I''m finding words within some text and keeping a count of their
frequency.
I''m new to STL, but I''ve just read through the headers and can''t see a
way to sort the results by value. (Maybe there''s a way to modify less<>?)
"Results" is a misleading word here. std::map<Key, Value> is used to
keep
one (composite) value per key. It''s a container, and containers do not
have
results. Functions have results=return values.
Or... Should I create a second multimap (a copy) and have the int as the
key, and the string as the value, and then trivaially keep that sorted.
That''s one solution, if you often need that representation too, and the
keys
and values don''t change. (basic caching pattern).
Or am I just using inappropriate container types to start with?
It''s a good container to use when determining the int values, but it
may not
be the best container to keep those int values in.
As I''m dealing with very large text files, speed is a concern too.




速度什么?建立集合,还是使用它?如果是后者,

如何?

(如果你说文件,你通常也会受到I / O的限制)


HTH,

Michiel Salters



Speed of what? Building the collection, or using it? And if the latter,
how?
(And if you talk files, you often are I/O limited anyway)

HTH,
Michiel Salters


文章< gf ************* **********@news.btinternet.com>,

Steve Edwards< gf*@lineone.net>写道:
In article <gf***********************@news.btinternet.com>,
Steve Edwards <gf*@lineone.net> wrote:


我正在使用带有string类型键的地图,类型为int的值。

typedef map< string,int,less< string> >一致性;

我在某些文字中找到了单词并记录了它们的频率。

我是STL的新手,但是我刚读过标题,看不到按值对结果进行排序的方法。 (也许有一种方法可以修改较少的<>?)或者......或者......我应该创建第二个多图(副本)并将int作为
键,并且将字符串作为值,然后将其保持整齐排序。

或者我只是使用不适当的容器类型开始?

因为我正在处理非常大的文本文件,速度也是一个问题。
Hi,

I''m using a map with the key of type string, and value of type int.

typedef map<string, int, less<string> >concordance;

I''m finding words within some text and keeping a count of their
frequency.
I''m new to STL, but I''ve just read through the headers and can''t see a
way to sort the results by value. (Maybe there''s a way to modify less<>?)

Or... Should I create a second multimap (a copy) and have the int as the
key, and the string as the value, and then trivaially keep that sorted.
Or am I just using inappropriate container types to start with?

As I''m dealing with very large text files, speed is a concern too.




我想的不是多图,可以使用map< int,set< string> >"

这样,字符串将保持按字母顺序排列。


-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验来纠正。



I''m think rather than a multimap, one could use "map<int, set<string> >"
That way, the strings will stay alphabetized.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


我是STL的新手,但是我刚读过标题,看不到按值对结果进行排序的方法。 (也许有一种方法可以修改
少于<>?)
I''m new to STL, but I''ve just read through the headers and can''t see a
way to sort the results by value. (Maybe there''s a way to modify
less<>?)
" Results"这里有一个误导性的词。 std :: map< Key,Value>用于保持每个键一个(复合)值。它是一个容器,容器没有


我的错误......我的意思是内容。

我正在处理对于非常大的文本文件,速度也是一个问题。
"Results" is a misleading word here. std::map<Key, Value> is used to
keep
one (composite) value per key. It''s a container, and containers do not
have
My mistake... I meant contents.
As I''m dealing with very large text files, speed is a concern too.



速度是多少?建立集合,还是使用它?如果是后者,
怎么样?
(如果你说文件,你通常都是I / O限制的)


Speed of what? Building the collection, or using it? And if the latter,
how?
(And if you talk files, you often are I/O limited anyway)




我''已经将我的大文本文件作为一个简单的数组加载到内存中

字符串(每个单词/标记一个)。我正在对

这些数据进行各种词汇分析。对于其中一些功能,我需要快速查找

字作为键(因此我选择< map>)来获取相关数据;

其他函数然后要求映射到键的值可以按顺序快速检索到
(因此我的原始问题)。


自从阅读你的回复我已经将一个副本构建为一个多重映射,并且交换了

键/值,并且将两个容器一起使用似乎是很快就可以通过键或值快速检索。

(虽然我没有另外一种比较速度的策略,所以

谁知道。)


建立结构现在自然变慢了,但它是一个可接受的权衡。


---------------- --------


在我的原始地图中


typedef map< string,int,less< string> >一致性;


每当我发现另一个字符串键出现时,是否有更快的方法来增加它的值计数, :


myConcordance [theWord] = myConcordance [theWord] +1;


看来我正在做2次查找[theWord] ,我可以在原地更改值

吗?


感谢您的帮助。



I''ve already loaded my large text file in to memory as a simple array
of strings (one per word/token). I''m doing various lexical analyses on
these data. For some of these functions I need to quickly look up the
word as the key (hence my choice of <map>) to get the associated data;
other functions then require that the value mapped to the key can be
retrieved rapidly in order (hence my original question).

Since reading your reply I''ve built a copy as a multimap with the
key/value swapped, and using both containers together seems to be
working quite well in retrieving by either key or value rapidly.
(Though I don''t have an alternative strategy to compare speed with, so
who knows.)

Building the structures is naturally slower, now, but it is an
acceptable tradeoff.

------------------------

In my original map

typedef map<string, int, less<string> >concordance;

every time I find another occurrence of the string key, is there a
quicker way to increment it''s value count, than:

myConcordance[theWord] = myConcordance [theWord]+1;

It seems I''m doing 2 lookups of [theWord], can I change the value
in-place instead?

Thanks for your help.


这篇关于STL地图:排序选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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