Boost logo

Boost Users :

Subject: Re: [Boost-users] Comparing two vectors element by element
From: Gevorg Voskanyan (v_gevorg_at_[hidden])
Date: 2008-09-10 06:18:49


Sean Farrow wrote:
> Hi:
> The code doesn't compile here as std:bitsetdoesn't define the <
> operator according to visual studio. I have a custom operator defined as
> a structure:
> struct BitSetComp
> {
> bool operator()(const std::bitset<6>& lhs, const std::bitset<6>&
> rhs) const
> {
> return lhs.to_ulong() < rhs.to_ulong();
> }
> };
> How can I use this in the code you providee, or do I need to redefine in
> a certain way?
> Cheers
> Sean.

[snip]

Hi Sean,

The answer depends on how you define one vector being less than another.

E.g. given the following vectors:

typedef std::bitset< 6 > my_bitset;
typedef std::vector< my_bitset > my_bitset_vector;
my_bitset_vector
   v1 = { my_bitset( "1100" ), my_bitset( "1101" ) },
   v2 = { my_bitset( "1110" ), my_bitset( "1000" ) },
   v3 = { my_bitset( "1101" ), my_bitset( "1111" ), my_bitset( "1010" ) },
   v4 = { my_bitset( "1100" ), my_bitset( "1101" ), my_bitset( "1001" };

how shall these vectors compare to each other?

#1. Lexicographical comparison - v1 < v2 is true, v1 < v3 is true, v2 < v3 is false, v1 < v4 is true, ...

#include <algorithm>

bool bitset_vector_less_1( const my_bitset_vector& lhs, const my_bitset_vector& rhs )
{
   return std::lexicographical_compare( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), BitSetComp() );
}

#2. Each element of the first vector shall be less than the correponding element of the second vector - v1 < v2 is false, v2 < v1 is false, v4 < v3 is true,
(here it is not immediately clear how would one compare vectors of different size such as v1 and v3)

bool bitset_vector_less_2( const my_bitset_vector& lhs, const my_bitset_vector& rhs )
{
   assert( lhs.size() == rhs.size() );
   return std::equal( lhs.begin(), lhs.end(), rhs.begin(), BitSetComp() );
}

If you mean something else, explain your requirement with more detail and I will try to offer some more help.

HTH,

Best Regards,
Gevorg

P.S.
Sorry but the answer is not strictly boost-related

      


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net