Re: Comparing a Vector with Another Vector or a Single Value
Charles wrote:
Consider a function that compares a vector of doubles, for instance, with
either another vector of equal length or just a single double. The type of
comparison is not known until runtime. The output will be a vector of
bools.
Do not use vector<bool>
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=350&rl=1
Case 1:
For each element in vectorA, if vectorA[x] is greater than vectorB[x],
output[x] = true. Otherwise output[x] = false.
Case 2:
For each element in vectorA, if vectorA[x] is greater than doubleB,
output[x] = true. Otherwise output[x] = false.
My plan is to use a Boost:Variant to hold either a pointer to vectorB or
doubleB itself. The comparison function will step through vectorA and
either
compare each element with the corresponding element in vectorB or just the
single doubleB value. (I could also just fill a vector with multiple
copies
of doubleB, eliminating Case 2 above.)
One somewhat efficient solution can be following:
#include <iostream>
#include <vector>
#include <cassert>
#include "boost/lambda/bind.hpp"
#include "boost/lambda/lambda.hpp"
#include "boost/variant.hpp"
using namespace std;
class compare_visitor : public boost::static_visitor<vector<char> >
{
public:
compare_visitor (const vector<double>& a)
// reference for performance reasons
// so do not destroy original object
:a_(a) {}
vector<char> operator()(double b) const
{
vector<char> nrv(a_.size());
using namespace boost::lambda;
transform(a_.begin(), a_.end(), nrv.begin(), b < _1);
return nrv;
}
vector<char> operator()(const vector<double>& b) const
{
assert(b.size() == a_.size());
vector<char> nrv(a_.size());
using namespace boost::lambda;
transform(a_.begin(), a_.end(), b.begin(), nrv.begin(), _2 <
_1);
return nrv;
}
private:
const vector<double>& a_;
};
int main()
{
vector<double> a;
a.push_back(2.0);
a.push_back(0.0);
vector<double> b;
b.push_back(3.0);
b.push_back(-1.0);
boost::variant< vector<double>, double > b1(1.0);
boost::variant< vector<double>, double > b2(b);
vector<char> r1 = boost::apply_visitor( compare_visitor(a), b1 );
vector<char> r2 = boost::apply_visitor( compare_visitor(a), b2 );
for (unsigned int i = 0; i < r1.size(); ++i)
cout << (int)r1[i] << endl;
cout << endl;
for (unsigned int i = 0; i < r2.size(); ++i)
cout << (int)r2[i] << endl;
return 0;
}
Best,
Jozef Wagner
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]