在下面的代码中,我遇到了很多错误。当我使用 cout << (v1 > v2) << endl 代替时,所有错误都消失了,我得到了正确的输出。为什么会这样呢?这是我收到的错误的第一部分(有很多错误,但如果需要,我将发布所有错误):
abc.cpp: In function 'int main()':
abc.cpp:18:10: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::vector<int>')
Run Code Online (Sandbox Code Playgroud)
我的代码是:
/* #include <bits/stdc++.h> */
#include <iostream>
#include <algorithm>
#include <vector>
//using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::sort;
int main(void)
{
vector <int> v1 = {1, 2, 3};
vector <int> v2 = {1, 2, 5};
cout << v1 > v2 << endl;
// free variables
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您需要括号的原因是因为运算符优先级。 优先级比这样>低<<
cout << v1 > v2 << endl;
Run Code Online (Sandbox Code Playgroud)
实际上是
(cout << v1) > (v2 << endl);
Run Code Online (Sandbox Code Playgroud)
这就是你收到错误的原因。