C++ 我可以使用'=='来比较两个向量吗?我试过了,似乎工作正常。但我不知道在更复杂的情况下是否会起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16422486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:20:21  来源:igfitidea点击:

Can I use ' == ' to compare two vectors. I tried it and seems to be working fine. But I don't know whether it will work in more complex situations

c++vectoroperator-overloadingequality

提问by suman shadow

First example:

第一个例子:

int main(){
    using namespace std;   
    vector<int> v1{10, 20, 30, 40, 50};
    vector<int> v2{10, 20, 30, 40, 50};

    if(v1==v2)
        cout<<"equal";
    else
        cout<<"unequal";
}   // it returns equal 

Second example:

第二个例子:

int main(){
    using namespace std;   
    vector<int> v1{10, 20, 30, 40, 50};
    vector<int> v2{10, 20, 100000, 40, 50};

    if(v1==v2)
        cout<<"equal";
    else
        cout<<"unequal";
}   // it returns notequal 

回答by Andy Prowl

The overload of operator ==that works on two std::vectorswill compare the vector sizes and return falseif those are different; if not, it will compare the contents of the vector element-by-element.

在两个s上工作重载operator ==std::vector将比较向量大小并false在它们不同时返回;如果没有,它将逐个元素比较向量的内容。

If operator ==is defined for the vector's element type, then the comparison of vectors through operator ==is valid and meaningful.

如果operator ==为向量的元素类型定义了 ,则向量通过的比较operator ==是有效且有意义的。

In formal terms, the C++11 standard specifies the operational semantics of a == bfor sequence containers as (Table 96, § 23.2.1):

在正式术语中,C++11 标准将a == b序列容器的操作语义指定为(表 96,第 23.2.1 节):

==is an equivalence relation.

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

==是等价关系。

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

As you can see, equality between sequence containers is defined in terms of the std::equalalgorithm between ranges defined by pairs of iterators, which in turn uses operator ==for comparison of individual elements.

如您所见,序列容器之间的相等性是根据std::equal迭代器对定义的范围之间的算法定义的,迭代器又operator ==用于比较各个元素。

回答by Joseph Mansfield

Yes, you can use operator==to compare two std::vectors. It will return trueonly if the vectors are the same size and all elements compare equal.

是的,您可以operator==用来比较两个std::vectors。true仅当向量大小相同且所有元素比较相等时才会返回。

回答by Tony

Be advised that vectors are ordered, and std::equalor the ==operator check that the vectors have the same contents in the same order. For many use cases this might be enough.

请注意向量是有序的,std::equal或者==操作员检查向量是否以相同的顺序具有相同的内容。对于许多用例,这可能就足够了。

But there might be occasions when you want to know if two vectors have the same contents but not necessarily in the same order. For that case you need another function.

但是有时您可能想知道两个向量是否具有相同的内容但不一定具有相同的顺序。对于这种情况,您需要另一个功能。

One nice and short implementation is the one below. It was suggested here: https://stackoverflow.com/questions/17394149/how-to-efficiently-compare-vectors-with-c/17394298#17394298There you will also find a discussion on why you might not want to use it...

下面是一种很好且简短的实现。这里建议:https: //stackoverflow.com/questions/17394149/how-to-efficiently-compare-vectors-with-c/17394298#17394298在那里你还会找到关于为什么你可能不想使用它的讨论...

Put this in a header file of your choice:

把它放在你选择的头文件中:

#include <algorithm>

template <class T>
static bool compareVectors(std::vector<T> a, std::vector<T> b)
{
   if (a.size() != b.size())
   {
      return false;
   }
   ::std::sort(a.begin(), a.end());
   ::std::sort(b.begin(), b.end());
   return (a == b);
}

And here an example illustrating the above theory:

这是一个说明上述理论的例子:

std::vector<int> vector1;
std::vector<int> vector2;

vector1.push_back(100);
vector1.push_back(101);
vector1.push_back(102);

vector2.push_back(102);
vector2.push_back(101);
vector2.push_back(100);

if (vector1 == vector2)
   std::cout << "same" << std::endl;
else
   std::cout << "not same" << std::endl;

if (std::equal(vector1.begin(), vector1.end(), vector2.begin()))
   std::cout << "same" << std::endl;
else
   std::cout << "not same" << std::endl;

if (compareVectors(vector1, vector2))
   std::cout << "same" << std::endl;
else
   std::cout << "not same" << std::endl;

The output will be:

输出将是:

not same
not same
same

回答by taocp

You can check the documentation of operator==for vector: operator==,!=,<,<=,>,>=(std::vector)

您可以查看operator==for vector的文档:operator==,!=,<,<=,>,>=(std::vector)

Quoting from the link:

从链接引用:

 template< class T, class Alloc >
 bool operator==( vector<T,Alloc>& lhs,
             vector<T,Alloc>& rhs );

Compares the contents of two containers.

Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs has equivalent element in rhs at the same position.

parameters:

lhs, rhs containers whose contents to compare

T must meet the requirements of EqualityComparable in order to use versions

Return value

true if the contents of the containers are equivalent, false otherwise

比较两个容器的内容。

检查 lhs 和 rhs 的内容是否相等,即 lhs.size() == rhs.size() 和 lhs 中的每个元素是否在相同位置具有 rhs 中的等效元素。

参数:

lhs、rhs 容器,其内容要比较

T 必须满足 EqualityComparable 的要求才能使用版本

返回值

如果容器的内容相等则为真,否则为假

回答by huskerchad

Yes. A good reference is cppreference.com, where you can look up operator==for vector<T>, for example on this page: non-member operators, and you will find:

是的。一个很好的参考是cppreference.com,在那里你可以看一下operator==vector<T>,比如这个页面上:非会员运营商,你会发现:

Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs has equivalent element in rhs at the same position.

检查 lhs 和 rhs 的内容是否相等,即 lhs.size() == rhs.size() 和 lhs 中的每个元素是否在相同位置具有 rhs 中的等效元素。

回答by Mats Petersson

As long as your vector contains elements that in themselves can be compared (have operator==), this works, yes. Note however that if you have a vector that contains for example pointers to identical objects, but not the SAME instance of an object, then the vector is not considered identical, because the element in the vector is what is compared, not the contents of the element as such, if that makes sense.

只要您的向量包含本身可以比较(有operator==)的元素,就可以,是的。但是请注意,如果您有一个向量,例如包含指向相同对象的指针,但不包含对象的 SAME 实例,则该向量不被视为相同,因为要比较的是向量中的元素,而不是对象的内容元素本身,如果这是有道理的。