使用float作为STL映射中的索引? [英] using a float as the index in an STL map?

查看:72
本文介绍了使用float作为STL映射中的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




似乎使用花车作为STL地图的第一个tupelo(显示为

以下)可能会导致问题,但我不知道记住问题是什么。

使用如下结构是否常见?如果你能分享你使用这种结构的经验,我将不胜感激

。谢谢。


std :: map< float,intm;


JD

Hi,

It seems that using floats as the first tupelo for an STL map (shown
below) can cause problems but I don''t remember what the problems were.
Is it common practice to use a structure like below? I would appreciate
if you can share your experience using such a structure. Thanks.

std::map<float, intm;

JD

推荐答案

JDT写道:
JDT wrote:

似乎使用浮动作为STL地图的第一个tupelo(显示

以下)可能会导致问题,但我不记得是什么问题。
It seems that using floats as the first tupelo for an STL map (shown
below) can cause problems but I don''t remember what the problems were.



我唯一能想到的是,如果你期望两个不同的
计算结果导致相同的数字(和最终

与地图值中存储的相同),您可能会感到意外。

由于FPU中的舍入错误,数学上相当于

计算可以在内部导致不同的数字。


例如,假设''''是1.f而''b''是2.f,表达式

(a + 2.f / 3)和(b - 1.f / 3)_can_给你不同的结果。

The only thing I can think of is that if you expect two different
results of a calculation to lead to the same number (and ultimately
to the same stored in the map value), you may be in for a surprise.
Due to rounding errors in the FPU the mathematically equivalent
calculations can lead to different numbers internally.

Example, given that ''a'' is 1.f and ''b'' is 2.f, the expressions
(a + 2.f/3) and (b - 1.f/3) _can_ give you different results.


是吗使用如下结构的常见做法?如果你可以分享你使用这种结构的经验,我会很高兴。

谢谢。

std :: map< float,intm;
Is it common practice to use a structure like below? I would
appreciate if you can share your experience using such a structure.
Thanks.
std::map<float, intm;



我_never_看到''map''其中''float''将是Key类型。我不能说b $ b声称已经看到了世界上所有的代码,甚至是一个重要的部分

,所以我无法证明共性。练习。


V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问

I _never_ saw a ''map'' where ''float'' would be the Key type. I cannot
claim to have seen all code in the world, and even a significant part
of it, so I cannot attest to "commonality" of the practice.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

例如,假设''''是1.f而''b''是2.f,表达式

(a + 2.f / 3)和(b - 1.f / 3)_can_给你不同的结果。
Example, given that ''a'' is 1.f and ''b'' is 2.f, the expressions
(a + 2.f/3) and (b - 1.f/3) _can_ give you different results.



具体示例:


double d1 = 1.0;

double d2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;

std :: cout<< d1<< " " << d2<< " " << (d1 == d2)<< std :: endl;


打印(至少在普通电脑中):


1 1 0

打印带有更多小数位的双打将显示

的差异。它非常小,但它就在那里,因此(d1 == d2)是

false。

Concrete example:

double d1 = 1.0;
double d2 = 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
std::cout << d1 << " " << d2 << " " << (d1 == d2) << std::endl;

That prints (at least in a regular PC):

1 1 0

Printing the doubles with more decimal places would show the
difference. It''s very small, but it''s there, thus (d1 == d2) is
false.


Victor Bazarov写道:
Victor Bazarov wrote:

JDT写道:
JDT wrote:

>>似乎使用浮动作为第一个tupelo STL地图(如下所示)可能会导致问题,但我不记得问题是什么。
>>It seems that using floats as the first tupelo for an STL map (shown
below) can cause problems but I don''t remember what the problems were.




我唯一能想到的是,如果你期望两个不同的
计算结果导致相同的数字(最终

与地图值中存储的相同),您可能会感到意外。

由于FPU中的舍入错误,数学上等效

计算可以在内部产生不同的数字。


例如,假设''''是1.f而''b''是2.f ,表达式

(a + 2.f / 3)和(b - 1.f / 3)_can_给你不同的结果。



The only thing I can think of is that if you expect two different
results of a calculation to lead to the same number (and ultimately
to the same stored in the map value), you may be in for a surprise.
Due to rounding errors in the FPU the mathematically equivalent
calculations can lead to different numbers internally.

Example, given that ''a'' is 1.f and ''b'' is 2.f, the expressions
(a + 2.f/3) and (b - 1.f/3) _can_ give you different results.


>>通常的做法是使用如下所示的结构吗?如果你能用这样的结构分享你的经验,我会很感激。
谢谢。
std :: map< float,intm;
>>Is it common practice to use a structure like below? I would
appreciate if you can share your experience using such a structure.
Thanks.
std::map<float, intm;




我_never_看到''map''''float''将是Key类型。我不能说b $ b声称已经看到了世界上所有的代码,甚至是一个重要的部分

,所以我无法证明共性。这个练习。


V



I _never_ saw a ''map'' where ''float'' would be the Key type. I cannot
claim to have seen all code in the world, and even a significant part
of it, so I cannot attest to "commonality" of the practice.

V



你好Victor:


下面的场景,我认为使用float作为地图的关键使得

感觉。例如,下表需要按第1个元组的

升序排序(即值)。所以我可以简单地将两个对子插入地图中,然后自动获得一个排序对的列表。

人们经常有类似的需求,还是有其他更好的方法

能达到这个目的吗?非常感谢任何进一步的帮助。


价值物品数量

3.5 5

4.7 9

9.3 7

......


JD


Hi Victor:

In the following scenario, I think using float as the key to a map makes
sense. For example, the following table needs to be sorted in the
ascending order of the 1st tuple (i.e. Values). So I can simply insert
the pairs into a map and then get a list of sorted pairs automatically.
Do people often have similar needs or are there other better ways to
accomplish this purpose? Any further help is much appreciated.

Values # of items
3.5 5
4.7 9
9.3 7
......

JD



这篇关于使用float作为STL映射中的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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