您可以调整 C++ 的大小吗? 初始化后的数组?
我正在学习编程,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
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.
我的建议更加强烈:使用
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: usestd::vector<>
.如果要调整数组的大小,您可能需要使用可以自动调整大小的向量。
If you want to resize an array, you probably want to use a vector, which can be resized automatically.
您可以将 [] 运算符与向量一起使用,就像在数组中一样。 您可以使用类似这样的向量来实现此功能(如果您想使用更多向量方法):
您还可以使用 .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):
You can also use .at(index) to add bounds checking to the vector access.
我还推荐
std::vector
。 但是,如果您受困于数组,您可以随时malloc
内存,然后realloc
(如果您需要使数组更大)。在这里搜索一下SO,有关于
malloc
和realloc
的信息。I would also recommend
std::vector
. However if you are stuck with an array you can alwaysmalloc
the memory and thenrealloc
if you need to make the array larger.Do a search here on SO, there is information about
malloc
andrealloc
.如果你想知道为什么你的第一个想法编译但似乎不起作用:
当你在 if 语句中省略大括号时:
它就像你使用它们一样:
所以它正在制作一个“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:
it's just the same as if you'd used them:
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.