check values in vector

C

coolchap

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

thanks in advance
Prashant
 
V

Victor Bazarov

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
 
S

Saeed Amrollahi

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

thanks in advance
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
 
V

Victor Bazarov

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

thanks in advance
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
 
J

Juha Nieminen

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

thanks in advance
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.
 
R

Rolf Magnus

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

thanks in advance
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.
 
P

Paul

Rolf Magnus said:
Juha said:
Victor Bazarov said:
On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
Hello,
Is there a quick and efficient way to check if all the
values
in a vector are the same ?

thanks in advance
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
want your data sorted :).
 
A

Anand Hariharan

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

thanks in advance
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

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.
 
D

Dombo

Op 11-Mar-11 21:45, Rolf Magnus schreef:
Juha said:
Victor Bazarov said:
On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
Hello,
Is there a quick and efficient way to check if all the values
in a vector are the same ?

thanks in advance
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top