gpt4 book ai didi

c++ - vector 比较失败

转载 作者:行者123 更新时间:2023-11-30 01:12:34 32 4
gpt4 key购买 nike

我正在尝试比较两个 vector ,据我所知, vector 支持关系运算符并且它以这种方式工作:将 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==,!=,<,<=,>,>=比较两个 vector lexicographicall 的内容。来自 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]。

关于c++ - vector 比较失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33938909/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com