"No match for operator>>" 但我不明白为什么。你能解释一下吗?

"No match for operator>>" but I don't understand why. Could you explain me, please?

我得到了以下代码:

#include <iostream>
#include <vector>

using namespace std;

vector<int> Reversed(const vector<int>& v){
    
    //vector<int> v;  //[Error] declaration of 'std::vector<int> v' shadows a parameter
    int z; vector<int> copy; vector<int> working;
    working = v;
    z = working.size();
    int i;
    for (i = 0; i<=z; i++){
        working[i] = working[z];
        copy.push_back(working[i]);
    }
    return copy;
}

int main() {
    
    vector<int> v;
    cin >> v;  /*[Error] no match for 'operator>>' (operand types are 'std::istream' 
                {aka 'std::basic_istream<char>'} and 'std::vector<int>')*/
    cout << Reversed(v);
    return 0;
}

请向我解释为什么我在第 18 行收到此错误:

no match for operator >>`

P.s.: const & i是前置任务,我无法更改。我只需要这个向量的倒置副本。

当您这样做时,您正试图一次读取整个向量 cin >> v;。您可能希望一次读取一个元素,例如:

#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
   // read five integers from stdin
  const int n = 5;
  vector<int> v(n);
  for(int i = 0; i < n; ++i)
    cin >> v[i];
  for(int i = 0; i < n; ++i)
    cout << v[i] << "\n";
  return 0;
}

输出:

0
1
2
3
4

或使用 std::vector::push_back @ajm 的回答。

您似乎在要求用户输入数字列表。 std::cin(或只是 cin,因为你有 use namespace std;)不知道如何接收整数列表并将其转换为 vector<int>

如果您想从用户输入中接收向量,我建议您向用户询问数字列表,例如:

// replace VECTOR_LENGTH
for (int i = 0; i < VECTOR_LENGTH; i++) {
  int num;
  cin >> num;
  v.push_back(num);
}