用std :: fill进行实验 [英] experiment with std::fill

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

问题描述



我已经分配了4K内存,我想用std :: fill用不同的值填充每个1K

。注意:我可以很容易地使用vector / deque但是

我对C风格的数组感兴趣。


int main()

{

int const max = 0x1000;

int * ptr_mem = new int [max];

int initial(1) ;

for(int idx(0); idx< 4; ++ idx)

{

std :: fill(ptr_mem, ptr_mem + 0x400,initial);

ptr_mem + = 0x400; //移动到下一个1K

初始+ = 1; //更改值

}


//调用显示函数将输出发送到文本 - 进行评估


delete [] ptr_mem;

ptr_mem = 0; //以防万一。

}


我来得很短。我确定问题的一部分是我对std :: fill的有限理解(在一分钟内回到文本上)。在

的同时,我将如何实现这一目标?

解决方案



ma740988写道:

我已经分配了4K内存,我想用std :: fill用不同的值填充每个1K
。注意:我可以很容易地使用vector / deque但是我对C样式数组感兴趣。

int main()
{
int const max = 0x1000;
int * ptr_mem = new int [max];
int initial(1);
for(int idx(0); idx< 4; ++ idx)
{
std :: fill(ptr_mem,ptr_mem + 0x400,initial);
ptr_mem + = 0x400; //移动到下一个1K
初始+ = 1; //更改值

//调用显示函数将输出发送到文本 - 进行评估

delete [] ptr_mem;
ptr_mem = 0; //以防万一。
}




看起来不错。有什么问题?


Michiel.Salt ... @ tomtom.com写道:

ma740988写道:< blockquote class =post_quotes>我已经分配了4K内存,我想用std :: fill用不同的值填充每个1K
。注意:我可以很容易地使用vector / deque但是我对C样式数组感兴趣。

int main()
{
int const max = 0x1000;
int * ptr_mem = new int [max];
int initial(1);
for(int idx(0); idx< 4; ++ idx)
{
std :: fill(ptr_mem,ptr_mem + 0x400,initial);
ptr_mem + = 0x400; //移动到下一个1K
初始+ = 1; //更改值

//调用显示函数将输出发送到文本 - 进行评估

delete [] ptr_mem;
ptr_mem = 0; //以防万一。
}



看起来不错。有什么问题?




除了ptr_mem从原来的位置改变了。除非在删除[]之前将
更改回来,否则会出现问题!我不敢问

为什么OP不能使用矢量(或者也许是boost :: array或静态

分配的数组)?

干杯! --M




mlimber写道:

我不敢问为什么OP不能使用向量(或者也许是boost :: array或静态分配的数组)?




我不知道为什么他不能用矢量。 boost :: array实际上并不是
标准,我不知道它是否会成为。它是否比图书馆中的矢量更便携?
?这是向量的重大缺点。


除非sizeof(int)在他的

系统中为1,否则他将超过他的4K内存。 br />



I''ve allocated 4K memory and I''d like to use std::fill to fill each 1K
with a different value. Note: I could easily use a vector/deque but
I''m interested in a C style array.

int main()
{
int const max = 0x1000;
int *ptr_mem = new int [ max ];
int initial(1);
for ( int idx(0); idx < 4; ++idx )
{
std::fill ( ptr_mem, ptr_mem + 0x400, initial );
ptr_mem += 0x400; // move to the next 1K
initial += 1; // change the value
}

// call a display function to send output to a text - for assessment

delete [] ptr_mem;
ptr_mem = 0; // just in case.
}

I''m coming up short. I''m sure part of the problem is my limited
understanding of std::fill (back to the text in a minute on this). In
the meantime, how would I achieve this?

解决方案


ma740988 wrote:

I''ve allocated 4K memory and I''d like to use std::fill to fill each 1K
with a different value. Note: I could easily use a vector/deque but
I''m interested in a C style array.

int main()
{
int const max = 0x1000;
int *ptr_mem = new int [ max ];
int initial(1);
for ( int idx(0); idx < 4; ++idx )
{
std::fill ( ptr_mem, ptr_mem + 0x400, initial );
ptr_mem += 0x400; // move to the next 1K
initial += 1; // change the value
}

// call a display function to send output to a text - for assessment

delete [] ptr_mem;
ptr_mem = 0; // just in case.
}



Looks OK. What''s the problem?


Michiel.Salt...@tomtom.com wrote:

ma740988 wrote:

I''ve allocated 4K memory and I''d like to use std::fill to fill each 1K
with a different value. Note: I could easily use a vector/deque but
I''m interested in a C style array.

int main()
{
int const max = 0x1000;
int *ptr_mem = new int [ max ];
int initial(1);
for ( int idx(0); idx < 4; ++idx )
{
std::fill ( ptr_mem, ptr_mem + 0x400, initial );
ptr_mem += 0x400; // move to the next 1K
initial += 1; // change the value
}

// call a display function to send output to a text - for assessment

delete [] ptr_mem;
ptr_mem = 0; // just in case.
}



Looks OK. What''s the problem?



Except that ptr_mem was changed from its original location. Unless it
is changed back before the delete[], there will be problems! Dare I ask
why the OP can''t use a vector (or perhaps boost::array or a statically
allocated array)?

Cheers! --M



mlimber wrote:

Dare I ask
why the OP can''t use a vector (or perhaps boost::array or a statically
allocated array)?



I don''t know why he can''t use vector. boost::array isn''t actually
standard and I don''t know if it''s ever going to be. Is it more portable
than vector across libraries? That''s the big downside of vector.

He will be exceeding his 4K of memory unless sizeof(int) is 1 on his
system.


这篇关于用std :: fill进行实验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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