简体   繁体   English

更改函数以使用向量而不是数组

[英]Changing a function to use vectors instead of arrays

I'm getting a little stuck changing functions to use vectors. 我在使用向量更改函数时遇到了一些困难。 I've got the below function that creates a new dynamic array. 我有以下创建新动态数组的函数。 Now I need to do the same but use vectors. 现在我需要做同样的事情,但是要使用向量。

int *makeData(int size)
{
    int *ptr = nullptr;
    ptr = new int[size];
    return ptr;
}

This is all i got so far and can't seem to figure out what to do next. 这是到目前为止我所能做的一切,似乎无法弄清楚下一步该怎么做。 Do i need a pointer to return? 我需要一个指针才能返回吗?

int *makeData(int size)
{
    vector<int> data(100);
}

This function seems to be okay but is there a way to optimize it (make it simpler, and cleaner). 此功能似乎还可以,但是有一种方法可以对其进行优化(使其更简单,更清洁)。

void getMovieData2(vector<int> data, int num)
{
int temp;
cout << "Enter the number of movies each student saw.\n";

for (int count = 0; count < num; count++)
{
    cout << "Student " << (count + 1) << ": ";
    cin >> temp;
    data.push_back (temp);

    while (arr[count] <= 0)
    {
        cout << "The number of movies be greater than zero.\n";
        cout << "How many movies were seen by Student " 
             << (count + 1) << "? ";
        cin >> temp;
        data.push_back (temp);
    }
}
}

Using a vector has made the makeData function redundant. 使用向量使makeData函数变得多余。 It doesn't do anything that simply declaring a variable of type vector doesn't already do better. 它并没有做任何事情,仅仅声明一个类型为vector的变量并没有做得更好。

Cleaning up the other function is possible, but has nothing to do with the question about vectors. 清理其他功能是可能的,但与向量的问题无关。

Also, none of this has anything to do with overloading. 而且,这些都与过载无关。

Your makeData for vectors would be 向量的makeData将是

vector<int> makeData(int size) {
    return vector<int>(size);
}

As you see it is effectively the same as 如您所见,它实际上与

vector<int> data(size);

However it can be useful if you use auto consistently 但是,如果您始终使用自动,它会很有用

auto data = makeData(size);

As of your getMovieData function: no, that function is not working as expected. getMovieData函数开始:否,该函数无法正常工作。 You are passing a vector as value and are "returning" data in it - except that you are putting your data into a local copy that will be destroyed when the function returns. 您正在传递向量作为值,并在其中“返回”数据-除非您将数据放入本地副本中,该副本将在函数返回时销毁。

The solution is to use a reference, as in 解决方案是使用参考,如

void getMovieData(vector<int> &outData, int count);

However I suggest you simply return a new vector 但是我建议您只需返回一个新向量

vector<int> getMovieData(int count);

The capacity of vector object can be either reserved or resized. 向量对象的容量可以保留,也可以调整大小。 The reserve function allocates additional memory for internal storage. 保留功能为内部存储分配额外的内存。

While the resize function sets the size of maximum possible elements in vector. 调整大小功能设置矢量中最大可能元素的大小。

Examples can be found here: 示例可以在这里找到:

http://www.cplusplus.com/reference/vector/vector/resize/ http://www.cplusplus.com/reference/vector/vector/resize/

http://www.cplusplus.com/reference/vector/vector/reserve/ http://www.cplusplus.com/reference/vector/vector/reserve/

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

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