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

发布于 2024-07-17 08:47:35 字数 879 浏览 7 评论 0原文

我正在学习编程,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];
    }

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

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

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

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

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.

    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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

︶葆Ⅱㄣ 2024-07-24 08:47:35

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

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

C++ arrays are fixed in size.

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

今天小雨转甜 2024-07-24 08:47:35

我的建议更加强烈:使用 std::vector (等),除非您有充分的理由使用 C 样式数组。 既然你正在学习C++,我怀疑你有这样的理由:使用std::vector

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<>.

愁以何悠 2024-07-24 08:47:35

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

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

吐个泡泡 2024-07-24 08:47:35

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

#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]);
}

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

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.

北斗星光 2024-07-24 08:47:35

我还推荐 std::vector。 但是,如果您受困于数组,您可以随时malloc内存,然后realloc(如果您需要使数组更大)。

在这里搜索一下SO,有关于mallocrealloc的信息。

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

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

不顾 2024-07-24 08:47:35

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

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

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

它就像你使用它们一样:

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

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

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 ((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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文