#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int num;
while(cin>>num){
vi.push_back(num);
}
for(int i=0;i<vi.size();++i){
cout<<vi[i]<<"\n";
}
return 0;
}
此代码给我“错误:'之前的预期非限定ID。'令牌|”。但为什么?请帮助我理解它。
您可能想要这个:
#include <vector> // get rid of <bits/stdc++.h> which is not standard
#include <iostream> // and include the proper headers
using namespace std;
vector<int> vi; // you want to declare a variable vi, not a type vi
// and also get rid of typedefs which only cause confusion
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int num;
while (cin >> num) {
vi.push_back(num);
}
for (int i = 0; i < vi.size(); ++i) {
cout << vi[i] << "\n";
}
return 0;
}
奖金:
您可以替换为:
for (int i = 0; i < vi.size(); ++i) {
cout << vi[i] << "\n";
}
带有这个,这更惯用:
for (auto & value : vi) {
cout << value << "\n";
}