Discussion:
Comparing two vectors element by element
Sean Farrow
2008-09-09 15:55:33 UTC
Permalink
Hi:
I need to compare two vectors. I already have a function to compare two
individual elements (std::bitsets) in this case. Can I use boost.foreach
to do this, or is there a better way.
I need to know whether vector one, is less than vector two.

Any help apreciated.
Cheers
Sean.


__________ Information from ESET NOD32 Antivirus, version of virus
signature database 3428 (20080909) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Peter Barker
2008-09-09 16:17:09 UTC
Permalink
On Tue, Sep 9, 2008 at 4:55 PM, Sean Farrow
Post by Sean Farrow
I need to compare two vectors. I already have a function to compare two
individual elements (std::bitsets) in this case. Can I use boost.foreach
to do this, or is there a better way.
I need to know whether vector one, is less than vector two.
Any help apreciated.
Cheers
Sean.
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v1;
std::vector<int> v2;
if(v1 == v2)
std::cout << "They're the same" << std::endl;
}
Oops - sorry - I posted too quickly. Let's try again...

Just use the < operator as you would with built in types. e.g.:

#include <vector>
#include <iostream>

int main()
{
std::vector<int> v1;
v1.push_back(5);
std::vector<int> v2;
v2.push_back(6);

if(v1 < v2)
std::cout << "v1 is smaller than v2" << std::endl;
}
Sean Farrow
2008-09-10 06:20:06 UTC
Permalink
Hi:
The code doesn't compile here as std:bitset<bits> doesn'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.
-----Original Message-----
From: boost-users-***@lists.boost.org
[mailto:boost-users-***@lists.boost.org] On Behalf Of Peter Barker
Sent: 09 September 2008 17:17
To: boost-***@lists.boost.org
Subject: Re: [Boost-users] Comparing two vectors element by element
On Tue, Sep 9, 2008 at 4:55 PM, Sean Farrow
Post by Sean Farrow
I need to compare two vectors. I already have a function to compare two
individual elements (std::bitsets) in this case. Can I use
boost.foreach
Post by Sean Farrow
to do this, or is there a better way.
I need to know whether vector one, is less than vector two.
Any help apreciated.
Cheers
Sean.
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v1;
std::vector<int> v2;
if(v1 == v2)
std::cout << "They're the same" << std::endl;
}
Oops - sorry - I posted too quickly. Let's try again...

Just use the < operator as you would with built in types. e.g.:

#include <vector>
#include <iostream>

int main()
{
std::vector<int> v1;
v1.push_back(5);
std::vector<int> v2;
v2.push_back(6);

if(v1 < v2)
std::cout << "v1 is smaller than v2" << std::endl;
}
_______________________________________________
Boost-users mailing list
Boost-***@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


__________ Information from ESET NOD32 Antivirus, version of virus
signature database 3430 (20080910) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus
signature database 3430 (20080910) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Ovanes Markarian
2008-09-10 09:05:58 UTC
Permalink
Hi!
This is not a directly a boost question. The answer is below
Post by Sean Farrow
I need to compare two vectors. I already have a function to compare two
individual elements (std::bitsets) in this case. Can I use boost.foreach
to do this, or is there a better way.
I need to know whether vector one, is less than vector two.
STL defines different algorithms in <algorithm> header. There is an equal
algorithm.


In you case you can write:

#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
vector<int> v1;
vector<int> v2;

equal(v1.begin(), v1.end(), v2.begin(), less<int>());

This is how to compare 2 vector instances if one is less than the other. I
do not really understand your question about bitsets. Do you have vector of
bitsets? If so, replace int in vector template instantiation with your
bitset instantiation and less<int>() with the instance to your functor.


Hope that helps,
Ovanes
Gevorg Voskanyan
2008-09-10 10:18:49 UTC
Permalink
The code doesn't compile here as std:bitsetdoesn't define the <
operator according to visual studio. I have a custom operator defined as
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
Sean Farrow
2008-09-10 11:48:30 UTC
Permalink
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-***@lists.boost.org
[mailto:boost-users-***@lists.boost.org] On Behalf Of Gevorg
Voskanyan
Sent: 10 September 2008 11:19
To: boost-***@lists.boost.org
Subject: Re: [Boost-users] Comparing two vectors element by element
The code doesn't compile here as std:bitsetdoesn't define the <
operator according to visual studio. I have a custom operator defined as
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-***@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
Jeff Flinn
2008-09-10 12:25:28 UTC
Permalink
Post by Sean Farrow
Each vector represents a braille cell.
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<std::bitset<6>,6> or std::bitset<36>.

If the unused values space savings are important why not
boost::dynamic_bit_set? It already has the comparison operators defined.

Jeff Flinn
Gevorg Voskanyan
2008-09-10 12:15:25 UTC
Permalink
Post by Sean Farrow
Each vector represents a braille cell.
A vector can have up to 6 bitsets, each bitset represents one dots
state.
Hth.
Sean.
Ok, what happens when a vector having 4 bitsets is compared with one having 6:

- 4 bitsets of the first vector are compared with the first 4 bitsets of the second vector, ignoring last 2 elements?
- The first vector is considered the lesser because it has fewer elements?
- Is this not allowed?
- something else?

Best Regards,
Gevorg
Sean Farrow
2008-09-10 12:53:06 UTC
Permalink
It's not allower, the sizes aren't identical.
Sean.

-----Original Message-----
From: boost-users-***@lists.boost.org
[mailto:boost-users-***@lists.boost.org] On Behalf Of Gevorg
Voskanyan
Sent: 10 September 2008 13:15
To: boost-***@lists.boost.org
Subject: Re: [Boost-users] Comparing two vectors element by element
Post by Sean Farrow
Each vector represents a braille cell.
A vector can have up to 6 bitsets, each bitset represents one dots
state.
Hth.
Sean.
Ok, what happens when a vector having 4 bitsets is compared with one
having 6:

- 4 bitsets of the first vector are compared with the first 4 bitsets of
the second vector, ignoring last 2 elements?
- The first vector is considered the lesser because it has fewer
elements?
- Is this not allowed?
- something else?

Best Regards,
Gevorg



_______________________________________________
Boost-users mailing list
Boost-***@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
Gevorg Voskanyan
2008-09-10 13:01:57 UTC
Permalink
Post by Sean Farrow
It's not allower, the sizes aren't identical.
Sean.
Then I think you can perfectly use the function below:

bool bitset_vector_less( const std::vector< std::bitset< 6 > > & lhs,
const std::vector< std::bitset< 6 > > & rhs )
{
assert( lhs.size() == rhs.size() );
return std::equal( lhs.begin(), lhs.end(), rhs.begin(), BitSetComp() );
}

where BitSetComp is the predicate you have shown earlier.

HTH,
Gevorg
Sean Farrow
2008-09-10 13:41:46 UTC
Permalink
That is now what I have, chers.
Sean.

-----Original Message-----
From: boost-users-***@lists.boost.org
[mailto:boost-users-***@lists.boost.org] On Behalf Of Gevorg
Voskanyan
Sent: 10 September 2008 14:02
To: boost-***@lists.boost.org
Subject: Re: [Boost-users] Comparing two vectors element by element
Post by Sean Farrow
It's not allower, the sizes aren't identical.
Sean.
Then I think you can perfectly use the function below:

bool bitset_vector_less( const std::vector< std::bitset< 6 > > & lhs,
const std::vector< std::bitset< 6 > >
& rhs )
{
assert( lhs.size() == rhs.size() );
return std::equal( lhs.begin(), lhs.end(), rhs.begin(), BitSetComp()
);
}

where BitSetComp is the predicate you have shown earlier.

HTH,
Gevorg




_______________________________________________
Boost-users mailing list
Boost-***@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

Loading...