【问题标题】:Store size of array is not known数组的存储大小未知
【发布时间】:2019-06-30 17:39:03
【问题描述】:

我想创建程序来反转数组。但是我收到一个错误,数组a[] 的存储大小未知。

#include<iostream>
using namespace std;

int main()
{
    int a[];
    int b, c;
    cin >> b;
    for (int i = 0; i < b; i++)
        cin >> a[i];
    for (c = b; c >= 0; c--)
        cout << a[c] << endl;

    return 0;
}

【问题讨论】:

  • 错误信息有什么不清楚的地方?您没有指定数组的大小。
  • @ShivamSharma std::vector。您是否发现这是您研究的一部分?
  • @ShivamSharma 这是我们保留的list
  • 每天都会问这个问题。每一天。请在发帖前做一些研究!
  • for(c=b ... b 超出范围。在这种情况下你需要c=b-1

标签: c++ arrays algorithm c++11 reverse


【解决方案1】:

我想创建程序来反转数组

只需使用 std::vector 即可。

一个好的开始应该是:

#include <iostream>
#include <vector>

int main()
{
    std::size_t size;
    std::cin >> size;        // size of the array    
    std::vector<int> a(size);// allocate memory for provided size and 
                             // initialize with 0's
    // using range based loop iterate though referances of
    // each elements in the array and update with user input.
    for (int& element : a) std::cin >> element;

    return 0;
}

【讨论】:

    【解决方案2】:

    扩展JeJoanswer

    后者简单、优雅且高效——适用于与int 类似的任何类型。但是,如果您正在处理更复杂的类型,那么使用这种方法,您将首先默认初始化所有元素,然后复制或移动分配最终对象,您很可能希望避免这种情况。在这种情况下,以下方法更胜一筹:

    std::vector<SomeComplexType> v;
    v.reserve(NumberOfObjectsYouNeed);
    
    // appropriate loop definition here!
    {
        v.emplace_back(/* the parameters you want to/must provide to constructor */);
    }
    

    【讨论】:

      【解决方案3】:

      您没有定义a[] 数组的大小,因此出现了错误消息。数组必须具有指定的大小。在您的情况下,您需要使用 new[] 运算符在您确定用户的大小后分配数组,例如:

      #include <iostream>
      
      int main() {
          int *a;
          int b;
      
          std::cin >> b;
          a = new int[b];
      
          for(int i = 0; i < b; ++i)
              std::cin >> a[i];
      
          for(int c = b - 1; c >= 0; --c)
              std::cout << a[c] << std::endl;
      
          delete[] a;
          return 0;
      }
      

      但是,在 C++ 中使用动态大小的数组的首选方法是使用标准的 std::vector 容器,例如:

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <iterator>
      
      int main() {
          std::vector<int> a;
          int b;
      
          std::cin >> b;
          a.reserve(b);
      
          std::copy_n(
              std::istream_iterator<int>(std::cin), n,
              std::back_inserter(a)
          );
      
          std::for_each(a.rbegin(), a.rend(),
              [](int i){ std::cout << i << std::endl; }
          );
      
          /* alternatively:
          std::reverse(a.begin(), a.end());
          for(int i : a)
              std::cout << i << std::endl;
          */
      
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        您需要指定数组的最大大小 只需通过分配数组的长度来尝试 前任 诠释a[5];

        【讨论】:

        • 不是那么简单 - 你以这种方式为数组施加最大长度,用户可能会输入更大的数字!因此,您还需要检查这种情况,并提供一些适当的错误处理。
        • 改用std::vector 查看答案。
        • @Aconcagua 你能给我推荐一本好的编程书来学习 C++ 编程或任何在线免费课程吗?
        • @ShivamSharma 看看here
        猜你喜欢
        • 1970-01-01
        • 2017-07-24
        • 1970-01-01
        • 1970-01-01
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        相关资源
        最近更新 更多