473,772 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count and count_if algorithms return type seem to insufficient forlarge values

Below is my understanding about count algorithms.

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

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

As an example, suppose maximum value of 'difference_typ e' 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_typ e' 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(belong ing 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
Jun 27 '08 #1
12 2315
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_ty pe.

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

As an example, suppose maximum value of 'difference_typ e' 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_typ e' 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(belong ing 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/>
Jun 27 '08 #2
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_trait s<InputIterator >::difference_t ype.

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

As an example, suppose maximum value of 'difference_typ e' 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_typ e' 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(belon ging 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_typ e) ?

Kindly clarify.

Thanks
V.Subramania n
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
Jun 27 '08 #3
On 2008-04-26 22:35:48 -0400, "su************ **@yahoo.com, India"
<su************ **@yahoo.comsai d:
Below is my understanding about count algorithms.

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

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

As an example, suppose maximum value of 'difference_typ e' 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)

Jun 27 '08 #4
Pete Becker wrote:
On 2008-04-26 22:35:48 -0400, "su************ **@yahoo.com, India"
<su************ **@yahoo.comsai d:
>Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_trait s<InputIterator >::difference_t ype.

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

As an example, suppose maximum value of 'difference_typ e' 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.
In the special case of differences between iterators and sizes of
containers, the problem seems solvable: the standard could require (a) the
existence of a signed type sufficiently big to hold all differences between
iterators that can occur on the given implementation and (b) require that
ptrdiff_t and the various difference_type are typedefs for such signed
type.

On a related note: it is even guaranteed that push_back() on a vector cannot
make its size overflow to 0?
Best

Kai-Uwe Bux
Jun 27 '08 #5
On 2008-04-27 08:05:14 -0400, Kai-Uwe Bux <jk********@gmx .netsaid:
Pete Becker wrote:
>On 2008-04-26 22:35:48 -0400, "su************ **@yahoo.com, India"
<su*********** ***@yahoo.comsa id:
>>Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_trai ts<InputIterato r>::difference_ type.

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

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

You can always create unsigned integer values that have no
correspondin g 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.

In the special case of differences between iterators and sizes of
containers, the problem seems solvable: the standard could require (a) the
existence of a signed type sufficiently big to hold all differences between
iterators that can occur on the given implementation and (b) require that
ptrdiff_t and the various difference_type are typedefs for such signed
type.
What that amounts to is artificially limiting the size of a container
to the non-negative values of a signed type. If you do that, surely
someone will complain that they want their containers to be larger, and
don't care about difference_type .

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

Jun 27 '08 #6

"Pete Becker" <pe**@versatile coding.comwrote in message
news:2008042710 290416807-pete@versatilec odingcom...
On 2008-04-27 08:05:14 -0400, Kai-Uwe Bux <jk********@gmx .netsaid:
>Pete Becker wrote:
>>On 2008-04-26 22:35:48 -0400, "su************ **@yahoo.com, India"
<su********** ****@yahoo.coms aid:

Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_tra its<InputIterat or>::difference _type.

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

As an example, suppose maximum value of 'difference_typ e' 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
correspondi ng 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.

In the special case of differences between iterators and sizes of
containers, the problem seems solvable: the standard could require (a)
the
existence of a signed type sufficiently big to hold all differences
between
iterators that can occur on the given implementation and (b) require that
ptrdiff_t and the various difference_type are typedefs for such signed
type.

What that amounts to is artificially limiting the size of a container to
the non-negative values of a signed type. If you do that, surely someone
will complain that they want their containers to be larger, and don't care
about difference_type .
The real problem is that containers have excessive requirements. arrays and
lookups dont in practise need to be iterable at all, but you can still write
generic algorithms. Remove the iterator paradigm and do for_each (container)
in parallel for example. STL was a major innovator for generic programming
but way way far from perfect.... Too many blooming iterators.

regards
Andy Little
Jun 27 '08 #7
On 2008-04-27 11:43:09 -0400, "kwikius" <an**@servocomm .freeserve.co.u ksaid:
>
The real problem is that containers have excessive requirements. arrays and
lookups dont in practise need to be iterable at all, but you can still write
generic algorithms. Remove the iterator paradigm and do for_each (container)
in parallel for example. STL was a major innovator for generic programming
but way way far from perfect.... Too many blooming iterators.
Without iterators STL is something entirely different. Container-based
algorithms are nowhere near as flexible as iterator-based ones, because
the sequences that iterators refer to don't need to come from
containers.

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

Jun 27 '08 #8
Kai-Uwe Bux wrote:
Pete Becker wrote:
>On 2008-04-26 22:35:48 -0400, "su************ **@yahoo.com, India"
<su*********** ***@yahoo.comsa id:
>>Below is my understanding about count algorithms.

Return type of count and count_if algorithms is
iterator_trai ts<InputIterato r>::difference_ type.

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

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

You can always create unsigned integer values that have no
correspondin g 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.

In the special case of differences between iterators and sizes of
containers, the problem seems solvable: the standard could require
(a) the existence of a signed type sufficiently big to hold all
differences between iterators that can occur on the given
implementation and (b) require that ptrdiff_t and the various
difference_type are typedefs for such signed type.
Isn't that what happens in practice?

On the most popular 32 bit systems, you would have to try extremely
hard to allocate a vector<charthat uses more than half the available
address space. For any other types, sizes and ptrdiff values are much
smaller.
Bo Persson


Jun 27 '08 #9

"Pete Becker" <pe**@versatile coding.comwrote in message
news:2008042711 481916807-pete@versatilec odingcom...
On 2008-04-27 11:43:09 -0400, "kwikius" <an**@servocomm .freeserve.co.u k>
said:
>>
The real problem is that containers have excessive requirements. arrays
and
lookups dont in practise need to be iterable at all, but you can still
write
generic algorithms. Remove the iterator paradigm and do for_each
(container)
in parallel for example. STL was a major innovator for generic
programming
but way way far from perfect.... Too many blooming iterators.

Without iterators STL is something entirely different. Container-based
algorithms are nowhere near as flexible as iterator-based ones, because
the sequences that iterators refer to don't need to come from containers.
Most containers arent sequences... except in STL "every container must be a
list". This is a viewpoint imposed by the iterator paradigm.
Custom conforming containers are very complicated to create as need to
provide full custom iterator shebang.

Rather than creating iterators it is just as possible to create views of
arbitrary entities as containers. You only need to create one container view
( as opposed to 2 iterators to create 2 ends of a container view ).

Containers can have simpler requirements... easier to fulfill requirements
and create custom containers

It is also possible to view one container type as another.
increases flexibility for the user and also enables the user to create
custom
containers more easily because each container type has fewer requirements.

But the most important point is it removes the necessity of exposing the
blooming iterators in every algorithm, which makes source code less
expressive :

fun(my_containe r, your_container , f) ; //overloaded or internally adapted

as against

fun(my_containe r.begin(), my_container.en d(), my_container.be gin(),
your_container. end(), f) ; //error?

The loser may be the library implementor of course, but library author to
user is one to many ratio so total code written reduced.
OTOH the library implementor has more scope to optimise because he doesnt
need necessarily to conform internal to the function to the iterator
paradigm.

regards
Andy Little



Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2712
by: JohanS | last post by:
Hi count(v.begin(), v.end(), 5) is easy for a vector of int's. But how do i make it work with a class object? I have tried a few ways but can't get it to work since i don't really understand how this stuff works yet. If i have class A with 2 int members that you get with A.x() and A.y() and i want to use count or count_if if A.y() == value.
20
2979
by: none | last post by:
I have managed to get the below script *almost* working. However, it still has a problem calculating the number of months. The date I am trying to calculate from is Oct 15, 1994. With the correct "thatmonth" (10) it displays 0. With 9 it displays 2, instead of 1 which would be correct. Any suggestions? === Cut === <script language=Javascript type=text/javascript class="smalltext">
0
4101
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL, TYPE VARCHAR(30) NOT NULL, DURATION TIME , YEAR_RELEASE YEAR NOT NULL, DESCRIPTION TEXT,
23
2903
by: Gary Wessle | last post by:
Hi I have a vector<charwhich looks like this (a d d d a d s g e d d d d d k) I need to get the biggest count of consecutive 'd'. 5 in this example I am toying with this method but not sure if it is optimal. thanks int k = 0;
22
12493
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
2
12081
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field contains "AAA" the count is X. if the field contains "CCC" the count is Y. if the field contains "Stop" then count is Z. I have tried several ways and can not seem to get any where. I get the same count for all string values. Can some one...
1
1733
by: zambia | last post by:
First post so please excuse any Faux Pas's I have an xml file as below <?xml version="1.0"?> <Car> <Type>Ford Sierra</Type> <Service>1 <Date>01/09/2006</Date> <Tyres>OK</Tyres>
4
2485
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. Here's the approach I'm currently looking at. I throw exceptions only from constructors. Destructors, of course, do not throw exceptions. All other functions return a signed integer. The values are all stored in one large header file (as...
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.