如何逐个元素比较两个向量的相等性?
有什么方法可以比较两个向量吗?
if (vector1 == vector2)
DoSomething();
注意:目前,这些向量未排序并且包含整数值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有什么方法可以比较两个向量吗?
if (vector1 == vector2)
DoSomething();
注意:目前,这些向量未排序并且包含整数值。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
您的代码 (
vector1 == vector2
) 是正确的 C++ 语法。向量有一个==
运算符。如果您想将短向量与较长向量的一部分进行比较,可以对向量使用
equal()
运算符。 (此处的文档)这是一个示例:
Your code (
vector1 == vector2
) is correct C++ syntax. 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. (documentation here)Here's an example:
查看
std::mismatch
方法C++。比较向量已在DaniWeb 论坛上进行了讨论,并且还得到了回答:
C++:比较两个向量
查看下面的 SO 帖子。这会对你有帮助。他们用两种不同的方法达到了同样的效果。
如何获取两个字符串向量之间不同或共同元素的数量?
Check out the
std::mismatch
method of C++.Comparing vectors has been discussed on DaniWeb forum and also answered:
C++: Comparing two vectors
Check out the below SO post. It will be helpful to you. They have achieved the same with two different methods.
How do I get the number of different or common elements between two vectors of strings?
==
上的std::vector
的 C++11 标准其他已经提到
operator==
确实比较向量内容并且有效,但这里引用了 C++11 N3337 标准草案 我相信这意味着这一点。我们首先看第 23.2.1 章“一般容器要求”,其中记录了必须对所有容器有效的内容,包括
std::vector
。表 96“容器要求”部分包含一个条目:
语义的距离部分意味着两个容器的大小相同,但对于非随机访问可寻址容器以通用迭代器友好的方式表示。
distance()
在 24.4.4“迭代器操作”中定义。那么关键问题是
equal()
是什么意思。在表的最后我们看到:,在第 25.2.11 节“Equal”中我们找到了它的定义:
在我们的例子中,我们关心没有
BinaryPredicate
版本的重载版本,它对应于第一个伪代码定义*i == *(first2 + (i - first1))
,我们看到这只是“所有迭代项都相同”的迭代器友好定义。其他容器的类似问题:
C++11 standard on
==
forstd::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.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
.That section Table 96 "Container requirements" which contains an entry:
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()
is defined at 24.4.4 "Iterator operations".Then the key question is what does
equal()
mean. At the end of the table we see:and in section 25.2.11 "Equal" we find its definition:
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".Similar questions for other containers:
根据此处的讨论,您可以使用直接比较两个向量
According to the discussion here you can directly compare two vectors using
如果它们真的绝对必须保持未排序(它们确实没有......并且如果您正在处理数十万个元素,那么我必须问为什么您要像这样比较向量),您可以将比较组合在一起适用于未排序数组的方法。
我想到的唯一方法是创建一个临时
vector3
并通过将vector1
的所有元素添加到其中来假装执行set_intersection
,然后在vector3
中搜索vector2
的每个单独元素,如果找到则将其删除。我知道这听起来很糟糕,但这就是为什么我不会很快编写任何 C++ 标准库。不过,实际上,先对它们进行排序。
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.
The only way I though of to do that was to create a temporary
vector3
and pretend to do aset_intersection
by adding all elements ofvector1
to it, then doing a search for each individual element ofvector2
invector3
and removing it if found. I know that sounds terrible, but that's why I'm not writing any C++ standard libraries anytime soon.Really, though, just sort them first.