c++ - 用输入表示数组中元素的数量吗?

标签 c++ arrays element

这是输入:

输入文件的第一行包含一个整数T,它表示测试用例的数量。对于每个测试用例,将有两行。第一行包含N,它表示数组中元素的数量,第二行包含N个空格分隔的整数。

我知道如何表示测试用例的数量,但不知道如何表示数组中元素的数量。
我对c++还是很陌生,所以如果您回答noob friendly会很好。

最佳答案

应该是这样的:

cin>>test;                                // Taking the number of test cases 
    while(test--){                        // For each test case
        cin>>n;                           // Taking input n
        vector<int> a(n);
        for(i=0;i<n;i++){
            cin>>a[i];                   // Taking input as n integers
        }
    }

OR

cin>>test;                                     // Taking the number of test cases  
    for(int i=0;i<test;i++){                   // For each test case
        cin>>n;                                // Taking input n
        vector<int> v;                         // declaring a vector
        for(int j=0;j<n;j++){
            cin>>x;
            v.push_back(x);                    // Taking input as n integers one by one
        }
    }

关于c++ - 用输入表示数组中元素的数量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62288535/

相关文章:

Java 二维数组填充模式

list - 在 Flutter/Dart 中映射多个列表?

c++ - Opengl 数组纹理,适用于原始数据但不适用于图像数据

c++ - Visual Studio 中的英特尔编译器未找到 boost 库

c++ - 结构数组的内存分配错误

css - 谷歌浏览器的 "Inspect Element": CSS Hints?

C++ vector.erase() 函数错误

c++ - 构造函数中的段错误

objective-c - obj-c 从数组中获取字符串

php - 将 $_POST 中包含的数组转换为 Yii 框架中的 MySql 查询