如何以特定的方式高效地比较向量和数组

How to compare vector with array in specific way in efficient way?

本文关键字:比较 向量 数组 高效 方式      更新时间:2023-10-16

我想比较一个向量和一个数组,假设元素的顺序不同。我有一个结构体如下(这个结构体没有"operator=="answers"operator<"->所以我不能使用sort):

struct A
{
    int index;
    A(int p_i) : index(p_i) {}
};

vector和数组的大小相同:

std::vector<A> l_v = {A(1), A(2), A(3)};
A l_a[3] = {A(3), A(1), A(2)};

我正在寻找一些函数从std,如下面的"some_function_X",它可以找到元素在特定的方式使用lambda,或函数可以只比较特定的字段,如"lhs。索引== rhs。通过类中的特定字段索引"->",不包含"operator=="answers"operator>"等。

bool checkIfTheSame(const std::vector<A>& l_v, const A& l_a)
{
    for(usigned int i=0; i< 3; ++i)
    {
        if(!std::some_function_X(l_v.begin(), l_v.end(), l_a, 
                              [](const A& lhs, const A& rhs){
                                 return lhs.index == rhs.index;
                              })) return false;
    }
    return true;
}

谢谢。

this struct没有operator==和operator<->所以不能使用sort

首先,只需要operator<。其次,它不必被定义为成员函数。下面的自由函数适用于std::less(如果不传递函子进行比较,std::sort将使用该函数)。

bool operator<(const A& a, const A& b) {...}

或者,您可以使用自定义比较函子来代替std::less

对数组和向量进行排序,然后进行比较,应该比简单地迭代一个数组并使用线性搜索检查元素是否存在于另一个数组具有更好的渐近运行复杂度。

另一种想法是只对其中一个容器进行排序,迭代无序的容器,并使用二分搜索来测试在有序容器中的存在性。它与对两者排序具有相同的渐近复杂度。

如果两个容器都不能修改,则可以复制一个容器进行排序。这消耗了一些内存,但仍然比普通方法渐进地快。std::partial_sort_copy可以通过直接对副本进行排序来节省一些副本。

看这里:http://www.cplusplus.com/reference/algorithm/

查找

函数,该函数只能比较特定字段,如"lhs"。索引== rhs。通过类中的特定字段索引"->",不包含"operator=="answers"operator>"等。

检查您的两个容器是否包含相同的数据与谓词:相等与自定义谓词

如果你只是想检查你的容器是否包含一个特定的值,看看这里:如何发现一个项是否存在于std::vector中?

我不确定我完全理解你想要什么,但我会猜测你想检查两个数组(具有不同的容器类型)是否等效,也就是说,对于数组a中的每个项目,在数组B中的某个地方有一个相应的项目,其中一些谓词返回true。

您希望在两个数组上执行此操作,这些数组不仅具有不同的类型,而且可能以不同的顺序。

这是一种方法:

struct Foo
{
    Foo(int i) : _index { i } {}
    int index() const {
        return _index;
    }
private:
    int _index;
};
// params:
// first1, last1 bound the first array
// first2, last2 bound the second array
// pred is a predicate that checks for equivalents
// order is a predicate that performs weak ordering
template<class Iter1, class Iter2, class Pred, class Order>
bool checkIfTheSame(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred, Order order)
{
    const auto len1 = last1 - first1;
    const auto len2 = last2 - first2;
    if (len1 != len2)
        return false;
    using namespace std;
    using item1_type = typename std::iterator_traits<Iter1>::value_type;
    using item2_type = typename std::iterator_traits<Iter2>::value_type;
    std::vector<std::reference_wrapper<item1_type>> refs1 { first1, last1 };
    std::sort(begin(refs1), end(refs1), order);
    std::vector<std::reference_wrapper<item2_type>> refs2 { first2, last2 };
    std::sort(begin(refs2), end(refs2), order);
    for (size_t i = 0 ; i < len1 ; ++i) {
        if (!pred(refs1[i], refs2[i]))
            return false;
    }
    return true;
}

简单的例子:

using namespace std;
bool same_indexes(const Foo& f1, const Foo& f2) {
    return f1.index() == f2.index();
}
bool sort_indexes(const Foo& f1, const Foo& f2) {
    return f1.index() < f2.index();
}
int main(int argc, const char * argv[])
{
    vector<Foo> x { 1, 2, 3 };
    Foo y[] = { 3, 2, 1 };
    cout << checkIfTheSame(begin(x), end(x), begin(y), end(y), same_indexes, sort_indexes) << endl;
    return 0;
}

如果无法对数组或向量进行排序,但只能知道两个元素是否相等,那么唯一的方法就是穷举搜索。