为什么比较运算符"=="返回"YES"即使两个向量不同?
Why is comparison operator "==" returning "YES" even though the two vectors are different?
我尝试运行这段代码,结果显示"是"甚至认为两个向量具有不同的内容并且大小不同。我不明白比较运算符如何处理向量
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector <int> example; //First vector definition
example.push_back(3);
example.push_back(10);
example.push_back(33);
for(int x=0;x<example.size();x++)
{
cout<<example[x]<<" ";
}
if(!example.empty())
{
example.clear();
}
vector <int> another_vector; //Second vector definition
another_vector.push_back(10);
example.push_back(10);
if(example==another_vector) //Comparison between the two vector
{
cout<<endl<<"YES";
}
else
{
cout<<endl<<"NO";
}
return 0;
}
预期输出为"否"但收到的输出为"是">
在这里,您将从example
中删除所有元素:
if(!example.empty())
{
example.clear();
}
因此,此时第一个向量为空。然后,创建 another_vector
,默认为空。现在
another_vector.push_back(10);
example.push_back(10);
此时,两个向量都只包含一个元素:10
。 operator ==
做了它应该做的事情。
相关文章:
- 在C++中查找两个向量之间最相似的值
- 如何在C++中从两个向量生成所有可能的对?
- 返回两个向量 – 使用引用还是元组?
- 检查两个向量是否并行的最有效方法
- 如何获得比较两个向量对的子集
- 如何在 c++ 中对两个向量进行线性搜索?
- 如何在 c++ 中从两个向量创建 JSON 对象?
- C++如何同时删除位于两个向量中的 2 个指针?
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 删除两个向量中的重复项
- 两个向量在犰狳中相等?
- 为什么我无法比较自定义类类型的两个向量?
- 通过块比较两个向量时,如何避免重复
- Boost.Test - 如何在交叉点上检查两个向量
- 通过移动从两个向量创建元组向量
- 如何在C 中合并两个向量
- R- armadillo c :用其他两个向量对向量进行排序
- 使用嵌套循环搜索两个向量并查看其属性
- 如何以精度换取速度来评估C++中两个向量的点积符号?(不特定于硬件)
- 我该如何XOR这两个向量的内容