0
2
5
3 4 5 2 1
5
4 4 4 2 1

是输入,其中2是测试用例的数量,5“不是”向量的大小,我必须将整行读入向量,但只能用我写的代码读取一行。你能建议更好的代码来读取输入吗?

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        char c;
        cin>>n;
        getchar();
        vector<int> V;
       while((c = getchar()) != '\n'){ 
            if(atoi(&c)!=0) V.push_back(atoi(&c));
       }
return 0;
}
4

1 回答 1

2
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <limits>
using namespace std;

int main(){
    int t;
    cin >> t;
    while (t--){
        int n, j;
        cin >> n;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        string s;
        getline(cin, s);
        istringstream iss(s);
        vector<int> V;
        while (iss >> j){ 
            V.push_back(j);
       }
    }
    return 0;
}
于 2021-06-20T05:34:13.470 回答