Re: Comparing a Vector with Another Vector or a Single Value
In article <D7Ccj.326307$kj1.1891@bgtnsc04-news.ops.worldnet.att.net>,
Charles <electrochuckREMOVE@XXXatt.net> 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.
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.)
Is this the best approach?
Depends, if the variant is just for this operation, its not needed.
and only a functor with three overloads of operator (). such as
VectorGreater below.
class VectorGreater
{
std::vector<bool> *ans;
public:
typedef const std::vector<double> &vector_type;
typedef std::vector<bool> const & return_type;
typedef double scalar_type;
VectorGreater(std::vector<bool> &a):ans(&a){}
// do the compares storing results in *ans, and return
// *ans as a const reference, avoiding unnecesary copying
// of the result.
return_value operator ()const (vector_type,vector_type);
return_value operator () const (vector_type,scalar_type);
return_value operator () const (scalar_type,vector_type);
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]