帮助向量< vector< double>> [英] help with vector<vector<double>>

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

问题描述




我正在努力解决如何初始化向量< vector< double>>

对象的问题。我正在从文件中提取数据并将其存储在

向量< vector< double>对象中。因为任何给定的文件都会有大量的数据,我使用ifstream对象读取,我不想使用push_back方法因为这个动态地增加

向量< vector< double>这会杀死我的执行

时间。所以,我想首先保留空间,当然使用储备

方法。但是,我不确定这样做的最佳方式是什么。

这就是我想做的事情(我甚至都不知道这是否会这样做

工作)但如果有更好的方法,我会全力以赴:


#include< vector>

int nColumns = 10;

int nRows = 15;


vector< vector< double> myData;

myData.reserve(nRows);


for(int i; i< nRows; i ++){

myData [i] .reserve (nColumns);

}

谢谢,

trevis

解决方案
吨。 Crane写道:


#include< vector>


int nColumns = 10;

int nRows = 15;


vector< vector< double> myData;


myData.reserve(nRows);


for(int i; i< nRows; i ++){

myData [i] .reserve(nColumns);

}



此代码已损坏。 vector :: reserve()实际上并没有创建或

初始化向量中的对象;它只为对象分配空间

,它们尚未使用push_back()或其他方式插入。作为

结果,即使在myData.reserve()调用之后,myData也不包含任何项目

而myData [i]指的是一个不存在的项目。


正如Alf所说,你可能根本不需要保留。首先,使其正确。

然后,只有在必要时,才能加快速度。


Phil


< blockquote> 8月9日下午1:17,Philip Potter< p ... @ removethis.doc.ic.ac.ukwrote:


T. Crane写道:


#include< vector>


int nColumns = 10;

int nRows = 15;


vector< vector< double> myData;


myData.reserve(nRows);


for(int i; i< nRows; i ++){

myData [i] .reserve(nColumns);

}



此代码已损坏。 vector :: reserve()实际上并没有创建或

初始化向量中的对象;它只为对象分配空间

,它们尚未使用push_back()或其他方式插入。作为

结果,即使在myData.reserve()调用之后,myData也不包含任何项目

而myData [i]指的是一个不存在的项目。


正如Alf所说,你可能根本不需要保留。首先,使其正确。

然后,只有在必要时,才能加快速度。


Phil



好​​的 - 这有助于一些。然后是一个相关的问题:如果我有一个向量

对象并且我事先没有保留任何空间,我理解如果

我将push_back值放入向量中,这将导致

矢量对象超过其初始容量。此时,

向量将识别一个足够大的连续内存块

以容纳原始向量和附加数据。然后它会将自己的副本和新数据复制到新的

内存块中。这基本上是它的工作方式吗?


如果是,我称之为动态增长矢量。这与

形成鲜明对比,保留了一大块内存,这些内存足够大,可以在开始时保留所有你需要的东西,然后使用
$填充它b $ b push_back。通过这种方式,没有理由走出去寻找新的大块连续记忆。


这是否澄清了我得到的东西?是的,我明白我可以先把它弄好,然后再把它弄得更快,但如果我能同时做到这两件事,那为什么不呢?


感谢您的帮助,

trevis


T。 Crane写道:

:: 8月9日下午1:17,Philip Potter< p ... @ removethis.doc.ic.ac.uk>

: :写道:

::: T. Crane写道:

:::: #include< vector>

:::

:::: int nColumns = 10;

:::: int nRows = 15;

:::

:::: vector< vector< double> myData;

:::

:::: myData.reserve(nRows);

:::

:::: for(int i; i< nRows; i ++){

:::: myData [i] .reserve(nColumns);

::::}

:::

:::此代码已损坏。 vector :: reserve()实际上并没有创建或者

:::初始化向量中的对象;它只为

:::使用push_back()或

:::无法插入的对象分配空间。因此,即使在myData.reserve()调用之后,

::: myData也不包含任何项目,myData [i]指的是

:::不存在。

:::

:::正如Alf所说,你可能根本不需要保留。首先,使它成为

:::正确。然后,只有在必要的时候才能加快速度。

:::

::: Phil

::

::好的 - 这有助于一些人。然后是一个相关的问题:如果我有一个

:: vector对象并且我事先没有保留任何空间,因为我是

::如果我push_back值了解它进入向量,这将导致

::导致某个向量对象超过其初始值
:: capacity。此时,向量将识别一个连续的

::内存块,其大小足以容纳原始的

:: vector以及其他数据。然后它会将

::本身和新数据的副本复制到新的内存块中。这是

::基本上它是如何工作的?


关于对,是的。它还使用了额外的功能,例如每次复制数据时,它会分配越来越大的块,内存越来越大,因此它不需要再制作额外的副本对于

很长时间。


如果你的真实尺码是10和15,你绝对不必担心。

如果你拥有数百万个元素,你可能需要这样做。但是你

必须先尝试一下!

Bo Persson



Hi,

I''m struggling with how to initialize a vector<vector<double>>
object. I''m pulling data out of a file and storing it in the
vector<vector<double>object. Because any given file will have a
large amount of data, that I read off using an ifstream object, I
don''t want to use the push_back method because this grows the
vector<vector<double>dynamically, and that will kill my execution
time. So, I want to reserve space first, using, of course the reserve
method. However, I''m not sure what the best way of doing this is.
Here''s what I am thinking of doing (I don''t even know if this will
work) but if there''s a better way to do it, I''m all ears:

#include <vector>

int nColumns = 10;
int nRows = 15;

vector<vector<double>myData;

myData.reserve(nRows);

for (int i;i<nRows;i++){
myData[i].reserve(nColumns);
}
thanks,
trevis

解决方案

T. Crane wrote:

#include <vector>

int nColumns = 10;
int nRows = 15;

vector<vector<double>myData;

myData.reserve(nRows);

for (int i;i<nRows;i++){
myData[i].reserve(nColumns);
}

This code is broken. vector::reserve() doesn''t actually create or
initialize objects in the vector; it only allocates space for objects
which have yet to be inserted using push_back() or whatever. As a
result, even after the myData.reserve() call, myData contains no items
and myData[i] refers to an item which doesn''t exist.

As Alf says, you may not need to reserve at all. First, make it correct.
Then, only if necessary, make it faster.

Phil


On Aug 9, 1:17 pm, Philip Potter <p...@removethis.doc.ic.ac.ukwrote:

T. Crane wrote:

#include <vector>

int nColumns = 10;
int nRows = 15;

vector<vector<double>myData;

myData.reserve(nRows);

for (int i;i<nRows;i++){
myData[i].reserve(nColumns);
}


This code is broken. vector::reserve() doesn''t actually create or
initialize objects in the vector; it only allocates space for objects
which have yet to be inserted using push_back() or whatever. As a
result, even after the myData.reserve() call, myData contains no items
and myData[i] refers to an item which doesn''t exist.

As Alf says, you may not need to reserve at all. First, make it correct.
Then, only if necessary, make it faster.

Phil

OK -- that helps some. A related question then: if I have a vector
object and I don''t reserve any space beforehand, as I understand it if
I push_back values into the vector, this will cause at some point the
vector object to exceed its initial capacity. At this point the
vector will identify a contiguous block of memory that is large enough
to accomodate the original vector plus the additional data. It will
then make a copy of itself and the new data into the new block of
memory. Is this basically how it works?

If it is, I call this growing the vector dynamically. This is in
contrast to reserving one chunk of memory that''s big enough to hold
everything you want at the beginning and then populating it using
push_back. In this manner, there is never a reason to go out and find
a new chunk of contiguous memory.

Does this clarify what I''m getting at? And yes, I understand that I
could get it right first and then get it faster, but if I can do both
at once, why not?

thanks for your help,
trevis


T. Crane wrote:
:: On Aug 9, 1:17 pm, Philip Potter <p...@removethis.doc.ic.ac.uk>
:: wrote:
::: T. Crane wrote:
:::: #include <vector>
:::
:::: int nColumns = 10;
:::: int nRows = 15;
:::
:::: vector<vector<double>myData;
:::
:::: myData.reserve(nRows);
:::
:::: for (int i;i<nRows;i++){
:::: myData[i].reserve(nColumns);
:::: }
:::
::: This code is broken. vector::reserve() doesn''t actually create or
::: initialize objects in the vector; it only allocates space for
::: objects which have yet to be inserted using push_back() or
::: whatever. As a result, even after the myData.reserve() call,
::: myData contains no items and myData[i] refers to an item which
::: doesn''t exist.
:::
::: As Alf says, you may not need to reserve at all. First, make it
::: correct. Then, only if necessary, make it faster.
:::
::: Phil
::
:: OK -- that helps some. A related question then: if I have a
:: vector object and I don''t reserve any space beforehand, as I
:: understand it if I push_back values into the vector, this will
:: cause at some point the vector object to exceed its initial
:: capacity. At this point the vector will identify a contiguous
:: block of memory that is large enough to accomodate the original
:: vector plus the additional data. It will then make a copy of
:: itself and the new data into the new block of memory. Is this
:: basically how it works?

About right, yes. It also uses additional features, like for each time
the data is copied it allocates increasingly larger and larger blocks
of memory, so that it will not have to make additional copies for a
long time.

If your real sizes are 10 and 15, you definitely don''t have to worry.
If you have 100s of millions of elements, you might have to. But you
will have to try it out first!
Bo Persson



这篇关于帮助向量&lt; vector&lt; double&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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