用C ++调整数组大小? [英] Resizing arrays in C++?

查看:108
本文介绍了用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 ++调整数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆