矢量比较失败
Fail of Vectors Comparison
我正在尝试比较两个向量,据我所知,向量支持关系运算符,它的工作方式如下:比较 v1 中的第一个元素与 v2 中的第一个元素,依此类推..
为什么以下代码的结果是 (true) where the last element in v1 > v2 ?!
#include <iostream>
#include <vector>
using namespace std;
void main()
{
vector <int> V1 = { 2,1,0,3 };
vector <int> V2 = { 3,4,2,2 };
cout << (V1 <= V2); //print true !!
system("pause");
}
operator==,!=,<,<=,>,>=
比较两个向量字典序的内容。
来自 http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare :
Lexicographical comparison is a operation with the following properties:
- Two ranges are compared element by element.
- The first mismatching element defines which range is lexicographically less or greater than the other.
这就是字符串“abcdx”小于“abced”和[的原因2,1,0,3] 小于 [3,4,2,2].
std::vector
是一个数据容器,除了包含多个元素外,与向量的数学概念无关。
std::vector
的 doc 说明了比较的工作原理:
The equality
comparison (operator==) is performed by first comparing sizes, and if
they match, the elements are compared sequentially using operator==,
stopping at the first mismatch (as if using algorithm equal).
The less-than comparison (operator<) behaves as if using algorithm
lexicographical_compare, which compares the elements sequentially
using operator< in a reciprocal manner (i.e., checking both a
我正在尝试比较两个向量,据我所知,向量支持关系运算符,它的工作方式如下:比较 v1 中的第一个元素与 v2 中的第一个元素,依此类推..
为什么以下代码的结果是 (true) where the last element in v1 > v2 ?!
#include <iostream>
#include <vector>
using namespace std;
void main()
{
vector <int> V1 = { 2,1,0,3 };
vector <int> V2 = { 3,4,2,2 };
cout << (V1 <= V2); //print true !!
system("pause");
}
operator==,!=,<,<=,>,>=
比较两个向量字典序的内容。
来自 http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare :
Lexicographical comparison is a operation with the following properties:
- Two ranges are compared element by element.
- The first mismatching element defines which range is lexicographically less or greater than the other.
这就是字符串“abcdx”小于“abced”和[的原因2,1,0,3] 小于 [3,4,2,2].
std::vector
是一个数据容器,除了包含多个元素外,与向量的数学概念无关。
std::vector
的 doc 说明了比较的工作原理:
The equality comparison (operator==) is performed by first comparing sizes, and if they match, the elements are compared sequentially using operator==, stopping at the first mismatch (as if using algorithm equal).
The less-than comparison (operator<) behaves as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< in a reciprocal manner (i.e., checking both a