创建一个可变大小但不可增长的数组 [英] Create a variable size but non-growable array

查看:74
本文介绍了创建一个可变大小但不可增长的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在STL中,我可以创建一个可变大小但不可增长的数组吗?


在Java中,我可以做这个:


int [] void f(int size){

int [] array = new int [size];

//用数组做一些事情

返回数组;

}


我想我可以使用vector<>为此,但我不需要阵列可以成长,我只是不知道前面的大小。所以我正在寻找一个更有效的数据结构。


谢谢。

Hi,

In STL, can I create a variable size but non-growable array?

In Java, I can do this:

int[] void f (int size) {
int[] array = new int[size];
// do something with array
return array;
}

I guess I can use vector<> for that, but I don''t need the array to be
growable, I just don''t know the size upfront. So I am looking for a
more efficient data structure.

Thank you.

推荐答案

yi*****@gmail.com 写道:
在STL中,我可以创建一个可变大小但不可增长的数组吗?


不在STL中。在C ++中。

在Java中,我可以这样做:

int [] void f(int size){
int [] array = new int [size ];
//用数组做某事
返回数组;
}


在C ++中你做了什么


int * void f(int size){

返回新的int [size];

}

我想我可以使用vector< >为此,


但是std :: vector的目的恰恰相反:它需要_growing_



但是我不需要阵列可以增长,


动态阵列永远不会可生长。

我只是不喜欢不知道前面的尺寸。所以我正在寻找一种更高效的数据结构。
In STL, can I create a variable size but non-growable array?
Not in STL. In C++.
In Java, I can do this:

int[] void f (int size) {
int[] array = new int[size];
// do something with array
return array;
}
In C++ you do

int* void f(int size) {
return new int[size];
}
I guess I can use vector<> for that,
But std::vector has exactly the opposite purpose: it''s _growing_ as
needed.
but I don''t need the array to be
growable,
Dynamic arrays are never "growable".
I just don''t know the size upfront. So I am looking for a
more efficient data structure.




要做什么?


V



To do what?

V


yi*****@gmail.com 写道:


在STL中,我可以创建一个可变大小但不可扩展的数组吗?

即使使用C ++,你也可以这样做。使用动态内存分配

在Java中,我可以这样做:
int [] void f(int size){


< OT>除此之外,一个函数不能有_two_返回类型 - 甚至在

Java< / OT>

int [] array = new int [size];
//用数组做某事
返回数组;
}


int你有的C ++

int * f(int size)

{

int * array = new int [size];

返回数组;

}


注意:你需要完成一次释放数组。

我想我可以使用vector<>为此,但我不需要阵列可以增长,我只是不知道前面的大小。所以我正在寻找一个
Hi,

In STL, can I create a variable size but non-growable array?
You can do that even with C++. Use dynamic memory allocation
In Java, I can do this:

int[] void f (int size) {
<OT> Aside, A function cannot have _two_ return types - not even in
Java </OT>
int[] array = new int[size];
// do something with array
return array;
}
int C++ you have
int* f(int size)
{
int * array = new int[size];
return array;
}

Note : you need to take care of freeing array exactly once.

I guess I can use vector<> for that, but I don''t need the array to be
growable, I just don''t know the size upfront. So I am looking for a




确保在哪里

1)你没有_need_数组可以增长

2)你没有_want允许数组可以增长。


除非是第二种情况,std :: vector更可取



Make sure wherether
1) You donot _need_ the array to be growable
2) You donot _want to allow_ the array to be growable.

unless it is second case, std::vector is preferable


好的,谢谢你的帮助。


假设我在c ++中这样做:

int * f(int size)

{

int * array = new int [size];

返回数组;


}


有没有安全的成语来防止内存泄漏?我应该在
C ++中使用auto_ptr吗?

或者我应该在其他库中使用其他智能指针模板吗?


谢谢。

Okay, thanks for the help.

Suppose I do this in c++:
int* f(int size)
{
int * array = new int[size];
return array;

}

Is there a safe idiom to prevent memory leak? Should i use auto_ptr in
C++?
Or should I use other smart pointer template in other libraries?

Thank you.


这篇关于创建一个可变大小但不可增长的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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