Re: [Boost-users] Comparing two vectors element by element
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
Hi Gevorg: In this case it's the second, for more background for interest: Each vector represents a braille cell. A braille cell has 6 dots hence the std::bitset<6> so: A vector can have up to 6 bitsets, each bitset represents one dots state. Hth. Sean. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Gevorg Voskanyan Sent: 10 September 2008 11:19 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Comparing two vectors element by element 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users __________ Information from ESET NOD32 Antivirus, version of virus signature database 3431 (20080910) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com __________ Information from ESET NOD32 Antivirus, version of virus signature database 3431 (20080910) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com
Sean Farrow wrote:
Hi Gevorg: In this case it's the second, for more background for interest: Each vector represents a braille cell. A braille cell has 6 dots hence the std::bitset<6> so: A vector can have up to 6 bitsets, each bitset represents one dots state. Hth. Sean.
If your vector length is bounded why not just use
boost::array
participants (3)
-
Gevorg Voskanyan
-
Jeff Flinn
-
Sean Farrow