简体   繁体   English

如何在 C++ 中逐个元素地比较两个向量的相等元素?

[英]How to compare two vectors for equality element by element in C++?

Is there any way to compare two vectors?有没有办法比较两个向量?

if (vector1 == vector2)
    DoSomething();

Note: Currently, these vectors are not sorted and contain integer values.注意:目前,这些向量未排序并包含 integer 值。

Your code ( vector1 == vector2 ) is correct C++ syntax.您的代码( vector1 == vector2 )是正确的 C++ 语法。 There is an == operator for vectors.向量有一个==运算符。

If you want to compare short vector with a portion of a longer vector, you can use the equal() operator for vectors.如果要将短向量与较长向量的一部分进行比较,可以对向量使用equal()运算符。 ( documentation here ) 这里的文档

Here's an example:这是一个例子:

using namespace std;

if( equal(vector1.begin(), vector1.end(), vector2.begin()) )
    DoSomething();

Check std::mismatch method of C++.检查 C++ 的std::mismatch方法。

comparing vectors has been discussed on DaniWeb forum and also answered .比较向量已在DaniWeb 论坛上进行了讨论并得到了回答

C++: Comparing two vectors C++:比较两个向量

Check the below SO post.检查下面的SO帖子。 will helpful for you.会对你有所帮助。 they have achieved the same with different-2 method.他们用不同的2方法达到了相同的效果。

Compare two vectors C++ 比较两个向量 C++

C++11 standard on == for std::vector C++11 标准==用于std::vector

Others have mentioned that operator== does compare vector contents and works, but here is a quote from the C++11 N3337 standard draft which I believe implies that.其他人提到operator==确实比较了矢量内容和工作,但这里引用了C++11 N3337 标准草案,我相信这暗示了这一点。

We first look at Chapter 23.2.1 "General container requirements", which documents things that must be valid for all containers, including therefore std::vector .我们首先看第 23.2.1 章“通用容器要求”,其中记录了必须对所有容器有效的内容,因此包括std::vector

That section Table 96 "Container requirements" which contains an entry:包含条目的表 96“容器要求”部分:

 Expression Operational semantics =========== ====================== a == b distance(a.begin(), a.end()) == distance(b.begin(), b.end()) && equal(a.begin(), a.end(), b.begin())

The distance part of the semantics means that the size of both containers are the same, but stated in a generalized iterator friendly way for non random access addressable containers.语义的distance部分意味着两个容器的大小相同,但以通用迭代器友好的方式表示,用于非随机访问可寻址容器。 distance() is defined at 24.4.4 "Iterator operations". distance()在第 24.4.4 节“迭代器操作”中定义。

Then the key question is what does equal() mean.那么关键问题是equal()是什么意思。 At the end of the table we see:在表格的最后,我们看到:

Notes: the algorithm equal() is defined in Clause 25.注意:算法 equal() 在第 25 条中定义。

and in section 25.2.11 "Equal" we find its definition:在第 25.2.11 节“相等”中,我们找到了它的定义:

 template<class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); template<class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);

1 Returns: true if for every iterator i in the range [first1,last1) the following corresponding conditions hold: *i == *(first2 + (i - first1)) , pred(*i, *(first2 + (i - first1))) != false . 1 返回:如果对于范围[first1,last1)中的每个迭代器 i 都满足以下相应条件,则返回 true: *i == *(first2 + (i - first1)) , pred(*i, *(first2 + (i - first1))) != false Otherwise, returns false.否则,返回 false。

In our case, we care about the overloaded version without BinaryPredicate version, which corresponds to the first pseudo code definition *i == *(first2 + (i - first1)) , which we see is just an iterator-friendly definition of "all iterated items are the same".在我们的例子中,我们关心没有BinaryPredicate版本的重载版本,它对应于第一个伪代码定义*i == *(first2 + (i - first1)) ,我们看到它只是“所有迭代的项目是一样的”。

Similar questions for other containers:其他容器的类似问题:

According to the discussion here you can directly compare two vectors using根据这里的讨论,您可以使用直接比较两个向量

== ==

if (vector1 == vector2){
   //true
}
else{
   //false
}

If they really absolutely have to remain unsorted (which they really don't.. and if you're dealing with hundreds of thousands of elements then I have to ask why you would be comparing vectors like this), you can hack together a compare method which works with unsorted arrays.如果他们真的绝对必须保持未排序(他们真的没有......如果你正在处理数十万个元素,那么我不得不问你为什么要比较这样的向量),你可以一起进行比较适用于未排序的 arrays 的方法。

The only way I though of to do that was to create a temporary vector3 and pretend to do a set_intersection by adding all elements of vector1 to it, then doing a search for each individual element of vector2 in vector3 and removing it if found.我想这样做的唯一方法是创建一个临时vector3并假装通过将vector1的所有元素添加到它来进行set_intersection ,然后在vector3中搜索vector2的每个单独元素并在找到时将其删除。 I know that sounds terrible, but that's why I'm not writing any C++ standard libraries anytime soon.我知道这听起来很糟糕,但这就是为什么我不会很快编写任何 C++ 标准库。

Really, though, just sort them first.不过,实际上,只需先对它们进行排序。

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

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