C++ 如何在c++;? #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main(){ int T; cin>>T; 字符输入[1000]; 对于(inti=0;i

C++ 如何在c++;? #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main(){ int T; cin>>T; 字符输入[1000]; 对于(inti=0;i,c++,C++,尝试这样的方法 #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int T; cin >> T; char input[1000]; for(int i=0;i<T;i++)

尝试这样的方法

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int T;
    cin >> T;
    char input[1000];
    for(int i=0;i<T;i++)
    {
        cin.getline(input,sizeof(input));
        cout << input << "\n";
    }
    return 0;
}

您可以这样做,使用
string
而不是
char[]

while(cin.getline(input,sizeof(input))){
}
输出:

3 Can I have a large container of coffee right now Can I have a large container of tea right now Now I wish I could recollect pi Eureka cried the great inventor Christmas Pudding Christmas Pie Is the problems very center
尝试:

intmain(){
int T;
cin>>T;
std::字符串温度;
std::字符串输入;

对于(int i=0;i,您的问题是第一个输入运算符:

int main() {
    int T;
    cin >> T;
    std::string temp;
    std::string input;
    for(int i=0;i<T;i++)
    {
        while(std::getline(std::cin,temp)) {
        input += temp;
    }
    std::cout << input << endl;
    return 0;
}
它读取一个数字并将其作为整数存储在T中,但在流中保留换行符。现在循环将读取三行,但第一行将是空行

您可以通过多种方式解决此问题,最简单的方法是在获取数字后丢弃换行符:

cin >> T;

更好的方法是检查文件结尾,而不是提前获取行数。并使用
std::string
代替其他人建议的字符数组。

为什么不使用
string
代替
char[]
?代码中的T是什么?T是测试用例的数量阅读
std:istream::getline
的文档。它使用换行符作为分隔符。也就是说,一旦遇到换行符,它就会停止读取。我认为@Nawaz是正确的。
int main() {
    int T;
    cin >> T;
    std::string temp;
    std::string input;
    for(int i=0;i<T;i++)
    {
        while(std::getline(std::cin,temp)) {
        input += temp;
    }
    std::cout << input << endl;
    return 0;
}
cin >> T;
cin >> T;
cin.getline(input, sizeof input);