Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   check values in vector (http://www.velocityreviews.com/forums/t744845-check-values-in-vector.html)

 coolchap 03-09-2011 02:27 PM

check values in vector

Hello,
Is there a quick and efficient way to check if all the values
in a vector are the same ?

Prashant

 Victor Bazarov 03-09-2011 02:36 PM

Re: check values in vector

On 3/9/2011 9:27 AM, coolchap wrote:
> Is there a quick and efficient way to check if all the values
> in a vector are the same ?

Loop over the vector (starting from 1) and compare each value to the 0th
element (or to the previous one). That's O(N). Pretty efficient if you
ask me. As for quick... Depends on the size of the vector and the size
of the contained object and how the equality is calculated.

V
--

 Saeed Amrollahi 03-09-2011 05:02 PM

Re: check values in vector

On Mar 9, 5:27*pm, coolchap <b.prash...@gmail.com> wrote:
> Hello,
> * * * * *Is there a quick and efficient way to check if all thevalues
> in a vector are the same ?
>
> Prashant

Hi
In C++0x - the new revision of C++ - there are a few new generic
algorithms:
all_of, any_of and none_of. I believe, the all_of is what you want.
of course you have to use lambda expressions:

void f()
{
vector<int> v(10, 0); // a vector of 10 zero element
bool b = all_of(v.begin(), v.end() ,[](int a) { return a ==
0; }); // b == true
}

of course you can write a generic function without using lambda
functions
from scratch.

HTH,
-- Saeed Amrollahi

 Victor Bazarov 03-09-2011 05:27 PM

Re: check values in vector

On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
> On Mar 9, 5:27 pm, coolchap<b.prash...@gmail.com> wrote:
>> Hello,
>> Is there a quick and efficient way to check if all the values
>> in a vector are the same ?
>>
>> Prashant

>
> Hi
> In C++0x - the new revision of C++ - there are a few new generic
> algorithms:
> all_of, any_of and none_of. I believe, the all_of is what you want.
> of course you have to use lambda expressions:
>
> void f()
> {
> vector<int> v(10, 0); // a vector of 10 zero element
> bool b = all_of(v.begin(), v.end() ,[](int a) { return a ==
> 0; }); // b == true
> }
>
> of course you can write a generic function without using lambda
> functions
> from scratch.

A combination of 'find_if', 'bind2nd', 'not_equal_to' should give
similar result.

V
--

 Juha Nieminen 03-10-2011 08:02 AM

Re: check values in vector

Victor Bazarov <v.bazarov@comcast.invalid> wrote:
> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
>> On Mar 9, 5:27 pm, coolchap<b.prash...@gmail.com> wrote:
>>> Hello,
>>> Is there a quick and efficient way to check if all the values
>>> in a vector are the same ?
>>>
>>> Prashant

>>
>> Hi
>> In C++0x - the new revision of C++ - there are a few new generic
>> algorithms:
>> all_of, any_of and none_of. I believe, the all_of is what you want.
>> of course you have to use lambda expressions:
>>
>> void f()
>> {
>> vector<int> v(10, 0); // a vector of 10 zero element
>> bool b = all_of(v.begin(), v.end() ,[](int a) { return a ==
>> 0; }); // b == true
>> }
>>
>> of course you can write a generic function without using lambda
>> functions
>> from scratch.

>
> A combination of 'find_if', 'bind2nd', 'not_equal_to' should give
> similar result.

Just explicitly looping over the vector and comparing the elements will
result in a much shorter piece of code.

 Rolf Magnus 03-11-2011 08:45 PM

Re: check values in vector

Juha Nieminen wrote:

> Victor Bazarov <v.bazarov@comcast.invalid> wrote:
>> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
>>> On Mar 9, 5:27 pm, coolchap<b.prash...@gmail.com> wrote:
>>>> Hello,
>>>> Is there a quick and efficient way to check if all the values
>>>> in a vector are the same ?
>>>>
>>>> Prashant
>>>
>>> Hi
>>> In C++0x - the new revision of C++ - there are a few new generic
>>> algorithms:
>>> all_of, any_of and none_of. I believe, the all_of is what you want.
>>> of course you have to use lambda expressions:
>>>
>>> void f()
>>> {
>>> vector<int> v(10, 0); // a vector of 10 zero element
>>> bool b = all_of(v.begin(), v.end() ,[](int a) { return a ==
>>> 0; }); // b == true
>>> }
>>>
>>> of course you can write a generic function without using lambda
>>> functions
>>> from scratch.

>>
>> A combination of 'find_if', 'bind2nd', 'not_equal_to' should give
>> similar result.

>
> Just explicitly looping over the vector and comparing the elements will
> result in a much shorter piece of code.

And it will result in a much shorter time to write the code.

 Paul 03-11-2011 09:48 PM

Re: check values in vector

"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:ile1m5\$2in\$01\$1@news.t-online.com...
> Juha Nieminen wrote:
>
>> Victor Bazarov <v.bazarov@comcast.invalid> wrote:
>>> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
>>>> On Mar 9, 5:27 pm, coolchap<b.prash...@gmail.com> wrote:
>>>>> Hello,
>>>>> Is there a quick and efficient way to check if all the
>>>>> values
>>>>> in a vector are the same ?
>>>>>
>>>>> Prashant
>>>>
>>>> Hi
>>>> In C++0x - the new revision of C++ - there are a few new generic
>>>> algorithms:
>>>> all_of, any_of and none_of. I believe, the all_of is what you want.
>>>> of course you have to use lambda expressions:
>>>>
>>>> void f()
>>>> {
>>>> vector<int> v(10, 0); // a vector of 10 zero element
>>>> bool b = all_of(v.begin(), v.end() ,[](int a) { return a ==
>>>> 0; }); // b == true
>>>> }
>>>>
>>>> of course you can write a generic function without using lambda
>>>> functions
>>>> from scratch.
>>>
>>> A combination of 'find_if', 'bind2nd', 'not_equal_to' should give
>>> similar result.

>>
>> Just explicitly looping over the vector and comparing the elements will
>> result in a much shorter piece of code.

>
> And it will result in a much shorter time to write the code.
>

If you are doing it more than once it may be worth sorting the vector and
then just compare first and last elements.
If you sort in such a way than nothing is moved if all elements are the same
, there would be no overhead on these sorts, compare to a looping and
comparing methods explained..
You get your data sorted into the bargain, obvioulsy its n/a if you don't

 Anand Hariharan 03-12-2011 01:22 AM

Re: check values in vector

On Mar 9, 8:27*am, coolchap <b.prash...@gmail.com> wrote:
> Hello,
> * * * * *Is there a quick and efficient way to check if all thevalues
> in a vector are the same ?
>
> Prashant

This does not care for what the container is, and hence not the most
efficient:

size_t NumElem = std::distance(it1, it2);
if ( NumElem > 1UL )
return NumElem == std::count( it1, it2, *it1 );
else
return true;

 Öö Tiib 03-12-2011 01:55 AM

Re: check values in vector

On Mar 12, 3:22*am, Anand Hariharan <mailto.anand.hariha...@gmail.com>
wrote:
> On Mar 9, 8:27*am, coolchap <b.prash...@gmail.com> wrote:
>
> > Hello,
> > * * * * *Is there a quick and efficient way to check if all the values
> > in a vector are the same ?

>
> > Prashant

>
> This does not care for what the container is, and hence not the most
> efficient:
>
> size_t NumElem = std::distance(it1, it2);

NumElem has most wrong type here. It should be either
iterator_traits<It>::difference_type or int when writer does not care
about strong typing. As result one can't write error diagnosing to the
next line ... like:

assert( NumElem >= 0 );

> if ( NumElem > 1UL )

Even if the first error was fixed that "1UL" would lead to undefined
behavior anyway by converting negative value of NumElem to large
unsigned.

> * return NumElem == std::count( it1, it2, *it1 );
> else
> * return true;

 Dombo 03-13-2011 03:55 PM

Re: check values in vector

Op 11-Mar-11 21:45, Rolf Magnus schreef:
> Juha Nieminen wrote:
>
>> Victor Bazarov<v.bazarov@comcast.invalid> wrote:
>>> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
>>>> On Mar 9, 5:27 pm, coolchap<b.prash...@gmail.com> wrote:
>>>>> Hello,
>>>>> Is there a quick and efficient way to check if all the values
>>>>> in a vector are the same ?
>>>>>
>>>>> Prashant
>>>>
>>>> Hi
>>>> In C++0x - the new revision of C++ - there are a few new generic
>>>> algorithms:
>>>> all_of, any_of and none_of. I believe, the all_of is what you want.
>>>> of course you have to use lambda expressions:
>>>>
>>>> void f()
>>>> {
>>>> vector<int> v(10, 0); // a vector of 10 zero element
>>>> bool b = all_of(v.begin(), v.end() ,[](int a) { return a ==
>>>> 0; }); // b == true
>>>> }
>>>>
>>>> of course you can write a generic function without using lambda
>>>> functions
>>>> from scratch.
>>>
>>> A combination of 'find_if', 'bind2nd', 'not_equal_to' should give
>>> similar result.

>>
>> Just explicitly looping over the vector and comparing the elements will
>> result in a much shorter piece of code.

>
> And it will result in a much shorter time to write the code.

And for the average C++ programmer also a much shorter time to
understand the code.

 All times are GMT. The time now is 04:26 PM.