Comparing two arrays [closed]
I've two arrays, say A = {1,2,3,4,5} and B = {5,6,3,4,5}. I want to compare the two arrays and tell how many elements in A are lesser than B and vice versa and also possibly the elements position which are lesser. I can do this using nested loop but is there a efficient way in C++ to do this ?
In this example, A = {1,2,3,4,5} B = {5,6,3,4,5}
So I expect the answer to be "B is greater than A"开发者_Go百科 just because it satisfies the condition that it is greater in two elements. or if it is
A = {1,2,3,4,5} and B = {1,2,3,0,4}, A is greater.
I need to compare only the number if elements greater or lesser and not the values in itself.
Sorry for my ambiguity.
Do you want to compare corresponding elements of A and B and count how many A' are less than the matching B's? This can be done with std::inner_product like this:
int A[] = { 1,2,3,4,5 };
int B[] = { 5,6,3,4,5 };
int count = std::inner_product(A, A+5, B, 0, std::plus<int>(), std::less<int>());
std::cout << count << "\n";
Of course, this just an O(n) loop inside the STL numeric algorithm.
It's not clear what you mean. Do you mean compare them element-wise?
vector<int> indexesForA;
vector<int> indexesForB;
for(int i = 0; i < length; i++) {
if(A[i] < B[i]) {
indexesForA.push_back(i);
}
else if(B[i] < A[i]) {
indexesForB.push_back(i);
}
}
// now indexesForA.size() is the count of indexes i where A[i] < B[i]
// and indexesForA contains the indexes
// similarly for indexesForB
Please post a clarification and we can guide you.
Here's my example. Maybe it could be using more advanced STL techniques but I think this suffices.
class Examiner
{
template <class Pred>
struct CompareToZero: std::unary_function<const int&, bool> {
Pred comparator;
bool operator() (const int& value) const {
return comparator(value, 0);
}
};
template<class T>
std::vector<size_t> getComparedIndices(const CompareToZero<T>& comparator, const std::vector<int>& vint) const {
std::vector<size_t> retval;
for (size_t i = 0; i < vint.size(); i++) {
if (comparator(vint[i]))
retval.push_back(i);
}
return retval;
}
const std::vector<int>& _results;
public:
Examiner(const std::vector<int>& results)
: _results(results) {
}
std::vector<size_t> getLessIndices() const {
return getComparedIndices(CompareToZero<std::less<int> >(), _results);
}
std::vector<size_t> getGreaterIndices() const {
return getComparedIndices(CompareToZero<std::greater<int> >(), _results);
}
std::vector<size_t> getEqualIndices() const {
return getComparedIndices(CompareToZero<std::equal_to<int> >(), _results);
}
size_t getLessCount() const {
return getLessIndices().size();
}
size_t getGreaterCount() const {
return getGreaterIndices().size();
}
size_t getEqualCount() const {
return getEqualIndices().size();
}
};
void arrcomp()
{
int A[5] = {1,2,3,4,5};
int B[5] = {5,6,3,2,4};
std::vector<int> results(sizeof(A)/sizeof(A[0]));
for (size_t i = 0; i < results.size(); ++i) {
results[i] =
(A[i] < B[i])? -1:
(A[i] > B[i])? 0:
1;
}
Examiner examiner(results);
std::cout << "less count : " << examiner.getLessCount() << std::endl;
std::cout << "greater count: " << examiner.getGreaterCount() << std::endl;
std::cout << "equal count : " << examiner.getEqualCount() << std::endl;
}
int A[5] = {1,2,3,4,5};
int B[5] = {5,6,3,2,4};
std::vector<int> vectA(A);
std::vector<int> vectB(B);
and using std <algorithm>
to do the job or part of the job on the two vectors.
for example: count_if Return number of elements in range satisfying condition (function template)
What do you mean? Do you mean where A[i] < B[i] or do you mean how many elements in B are less than A[i]? Either you'll have a single loop where you're comparing the ith element of each array, or you'll have a nested loop where you're comparing the ith element of A with the jth element of B. Since I don't know which one you want, I can't tell you the right answer.
I think the most efficient way is to sort the arrays first and then just check in which index is the first element of array B that is greater than its A counterpart. As the ordering is transitive, you can even do it for all the elements in the same pass.
精彩评论