【问题标题】:initialise an array with user input使用用户输入初始化数组
【发布时间】:2015-04-02 16:16:07
【问题描述】:

我正在使用 Visual Studio 2013、c++、控制台应用程序。很长一段时间以来,我一直在为一个问题苦苦挣扎。我想知道是否有一种方法可以通过用户输入来初始化数组,例如:

我有一个数组:int arr[] = { 3, 7, 5, 9, 1};。因此,我希望初始化值是用户输入。

有什么办法可以做到吗?我们将不胜感激所有帮助和 cmets。

这是我的代码: cout > 元素;

cout << "Enter the difference value: ";
cin >> difference;

cout << "Enter the sequence of elements:  ";

vector<int> arr(elements);


for (int i = 0; i < elements; i++)
{
    cin >> arr[i];

}
//the following needs to have an array
//in their respective functions.
sorter(arr[], elements);
elementsDifference(arr[], elements, difference);

这个程序必须遍历一个数组并找到具有给定差异的对。

【问题讨论】:

    标签: arrays c++11 visual-studio-2013 console-application


    【解决方案1】:

    怎么样

    int arr[10] , i;
    for ( i = 0 ; i < 10 ; i++ ) 
       std::cin >> a[i];
    

    这个简单的代码片段将从用户那里获取 10 个输入并将它们存储在数组中。

    如果你想改变输入的数量,你可以在for循环中改变条件(还要确保你的数组有足够的大小来存储所有的值)。

    更新

    你可以这样试试

    int size;
    cin >> size;
    int a[size],i;
    for ( i = 0 ; i < size ; i++ )
      cin >> a[i];
    for ( i = 0 ; i < size ; i++ )
      cout << a[i] << endl; 
    

    通常,人们只会将数组的大小设置得非常大(比如a[100000] 左右),然后接受这个大小,并使用类似于我上面给出的代码填充数组。

    但更好的方法是使用 vector 。你应该学习使用vector

    【讨论】:

    • 但是,在我的问题中,数组的大小取决于用户的输入,例如,输入元素的大小:然后用户输入元素的数量,这将如何工作? @Arun A.S
    【解决方案2】:

    如果你需要 C++ 中的变长数组,你应该使用std::vector:

    std::cout << "Enter the number of elements: ";
    int n;
    std::cin >> n;
    std::vector<int> ints;
    ints.reserve(n);
    
    for (int i = 0; i < n; ++i)
    {
        std::cout << "Enter element #" << i + 1 << ": ";
        int element;
        std::cin >> element;
        ints.push_back(element);
    }
    

    【讨论】:

    • 我确实尝试过,但没有运气,这是我的代码,它可能有助于解释我真正在寻找什么:
    • cout > 元素; cout > 差异; cout arr(元素); for (int i = 0; i > arr[i]; // 这是我需要输入数字序列的地方 } //下面需要有一个数组 //在它们各自的函数中。排序器(arr [],元素);元素差异(arr [],元素,差异);
    • 您可以使用arr.data()&amp;arr[0] 从不支持C++11 的向量中获取底层数组。例如。 sorter(arr.data(), elements).
    【解决方案3】:

    #包括

    int main() {

    char ch;
    std::cout << "Enter the size of the array\n";
    std::cin >> ch;
    std::cout<<"Enter the numbers you want in the array\n";
    switch (ch) {
    case '1': { int arr[1]; 
        int i;
        for (i = 0; i < 1; i++)
            std::cin >> arr[i];
        for (i = 0;i < 1;++i)
            std::cout << " " << arr[i];break;
    }
    case '2': { int arr[2]; 
        int i;
        for (i = 0; i < 2; i++)
            std::cin >> arr[i];
        for (i = 0;i < 2;++i)
            std::cout << " " << arr[i];break;
    }
    case '3': { int arr[3]; 
        int i;
        for (i = 0; i < 3; i++)
            std::cin >> arr[i];
        for (i = 0;i < 3;++i)
            std::cout << " " << arr[i];break;
    }
    case '4': { int arr[4]; 
        int i;
        for (i = 0; i < 4; i++)
            std::cin >> arr[i];
        for (i = 0;i < 4;++i)
            std::cout << " " << arr[i];break;
    }
    
            
    }
    

    }//等等...

    【讨论】:

    • 这只是一个错误代码的例子。不要这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2021-12-31
    • 2017-09-21
    相关资源
    最近更新 更多