简体   繁体   English

非常数数组

[英]Non-Constant Array

I'm trying to create an array using a class object, and the array size can vary. 我正在尝试使用类对象创建数组,并且数组大小可能会有所不同。 What I'm doing is creating a "Library" using C++ on Visual Studio 2015, and the information comes from a text file. 我正在做的是在Visual Studio 2015上使用C ++创建一个“库”,信息来自文本文件。 The first line of the file holds the number of books, and I've got the program to read the first line and set that to an integer variable. 文件的第一行包含书籍的数量,并且我有程序读取第一行并将其设置为整数变量。 I'm trying to get a constant integer to be set as the size equal to the previous integer, but when I try to create the array, it tell's me it's not a constant. 我试图将一个常量整数设置为等于前一个整数的大小,但是当我尝试创建数组时,它告诉我它不是一个常量。

int numBooks;
    inputfile >> numBooks;

    const int SIZE = numBooks;
    Library records[SIZE]; //"Expression must have a constant value"

What do I need to do here to get this to work. 我需要在这里做什么才能使其正常工作。 The number of books will change, so must the size of the array. 书籍的数量将改变,数组的大小也必须改变。

Don't use a raw array. 不要使用原始数组。 The size of a raw array must be known at compile time. 原始数组的大小必须在编译时知道。 Use std::vector instead: 使用std::vector代替:

int numBooks;
inputfile >> numBooks;
std::vector<Library> records(numBooks);

Chances are that you won't need to tell the vector an initial size. 您可能不需要告诉向量初始大小。 Just tell it to grow with each item: 只需告诉它随每个项目一起增长即可:

int numBooks;
inputfile >> numBooks;
std::vector<Library> records;
// ...
records.push_back(book);

Also note that Library is a bad name for this class. 还要注意, Library对于此类来说是一个坏名字。 It should probably be called Book instead. 它可能应该称为Book

const int SIZE = numBooks;

Must be initialized at compile time, hence you can't initialize it. 必须在编译时进行初始化,因此无法对其进行初始化。

The best alternative is to use a std::vector : 最好的替代方法是使用std::vector

size_t numBooks;
cin >> numBooks;
std::vector<Library> records(numBooks);

Note: 注意:

In contrary to what @Christian Hackl stated in their answer , in many cases (especially with user defined types) it is better to use something along these lines: @Christian Hackl回答相反,在许多情况下(尤其是对于用户定义的类型),最好使用以下方式:

size_t numBooks;
cin >> numBooks;
std::vector<Library> records;
records.reserve(numBooks);

and add elements there with std::vector::emplace_back(); 并在其中添加std::vector::emplace_back();

std::vector<Library> records(numBooks);

would call the default constructor for Library numBooks times and later you would come up manipulating the vector elements through copy operations or such. 将为Library numBooks时间调用默认的构造函数,稍后您将通过复制操作或类似操作来操纵矢量元素。

std::vector::emplace_back() would allow to move newly constructed Library records directly to the vector, and std::vector::reserve() will guarantee that no memory reallocation needs to take place (which could become costly performance wise). std::vector::emplace_back()将允许将新构造的Library记录直接移动到vector,而std::vector::reserve()将保证无需进行内存重新分配(这可能会导致性能损失) 。

For a large number of records read from a file that would be definitely worth to consider. 对于从文件中读取的大量记录,绝对值得考虑。

The value of SIZE and so the size of the array is not initialized until the runtime, therefore which is not according to how C++ works. SIZE的值以及数组的大小直到运行时才被初始化,因此这与C ++的工作方式不符。

You must assign a fixed, real integer value when initializing an array. 初始化数组时,必须分配一个固定的实整数值。

Use vector otherwise. 否则使用vector。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM