count和count_if算法返回类型似乎不足以容纳大值 [英] count and count_if algorithms return type seem to insufficient forlarge values

查看:59
本文介绍了count和count_if算法返回类型似乎不足以容纳大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我对计数算法的理解。


返回计数类型和count_if算法是

iterator_traits< InputIterator> :: difference_type。


如果容器包含多个''difference_type''元素

满足条件,那么count和count_if算法不能

返回一个值大于''difference_type''。


例如,假设''difference_type''的最大值是INT_MAX。

然后相应的最大值''size_type''是UINT_MAX。现在

考虑声明

vector< charv;

其中''v''包含UINT_MAX元素,每个元素都是''a'' 。


现在,如果我这样做了
count(v.begin(),v.end(),''a'')

然后实际计数将是UINT_MAX但是它无法表示

''difference_type''这是计数算法的返回类型。


所以''我们不会从计数算法中得到错误的结果吗?


我明白difference_type与ptrdiff_t相同

指针(属于同一个) array和size_type如果与size_t相同。

这意味着计数算法永远不会从

ptrdiff_t + 1 upto size_t返回值。


这种理解是否正确?


如果我提到的是计算算法确实存在问题,

我应该如何计算容器满足

条件f或大量元素(即大于

difference_type)?


请澄清。


谢谢

V.Subramanian

解决方案

su ************** @ yahoo.com ,印度写道:


以下是我对计数算法的理解。


返回计数类型和count_if算法是

iterator_traits< InputIterator> :: difference_type。


如果容器包含多个''difference_type''元素

满足条件,则count和count_if算法不能

返回大于''difference_type''。


例如,假设''difference_type''的最大值是INT_MAX。

然后相应的最大值'' size_type''是UINT_MAX。现在

考虑声明

vector< charv;

其中''v''包含UINT_MAX元素,每个元素都是''a'' 。


现在,如果我这样做了
count(v.begin(),v.end(),''a'')

然后实际计数将是UINT_MAX但是它无法表示

''difference_type''这是计数算法的返回类型。


所以''我们不会从计数算法中得到错误的结果吗?


我明白difference_type与ptrdiff_t相同

指针(属于同一个) array和size_type如果与size_t相同。

这意味着计数算法永远不会从

ptrdiff_t + 1 upto size_t返回值。


这种理解是否正确?


如果我提到的是计算算法确实存在问题,

我应该如何计算容器满足la的

条件rge元素数量(即大于

difference_type)?


请澄清。


谢谢

V.Subramanian



理论上,你可能是对的,但在实践中,这将是一个

包含的向量40亿条目,包含一个向量所具有的所有开销。


从实用的角度讲,你不必担心溢出问题。


-

Daniel Pitts的'科技博客:< http://virtualinfinity.net/wordpress/>


Daniel Pitts写道:

su ************** @ yahoo.com ,印度写道:


>以下是我的了解计数算法。

计数和count_if算法的返回类型是
iterator_traits< InputIterator> :: difference_type。

如果容器包含ns超过''difference_type''元素
满足条件,那么count和count_if算法不能返回大于''difference_type''的值。

例如,假设''difference_type''的最大值是INT_MAX。
那么''size_type''的相应最大值是UINT_MAX。现在
考虑声明
vector< charv;
其中''v''包含UINT_MAX元素,每个元素都是'a''。

现在如果我do
count(v.begin(),v.end(),'a'')
那么实际的计数将是UINT_MAX但是它不能在''difference_type'中表示
'这是计数算法的返回类型。

所以''我们不会从计数算法中得到错误的结果吗?

我知道difference_type与ptrdiff_t相同
指针(属于同一个数组)和size_type(如果与size_t相同)。
这意味着计数算法永远不会返回值来自ptrdiff_t + 1到size_t。

如果我提到的是计算算法确实存在问题,那么我应该如何计算满足大条件的容器中的元素?元素数量(即大于
difference_type)?

请澄清。

谢谢
V.Subramanian



从理论上讲,你可能是对的,但在实践中这将是一个矢量

包含40亿个条目,并且每个条目都有一个向量具有的所有开销。



实际上,在我所知的所有实现中,每个条目的向量都没有开销。

由于它存储了它,它有一个小的常量开销大小和容量。


从实用角度讲,你不必担心溢出问题。



同意。

最佳


Kai-Uwe Bux


On 2008-04-26 22:35:48 -0400,su ************** @ yahoo.com,India

< su ************** @ yahoo.comsaid:


以下是我对计数算法的理解。


返回计数类型和count_if算法是

iterator_traits< InputIterator> :: difference_type。


如果容器包含多个''difference_type''元素

满足条件,则count和count_if算法不能

返回大于''difference_type''的值。


例如,假设''difference_type''的最大值是INT_MAX。

那么''size_type''的相应最大值是UINT_MAX。现在

考虑声明

vector< charv;

其中''v''包含UINT_MAX元素,每个元素都是''a'' 。



您始终可以创建无符号整数值,这些值没有

对应的有符号值。同样,您始终可以创建没有相应无符号值的已签名

整数值。那是一个众所周知且无法解决的size_t和ptrdiff_t限制,以及

其他试图谈论尺寸和差异的东西。

-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_traits<InputIterator>::difference_type.

If the container contains more than ''difference_type'' elements
satisfying the condition, then count and count_if algorithm cannot
return a value greater than ''difference_type''.

As an example, suppose maximum value of ''difference_type'' is INT_MAX.
Then the corresponding maximum value of ''size_type'' is UINT_MAX. Now
consider the declaration
vector<charv;
where ''v'' contains UINT_MAX elements each of which is ''a''.

Now if I do
count(v.begin(), v.end(), ''a'')
then the actual count would be UINT_MAX but it cannot be represented
in ''difference_type'' which is the return type of count algorithm.

So'' won''t we get erroneous results from count algorithms ?

I understand that difference_type is same as ptrdiff_t for
pointers(belonging to the same array) and size_type if same as size_t.
This means that the count algorithms can never return values from
ptrdiff_t + 1 upto size_t.

Is this understanding correct ?

If what I have mentioned is really a problem with count algorithms,
how should I count the elements in a container satisfying the
condition for large number of elements(ie greater than
difference_type) ?

Kindly clarify.

Thanks
V.Subramanian

解决方案

su**************@yahoo.com, India wrote:

Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_traits<InputIterator>::difference_type.

If the container contains more than ''difference_type'' elements
satisfying the condition, then count and count_if algorithm cannot
return a value greater than ''difference_type''.

As an example, suppose maximum value of ''difference_type'' is INT_MAX.
Then the corresponding maximum value of ''size_type'' is UINT_MAX. Now
consider the declaration
vector<charv;
where ''v'' contains UINT_MAX elements each of which is ''a''.

Now if I do
count(v.begin(), v.end(), ''a'')
then the actual count would be UINT_MAX but it cannot be represented
in ''difference_type'' which is the return type of count algorithm.

So'' won''t we get erroneous results from count algorithms ?

I understand that difference_type is same as ptrdiff_t for
pointers(belonging to the same array) and size_type if same as size_t.
This means that the count algorithms can never return values from
ptrdiff_t + 1 upto size_t.

Is this understanding correct ?

If what I have mentioned is really a problem with count algorithms,
how should I count the elements in a container satisfying the
condition for large number of elements(ie greater than
difference_type) ?

Kindly clarify.

Thanks
V.Subramanian

In theory, you may be right, but in practice that would be a vector that
contains 4 billion entries, with all the overhead that a vector has per
entry.

Pragmatically speaking, you won''t have to worry about overflow problems.

--
Daniel Pitts'' Tech Blog: <http://virtualinfinity.net/wordpress/>


Daniel Pitts wrote:

su**************@yahoo.com, India wrote:

>Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_traits<InputIterator>::difference_type .

If the container contains more than ''difference_type'' elements
satisfying the condition, then count and count_if algorithm cannot
return a value greater than ''difference_type''.

As an example, suppose maximum value of ''difference_type'' is INT_MAX.
Then the corresponding maximum value of ''size_type'' is UINT_MAX. Now
consider the declaration
vector<charv;
where ''v'' contains UINT_MAX elements each of which is ''a''.

Now if I do
count(v.begin(), v.end(), ''a'')
then the actual count would be UINT_MAX but it cannot be represented
in ''difference_type'' which is the return type of count algorithm.

So'' won''t we get erroneous results from count algorithms ?

I understand that difference_type is same as ptrdiff_t for
pointers(belonging to the same array) and size_type if same as size_t.
This means that the count algorithms can never return values from
ptrdiff_t + 1 upto size_t.

Is this understanding correct ?

If what I have mentioned is really a problem with count algorithms,
how should I count the elements in a container satisfying the
condition for large number of elements(ie greater than
difference_type) ?

Kindly clarify.

Thanks
V.Subramanian

In theory, you may be right, but in practice that would be a vector that
contains 4 billion entries, with all the overhead that a vector has per
entry.


Actually, in all implementations I know, a vector has no overhead per entry.
It has a small constant overhead since it stores its size and the capacity.

Pragmatically speaking, you won''t have to worry about overflow problems.

Agreed.
Best

Kai-Uwe Bux


On 2008-04-26 22:35:48 -0400, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:

Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_traits<InputIterator>::difference_type.

If the container contains more than ''difference_type'' elements
satisfying the condition, then count and count_if algorithm cannot
return a value greater than ''difference_type''.

As an example, suppose maximum value of ''difference_type'' is INT_MAX.
Then the corresponding maximum value of ''size_type'' is UINT_MAX. Now
consider the declaration
vector<charv;
where ''v'' contains UINT_MAX elements each of which is ''a''.

You can always create unsigned integer values that have no
corresponding signed value. Similarly, you can always create signed
integer values that have no corresponding unsigned value. That''s a
well-known and unsolvable limitation of size_t and ptrdiff_t, and
anything else that tries to talk about sizes and differences.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于count和count_if算法返回类型似乎不足以容纳大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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