用C ++调整数组大小?

编程入门 行业动态 更新时间:2024-10-28 07:26:14
本文介绍了用C ++调整数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我该怎么做? 我正在做一个练习题,其中包括我作为一个班级实施一套 整数。我不想使用矢量,因为那将是作弊的b $ b。 ;)我不想花足够的时间来制作一个 树集,所以我要做一个数组。它会有一个静态的 const int默认大小,并且它将有一个成员私有的 阈值。一旦集合的大小超过阈值,下一次调用它的任何操作将调整它的大小,使它的当前大小为 的两倍。 所以唯一的困难是,如何调整阵列的大小?我知道我可以使用c'的malloc和realloc如果我包含了库,但是我不知道这是不是正确的方法C ++中的东西。 任何帮助都非常感激,一如既往。

How do I do it? I''m doing a practice problem which includes me implementing a set of ints as a class. I don''t want to use vector because that would be cheating. ;) I don''t want to spend enough time on it to make it a treeset, so I''m just gonna do an array set. It''s gonna have a static const int default size, and it''s going to have a member private int threshold. Once the size of the set passes the threshold, the next call to any operation on it will resize it to double whatever its current size is. So the only difficulty is, how do I resize the array? I know that I could use c''s malloc and realloc if I included the libraries, but I don''t know if this is the right way to do things in C++. Any help is much appreciated, as always.

推荐答案

Blue Ocean写道: Blue Ocean wrote: 我该怎么做? 我正在做一个练习题,其中包括我将一组整体作为一个类实现。我不想使用矢量,因为那会是作弊。 ;)我不想花太多时间把它变成一个树集,所以我要做一个数组。它将具有静态的 const int默认大小,并且它将具有成员私有的阈值。一旦设置的大小超过阈值,下一次调用它的任何操作都会调整它的大小,使它的当前大小变为两倍。 所以唯一的困难是,我该如何调整阵列的大小?我知道如果我包含库,我可以使用c'的malloc和realloc,但我不知道这是否是用C ++做事的正确方法。 How do I do it? I''m doing a practice problem which includes me implementing a set of ints as a class. I don''t want to use vector because that would be cheating. ;) I don''t want to spend enough time on it to make it a treeset, so I''m just gonna do an array set. It''s gonna have a static const int default size, and it''s going to have a member private int threshold. Once the size of the set passes the threshold, the next call to any operation on it will resize it to double whatever its current size is. So the only difficulty is, how do I resize the array? I know that I could use c''s malloc and realloc if I included the libraries, but I don''t know if this is the right way to do things in C++.

嗯,malloc()和realloc()是C的一部分。当你处理POD数据时,没有真正的问题。如果你使用new,那么你将有 将数据复制到新块。所以选择方法并继续使用它。我会 使用* alloc()系列,如果我不得不做这个问题。 别忘了你需要实现至少 的[]和=运算符使它看起来像一个数组。如何处理越界访问权限 由您决定。 Brian Rodenborn

Well, malloc() and realloc() are part of C. As you dealing with POD data (ints) there''s no real problem doing so. If you use new, then you''d have to copy the data to the new block. So pick method and go with it. I''d use the *alloc() family, personally if I had to do this problem. Don''t forget you need to implement the [] and = operators at the least to make it look work an array. What to do with out of bounds access is up to you. Brian Rodenborn

Blue Ocean写道: Blue Ocean wrote: 我该怎么办? 做什么? 我正在做一个练习题,包括我在课堂上实施一套整体。我不想使用矢量,因为那会是作弊。 ;)我不想花太多时间把它变成一个树集,所以我要做一个数组。它将具有静态的 const int默认大小,并且它将具有成员私有的阈值。一旦设置的大小超过阈值,下一次调用它的任何操作都会调整它的大小,使它的当前大小变为两倍。 所以唯一的困难是,我该如何调整阵列的大小?我知道如果我包含库,我可以使用c'的malloc和realloc,但我不知道这是否是用C ++做事的正确方法。 How do I do it? Do what? I''m doing a practice problem which includes me implementing a set of ints as a class. I don''t want to use vector because that would be cheating. ;) I don''t want to spend enough time on it to make it a treeset, so I''m just gonna do an array set. It''s gonna have a static const int default size, and it''s going to have a member private int threshold. Once the size of the set passes the threshold, the next call to any operation on it will resize it to double whatever its current size is. So the only difficulty is, how do I resize the array? I know that I could use c''s malloc and realloc if I included the libraries, but I don''t know if this is the right way to do things in C++.

使用新的。如果需要调整大小,请使用new分配另一个块,复制数据并取消分配旧块。这就是realloc通常 的确如此。

Use new. If you need to resize, allocate another block with new, copy the data over and deallocate the old block. That''s what realloc usually does, too.

默认用户写道: 嗯,malloc()和realloc()是C的一部分。 Well, malloc() and realloc() are part of C.

该死的。我想说的是C ++的一部分。 Brian Rodenborn

Damn. Are part of C++ I meant to say. Brian Rodenborn

更多推荐

用C ++调整数组大小?

本文发布于:2023-11-30 11:15:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!