简体   繁体   English

检查两个向量是否相等

[英]Check if two vectors are equal

How can I check whether the first "n" elements of two vectors are equal or not?如何检查两个向量的前“n”个元素是否相等?

I tried the following:我尝试了以下方法:

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

typedef vector<double> v_t;

int main(){
    v_t v1,v2;
    int n = 9;

    for (int i = 1; i<10; i++){
        v1.push_back(i);
        v2.push_back(i);
    }
    v1.push_back(11);
    v2.push_back(12);

    if (v1.begin()+n == v2.begin()+n)
        cout << "success" << endl;
    else
        cout << "failure" << endl;
}

Why does it print "failure" and not "success"?为什么它打印“失败”而不是“成功”?

Use the std::equal function from the <algorithm> header: 使用<algorithm>标头中的std::equal函数:

if (std::equal(v1.begin(), v1.begin() + n, v2.begin())
  std::cout << "success" << std::endl;

Note that both vectors must have at least n elements in them. 请注意,两个向量必须至少包含n元素。 If either one is too short, behavior of your program will be undefined. 如果任何一个太短,程序的行为将是不确定的。

If you want to check whether the entire vector is equal to the other, just compare them like you'd compare anything else: 如果你想检查整个矢量是否等于另一个,只需比较它们就像你比较其他任何东西:

if (v1 == v2)

Your (failed) code was comparing an iterator of one vector with an iterator of the other. 你的(失败的)代码是将一个向量的迭代 与另一个向量的迭代器进行比较。 Iterators of equal vectors are not equal. 等向量的迭代器不相等。 Each iterator is tied to the sequence it's iterating, so an iterator from one vector will never be equal to the iterator of another. 每个迭代器都与它迭代的序列相关联,因此一个向量的迭代器永远不会等于另一个向量的迭代器。

The easiest (in terms of fewest non-everyday functions to look up) way to compare the two is to loop again: 最简单的(就最少的非日常功能来查找)比较两者的方法是再次循环:

bool are_equal = true;
for (int i = 0; i < first_how_many; i++)
    if (v1[i] != v2[i])
    {
        are_equal = false;
        break;
    }

It'll do much the same thing, but if you prefer you can use the <algorithm> header's std::equal function: http://www.cplusplus.com/reference/algorithm/equal/ 它会做同样的事情,但如果你愿意,可以使用<algorithm> header的std::equal函数: http//www.cplusplus.com/reference/algorithm/equal/

First, there is no need to keep track the size of a vector, ie n is useless;首先,不需要跟踪向量的大小,即n是无用的; begin(v) + n == end(v) or just n == size(v) (the size information is in the vector class). begin(v) + n == end(v)或只是n == size(v) (大小信息在向量类中)。

Now, I just want to point out a C++20 feature, the ranges library.现在,我只想指出 C++20 的一个特性,即范围库。 It simplifies a lot of standard algorithms' function signatures, for instance the current "best" way to solve your problem would be:它简化了许多标准算法的函数签名,例如,当前解决问题的“最佳”方法是:

std::ranges::equals(v1, v2); // returns a bool

Instead of the previous std::equals(begin(v1), end(v1), begin(v2)) .而不是之前的std::equals(begin(v1), end(v1), begin(v2)) Also, if your ranges are more complex (ie vector of a class), you should consider the projection feature (if you want to apply a function before the comparaison, or compare a given member variable etc.).此外,如果您的范围更复杂(即类的向量),您应该考虑投影功能(如果您想在比较之前应用函数,或比较给定的成员变量等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM