check values in vector

Discussion in 'C++' started by coolchap, Mar 9, 2011.

  1. coolchap

    coolchap Guest

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

    thanks in advance
    Prashant
     
    coolchap, Mar 9, 2011
    #1
    1. Advertising

  2. 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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 9, 2011
    #2
    1. Advertising

  3. On Mar 9, 5:27 pm, coolchap <> wrote:
    > 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
     
    Saeed Amrollahi, Mar 9, 2011
    #3
  4. On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
    > On Mar 9, 5:27 pm, coolchap<> 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.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 9, 2011
    #4
  5. Victor Bazarov <> wrote:
    > On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
    >> On Mar 9, 5:27 pm, coolchap<> 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.
     
    Juha Nieminen, Mar 10, 2011
    #5
  6. coolchap

    Rolf Magnus Guest

    Juha Nieminen wrote:

    > Victor Bazarov <> wrote:
    >> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
    >>> On Mar 9, 5:27 pm, coolchap<> 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.
     
    Rolf Magnus, Mar 11, 2011
    #6
  7. coolchap

    Paul Guest

    "Rolf Magnus" <> wrote in message
    news:ile1m5$2in$01$-online.com...
    > Juha Nieminen wrote:
    >
    >> Victor Bazarov <> wrote:
    >>> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
    >>>> On Mar 9, 5:27 pm, coolchap<> 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 :).
     
    Paul, Mar 11, 2011
    #7
  8. On Mar 9, 8:27 am, coolchap <> wrote:
    > 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;
     
    Anand Hariharan, Mar 12, 2011
    #8
  9. coolchap

    Öö Tiib Guest

    On Mar 12, 3:22 am, Anand Hariharan <>
    wrote:
    > On Mar 9, 8:27 am, coolchap <> 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

    >
    > 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;
     
    Öö Tiib, Mar 12, 2011
    #9
  10. coolchap

    Dombo Guest

    Op 11-Mar-11 21:45, Rolf Magnus schreef:
    > Juha Nieminen wrote:
    >
    >> Victor Bazarov<> wrote:
    >>> On 3/9/2011 12:02 PM, Saeed Amrollahi wrote:
    >>>> On Mar 9, 5:27 pm, coolchap<> 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.
     
    Dombo, Mar 13, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    24,133
  2. Replies:
    8
    Views:
    2,001
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    625
    James Kanze
    Sep 4, 2007
  4. Bruno Desthuilliers

    check if the values are prensent in a list of values

    Bruno Desthuilliers, Sep 9, 2008, in forum: Python
    Replies:
    5
    Views:
    297
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    399
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page