初始化后可以调整 C++ 数组的大小吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/756906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:07:11  来源:igfitidea点击:

Can you resize a C++ array after initialization?

c++arraysresizesizeof

提问by jkeys

I'm learning to program, and C++ is my first language. Don't bother using pointers to show me - I don't understand them yet, and won't bother until I have more free time to dedicate to this.

我正在学习编程,C++ 是我的第一门语言。不要费心使用指针来向我展示 - 我还不理解它们,并且在我有更多空闲时间致力于此之前不会打扰。

    int mergeSort()
{
    const int n = 9;
    int originalarray[n] = {1, 3, 5, 7, 9, 2, 4, 6, 8};


    const int halfelements = (sizeof(originalarray) / sizeof(int)) / 2;
    int farray[halfelements];
    int sarray[halfelements];

    for (int i = 0; i < halfelements; i++) {
        farray[i] = originalarray[i];
    }

    for (int i = halfelements, x = 0; i < (halfelements * 2); i++, x++) {
        sarray[x] = originalarray[i];
    }

I was assigned (I'm not taking classes - just learning with a few friends helping me out) a merge sort algorithm, with the algorithm explained but not the implementation. I want to rewrite this so it will work for both odd and even integers. I tried adding this code:

我被分配了(我没有上课 - 只是和几个帮助我的朋友一起学习)一个合并排序算法,算法解释了但没有实现。我想重写它,以便它适用于奇数和偶数整数。我尝试添加此代码:

if ((n % 2) != 0) int farray[halfelements + 1];

So that I could use the same integer to iterate over both subsequent arrays. A sizeof(farray) is showing to be 16 bytes, or 4 integers. So it isn't resizing. What I want to know - is it possible to resize arrays after they initialized?

这样我就可以使用相同的整数来迭代两个后续数组。sizeof(farray) 显示为 16 个字节,或 4 个整数。所以它没有调整大小。我想知道 - 初始化后是否可以调整数组大小?

Edit: How would I implement a vector? I don't understand how to use iterators in a loop to iterate over and copy the values.

编辑:我将如何实现向量?我不明白如何在循环中使用迭代器来迭代和复制值。

回答by Legion

C++ arrays are fixed in size.

C++ 数组的大小是固定的。

If you need a "resizable array", you'll want to use std::vectorinstead of an array.

如果您需要“可调整大小的数组”,则需要使用std::vector而不是数组。

回答by Dаn

My advice is even stronger: use std::vector<>(et. al.) unless you have a very good reason to use a C-style array. Since you're learning C++, I doubt you have such a reason: use std::vector<>.

我的建议更强烈:使用std::vector<>(et. al.) 除非您有充分的理由使用 C 风格的数组。既然你正在学习 C++,我怀疑你有这样的理由:使用std::vector<>.

回答by Gregor Brandt

I would also recommend std::vector. However if you are stuck with an array you can always mallocthe memory and then reallocif you need to make the array larger.

我也会推荐std::vector。但是,如果您坚持使用数组,则始终可以malloc使用内存,然后realloc如果需要使数组变大。

Do a search here on SO, there is information about mallocand realloc.

在这里搜索 SO,有关于malloc和 的信息realloc

回答by Gordon Gustafson

You can use the [] operator with a vector the same way you would in an array. You could implement this with a vector something like this (if you wanted to use more vector methods):

您可以像在数组中一样将 [] 运算符与向量一起使用。你可以用一个像这样的向量来实现这个(如果你想使用更多的向量方法):

#include <vector>

const int halfelements = originalarray.size()/2; //use size to get size
vector <int> farray(halfelements);
vector <int> farray(halfelements);

for (int i = 0; i < halfelements; i++) {
    farray.push_back(originalarray[i]); //adds element at i to the end of vector
}

for (int i = halfelements, x = 0; i < (halfelements * 2); i++, x++) {
    sarray.push_back(originalarray[i]);
}

You can also use .at(index) to add bounds checking to the vector access.

您还可以使用 .at(index) 为向量访问添加边界检查。

回答by tstenner

If you want to resize an array, you probably want to use a vector, which can be resized automatically.

如果要调整数组大小,您可能需要使用可以自动调整大小的向量。

回答by Ken

If you want to know why your first idea compiled but didn't seem to work:

如果你想知道为什么你的第一个想法被编译但似乎不起作用:

When you omit braces in an if-statement:

当您在 if 语句中省略大括号时:

if ((n % 2) != 0) int farray[halfelements + 1];

it's just the same as if you'd used them:

这就像你使用过它们一样:

if ((n % 2) != 0) {
  int farray[halfelements + 1];
}

So it is making an 'farray' of the correct size -- and then it immediately goes out of scope and is gone, and you're left with only the original one.

所以它正在制作一个正确大小的“farray”——然后它立即超出范围并消失了,你只剩下原始的了。