stl矢量问题 [英] stl vector problem

查看:60
本文介绍了stl矢量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图通过手动分配/解除分配内存块来加快stl向量的性能。当我尝试释放内存时,一个版本的

代码崩溃了。另一个版本似乎是
工作。我很感激有人对此发表评论。


版本1(解除分配时崩溃)


#include< iostream>

#include< vector>

#include< stdlib.h>


使用命名空间std;


int main(int argc,char ** argv)

{

vector< double *> vec;

double * ary1 = new double [5];

for(int i = 0; i< 5; i ++)

{

vec.push_back(& ary1 [i]);

}


double * ary2 = new double [5] ;

for(i = 0; i< 5; i ++)

{

vec.push_back(& ary2 [i]);

}


for(i = 0; i< vec.size(); i ++)

* vec [i] = double(i);


for(i = 0; i< vec.size(); i ++)

cout<< * vec [i]<< endl;


//崩溃!!

for(i = 0; i< vec.size(); i ++)

{

double * p = vec [i];

delete p; //释放每个元素,一次一个

}

}


版本2:似乎有效,但是它好吗?


#include< iostream>

#include< vector>

#include< stdlib.h>


使用命名空间std;


int main(int argc,char ** argv)

{

vector< double *> tab;

vector< double *> vec;


double * ary1 = new double [5];


for(int i = 0; i< 5; i ++)< br $>
{

vec.push_back(& ary1 [i]);

}


tab .push_back(ary1);


double * ary2 = new double [5];


for(i = 0; i< 5; i ++)

{

vec.push_back(& ary2 [i]);

}


tab.push_back(ary2);


for(i = 0; i< vec.size(); i ++)

* vec [i] = double(i);


for(i = 0; i< vec.size(); i ++)

cout<< * vec [i]<< endl;


for(i = 0; i< tab.size(); i ++)

delete [] tab [i]; //删除为两个数组


for(i = 0; i< vec.size(); i ++)

{

cout<< vec [i]<< endl;

}


for(i = 0; i< vec.size(); i ++)

{

cout<< * vec [i]<<结束;

}


}

解决方案

luigi写道:



我试图通过手动分配/解除分配内存块来加快stl向量的性能。当我试图释放内存时,一个版本的代码崩溃了。另一个版本似乎工作。我很感激有人对此发表评论。

版本1(在解除分配时崩溃)

#include< iostream>
#include< vector>
#include< stdlib.h>

使用命名空间std;


向量< double *> vec;
double * ary1 = new double [5];


new#1

for(int i = 0; i< 5; i ++)
{
vec.push_back(& ; ary1 [i]);
}

double * ary2 = new double [5];


new#2

for(i = 0; i< 5; i ++)
{
vec.push_back(&) ary2 [i]);
}

for(i = 0; i< vec.size(); i ++)
* vec [i] = double(i);

for(i = 0; i< vec.size(); i ++)
cout<< * vec [i]<<结束;

//崩溃!!
for(i = 0; i< vec.size(); i ++)
{
double * p = vec [我];
删除p; //释放每个元素,一次一个


????删除5件事????

}
}


新增和删除需要匹配。

版本2 :好像有效,但是好吗?

#include< iostream>
#include< vector>
#include< stdlib.h>

使用命名空间std;
int main(int argc,char ** argv)
{
vector< double *> tab;
vector< double *> vec;

double * ary1 = new double [5];


** new#1

for(int i = 0; i< 5; i ++)
{
vec.push_back (& ary1 [i]);
}

tab.push_back(ary1);

double * ary2 = new double [5];


** new#2

for(i = 0; i< 5; i ++)
{
vec.push_back( & ary2 [i]);
}

tab.push_back(ary2);

for(i = 0; i< vec.size(); i ++)
* vec [i] = double(i);

for(i = 0; i< vec.size(); i ++)
cout<< * vec [i]<< endl;

for(i = 0; i< tab.size(); i ++)
delete [] tab [i]; //删除为两个数组


再次删除 - 比新闻更多。

for(i = 0; i< vec.size(); i ++)
{
cout<< vec [i]<<对于(i = 0; i< vec.size(); i ++)
{
cout<< * vec [i]<< endl;
}

}




我有个你的想法 - 在你开始制作之前衡量表现

优化。


" luigi" < LC **** @ yahoo.com>在留言中写道

news:27 ************************** @ posting.google.c om ...



我试图通过手动分配/解除分配内存块来加快stl向量的性能。当我试图释放内存时,一个版本的代码崩溃了。另一个版本似乎工作。我很感激有人对此发表评论。




我认为你的生活变得不必要了。假设你首先遇到了一个性能问题,并且性能问题是由std :: vector分配内存(?)的方式引起的,那么为什么不使用

std :: vector :: reserve()可以更有效地分配内存吗?

版本1有一些非常严重的问题,表明你

不完全理解你在做什么。

首先如果你用''new double [5]分配一些东西,''它必须是

deallocated用''删除[] p;''。其次,你不能从一个分配了''new double [5];''的数组中一次删除一个单独的
元素,你可以

只删除整个数组。我认为你无法将版本1

变成合理的东西。


我建议你先尝试更好地理解C ++之前

你开始进行棘手的优化。我的第二个建议是
,只有当应用程序的性能不可接受时才能优化代码,并证明性能瓶颈在于

您正在尝试优化的代码。我已经看到太多无用的优惠尝试失败,导致代码有缺陷,难以维持,并且很少或没有比清洁未经优化的更快的

版。


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl


lc****@yahoo.com (luigi)在留言新闻中写道:< 27 ************************** @ posting.google。 com> ...



我试图通过手动分配/解除分配内存块来加快stl向量的性能。



这不会阻止向量分配和释放内存。你调用push_back的每个

时间你的向量可能会分配另一个
内存的块。如果你知道你想要你的矢量有多大,那么只要你创造它就可以了。

。例如


std :: vector< double> vec(5);


vec长5个元素,每个元素包含0.0(

默认初始值为double)。


std :: vector< double> vec2(5,42.42);


vec长5个元素,每个元素包含42.42。


这样做我认为你尝试的一切去做。另请查看

向量成员函数保留,容量和调整大小。


< snip>


hth

GJD


Hi,

I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version seem to
work. I would appreciate someone to comment on this.

Version 1 (crashes on deallocating)

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
vector<double *> vec;
double *ary1 = new double[5];
for(int i=0;i<5;i++)
{
vec.push_back(&ary1[i]);
}

double *ary2 = new double[5];
for(i=0;i<5;i++)
{
vec.push_back(&ary2[i]);
}

for(i=0;i<vec.size();i++)
*vec[i]=double(i);

for(i=0;i<vec.size();i++)
cout << *vec[i] << endl;

// crash!!
for(i=0;i<vec.size();i++)
{
double *p = vec[i];
delete p; // free each element, one at a time
}
}

Version 2: seem to work, but is it good?

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
vector<double *> tab;
vector<double *> vec;

double *ary1 = new double[5];

for(int i=0;i<5;i++)
{
vec.push_back(&ary1[i]);
}

tab.push_back(ary1);

double *ary2 = new double[5];

for(i=0;i<5;i++)
{
vec.push_back(&ary2[i]);
}

tab.push_back(ary2);

for(i=0;i<vec.size();i++)
*vec[i]=double(i);

for(i=0;i<vec.size();i++)
cout << *vec[i] << endl;

for(i=0;i<tab.size();i++)
delete [] tab[i]; // delete as two arrays

for(i=0;i<vec.size();i++)
{
cout << vec[i] << endl;
}

for(i=0;i<vec.size();i++)
{
cout << *vec[i] << endl;
}

}

解决方案

luigi wrote:

Hi,

I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version seem to
work. I would appreciate someone to comment on this.

Version 1 (crashes on deallocating)

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
vector<double *> vec;
double *ary1 = new double[5];
new # 1
for(int i=0;i<5;i++)
{
vec.push_back(&ary1[i]);
}

double *ary2 = new double[5];
new # 2
for(i=0;i<5;i++)
{
vec.push_back(&ary2[i]);
}

for(i=0;i<vec.size();i++)
*vec[i]=double(i);

for(i=0;i<vec.size();i++)
cout << *vec[i] << endl;

// crash!!
for(i=0;i<vec.size();i++)
{
double *p = vec[i];
delete p; // free each element, one at a time
???? delete 5 things ????
}
}
new and delete need to match.

Version 2: seem to work, but is it good?

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
vector<double *> tab;
vector<double *> vec;

double *ary1 = new double[5];
** new # 1

for(int i=0;i<5;i++)
{
vec.push_back(&ary1[i]);
}

tab.push_back(ary1);

double *ary2 = new double[5];
** new # 2

for(i=0;i<5;i++)
{
vec.push_back(&ary2[i]);
}

tab.push_back(ary2);

for(i=0;i<vec.size();i++)
*vec[i]=double(i);

for(i=0;i<vec.size();i++)
cout << *vec[i] << endl;

for(i=0;i<tab.size();i++)
delete [] tab[i]; // delete as two arrays
again - more deletes than news.

for(i=0;i<vec.size();i++)
{
cout << vec[i] << endl;
}

for(i=0;i<vec.size();i++)
{
cout << *vec[i] << endl;
}

}



I have an idea for you - measure the performance BEFORE you start making
an optimization.


"luigi" <lc****@yahoo.com> wrote in message
news:27**************************@posting.google.c om...

Hi,

I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version seem to
work. I would appreciate someone to comment on this.



I think you are making your life unnecessary hard. Assuming you have a
performance problem in the first place and the performance problem is
caused by the way std::vector allocates memory(?), why not just use
std::vector::reserve() for more efficient memory allocation?

Version 1 has some very serious problems that are an indication that you
do not fully understand what you are doing.
First if you allocate something with ''new double[5];'' it must be
deallocated with ''delete[] p;''. Secondly you cannot delete an individual
element at a time from an array allocated with ''new double[5];'', you can
only delete the complete array. I see no way you could turn Version 1
into something reasonable.

I recommend that you first try gain a better understanding of C++ before
you start doing tricky optimizations. The second recommendation I have
is to optimize code only if the perfomance of the application is
unacceptable and it is proven that the performance bottleneck is in the
code you are trying to optimize. I have seen too many futile
optimization attempts fail, resulting in code that is buggy, hard to
maintain and little or no faster that than the clean unoptimized
version.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


lc****@yahoo.com (luigi) wrote in message news:<27**************************@posting.google. com>...

Hi,

I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually.



That won''t stop the vector allocating and deallocating memory. Every
time you call push_back your vector might allocate another chunck of
memory. If you know how big you want your vector to be, just make it
that big when you create it. For example

std::vector<double> vec(5);

vec is 5 elements long and each element contains 0.0 (the
default-initialisation value for double).

std::vector<double> vec2(5, 42.42);

vec is 5 elements long and each element contains 42.42.

This does everything I think you were trying to do. Also look at the
vector member functions reserve, capacity and resize.

<snip>

hth
GJD


这篇关于stl矢量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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