矢量问题的矢量 [英] Vector of vector question

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

问题描述

如果我创建一个double的向量向量:


std :: vector<的std ::矢量<双> > table1;


我的双打向量是否未初始化?我是否必须遍历table1

并使用new初始化每个双打向量?


在清理时,手动删除这些双打的向量?


谢谢,

B

解决方案

" BCC" <峰; br *** @ akanta.com>在消息中写道

news:gv ***************** @ newssvr27.news.prodigy.co m ...

如果我创建了double的向量向量:

std :: vector<的std ::矢量<双> > table1;

我的双打矢量未初始化?



编号。默认初始化。你有一个空向量的空向量。

我是否必须遍历table1
并使用new初始化每个双精度向量?


号。请。

在清理时,手动删除这些双打矢量?


再没有。 std :: vector是一个设计良好的类,不需要很多

手持。

谢谢,
B



-

Cy
http://home.rochester.rr.com/cyhome/


文章< gv *** **************@newssvr27.news.prodigy.com> ,BCC写道:

如果我创建了一个双向量矢量:

std :: vector<的std ::矢量<双> > table1;

我的双打矢量未初始化?


他们是空的。 table1.size()将产生0并且table1 [0] .size导致

未定义的行为(因为还没有第一个元素)

我是否必须循环table1
并使用new初始化每个双打向量?


你不使用new,vector类管理它自己的存储。你使用vector

成员函数。如果你想要向量有一些条目,你需要

做这样的事情:

typedef std :: vector< double> :: size_type dvecsize;

dvecsize m = 10,n = 5;

std :: vector<的std ::矢量<双> > table1(m,n);

std :: cout<< table1.size()<<的std :: ENDL; // 10

std :: cout<< table1 [0] .size()<<的std :: ENDL; // 5

在清理时,手动删除这些双打矢量?




不,矢量的析构函数要小心解除分配存储空间那是

有一个向量的要点。


干杯,

-

Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord /


" BCC" <峰; br *** @ akanta.com>写道:

如果我创建了一个双向量矢量:

std :: vector<的std ::矢量<双> > table1;

我的双打矢量未初始化?我是否必须循环使用table1
并使用new初始化每个双打矢量?

在清理时,手动删除这些双打矢量?




由向量创建的任何双打都将被初始化为0.0,他们不需要
需要新,也不要删除。


您确定要使用矢量矢量吗?如果我需要一个粗糙的阵列,我只会这样做

。如果数组表示一个表,那么创建一个2D数组类会更好。请参阅常见问题解答以获取示例

实施。


If I create a vector of vectors of double:

std::vector< std::vector<double> > table1;

Are my vectors of doubles uninitialized? Do I have to loop through table1
and initialize each vector of doubles using new?

And in cleaning up, manually delete each of these vectors of doubles?

Thanks,
B

解决方案

"BCC" <br***@akanta.com> wrote in message
news:gv*****************@newssvr27.news.prodigy.co m...

If I create a vector of vectors of double:

std::vector< std::vector<double> > table1;

Are my vectors of doubles uninitialized?

No. They are default initialized. You have an empty vector of empty vectors.
Do I have to loop through table1
and initialize each vector of doubles using new?
No. Please.

And in cleaning up, manually delete each of these vectors of doubles?
No again. std::vector is a well designed class that doesn''t require a lot of
handholding.

Thanks,
B



--
Cy
http://home.rochester.rr.com/cyhome/


In article <gv*****************@newssvr27.news.prodigy.com> , BCC wrote:

If I create a vector of vectors of double:

std::vector< std::vector<double> > table1;

Are my vectors of doubles uninitialized?
They''re empty. table1.size() will produce 0 and table1[0].size results in
undefned behaviour (since there is no first element yet)
Do I have to loop through table1
and initialize each vector of doubles using new?
You don''t use new, the vector class manages its own storage. You use vector
member functions. If you want the vectors to have some entries, you need to
do something like this:
typedef std::vector<double>::size_type dvecsize;
dvecsize m = 10, n = 5;
std::vector< std::vector<double> > table1 (m,n);
std::cout << table1.size() << std::endl; // 10
std::cout << table1[0].size() << std::endl; // 5
And in cleaning up, manually delete each of these vectors of doubles?



No, the destructor of the vector takes care of deallocating storage. That''s
the main point of having a vector.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/


"BCC" <br***@akanta.com> wrote:

If I create a vector of vectors of double:

std::vector< std::vector<double> > table1;

Are my vectors of doubles uninitialized? Do I have to loop through table1
and initialize each vector of doubles using new?

And in cleaning up, manually delete each of these vectors of doubles?



Any doubles created by the vector will be initialized to 0.0, they don''t
need to be ''new''ed nor ''delete''ed.

Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class. See the FAQ for a sample
implementation.


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

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