我可以假设记忆是连续的吗? [英] Can I assume the memory is continuous?

查看:72
本文介绍了我可以假设记忆是连续的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我使用矢量迭代器时,我通常会这样做:


vector< int> :: iterator itr = vec.begin( );

for(; itr!= vec.end(); ++ itr){

}


注意我我在比较itr是否等于vec.end()。我想知道我是否可以使用小于:


for(; itr< vec.end(); ++ itr){

}


我知道这个系统并不好用虚拟内存不是因为内存可以被分段所以使用
。但是如果我们只假设程序

只在虚拟内存系统上运行,那么可以使用小于来编写代码

。我对吗?正如我在虚拟内存中看到的分配

内存总是连续的。

解决方案

linq936写道:




当我使用矢量迭代器时,我通常会这样做:


vector< int> :: iterator itr = vec.begin();

for(; itr!= vec.end(); ++ itr){

}


注意我正在比较itr是否等于vec.end()。我想知道我是否可以使用小于:


for(; itr< vec.end(); ++ itr){

}


我知道这个系统并不好用虚拟内存不是因为内存可以被分段所以使用
。但是如果我们只假设程序

只在虚拟内存系统上运行,那么可以使用小于来编写代码

。我对吗?正如我在虚拟内存中看到的分配

内存总是连续的。



当然。但要注意,如果你将容器更改为

std :: list,它就不会起作用。为什么你真的想要''<'',

呢?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


向量保证元素位于

内存的1个连续块中。


因此对于向量,你可以使用iter< vector.end()。

因为迭代器充当智能指针,所以它总能工作。


它不适用于像map这样的容器,设置,列表,因为有

不能保证元素在内存中的位置。

Adrian

On 2007-09-13 13:11:15 -0400,Adrian< nn ** @ bluedreamer.comsaid:


Vectors保证元素在$ 1
内存的1个连续块中。


因此对于矢量,你可以使用iter< vector.end()。



问题不在于内存是否在一个块中,而在于是否

容器'' s iterator是一个随机访问迭代器。那是

向量和deque(它不保证连续块)所必需的。

随机访问迭代器支持<。其他迭代器没有。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


Hi,
When I use a vector iterator I normally do this way:

vector<int>::iterator itr = vec.begin();
for (; itr != vec.end(); ++itr ){
}

Note I am comparing if itr equals to vec.end(). I am wondering if I
could use "less than":

for (; itr < vec.end(); ++itr ){
}

I know this is not good in the system that virtual memory is not
used since memory can be segmented. But if we just assume the program
only runs on virtual memory system, then it is ok to write the code
using "less than". Am I right? As I see in virtual memory allocated
memory is always continuous.

解决方案

linq936 wrote:

Hi,
When I use a vector iterator I normally do this way:

vector<int>::iterator itr = vec.begin();
for (; itr != vec.end(); ++itr ){
}

Note I am comparing if itr equals to vec.end(). I am wondering if I
could use "less than":

for (; itr < vec.end(); ++itr ){
}

I know this is not good in the system that virtual memory is not
used since memory can be segmented. But if we just assume the program
only runs on virtual memory system, then it is ok to write the code
using "less than". Am I right? As I see in virtual memory allocated
memory is always continuous.

Sure. Beware, though, that if you change your container to, say,
std::list, it''s not going to work. Why do you really want the ''<'',
anyway?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Vectors guarantee that the elements are in 1 contiguous block of
memory.

So for vectors you can use iter < vector.end().

Since iterators act as smart pointers this will always work.

It will not work for containers like map, set, list though as there is
not such guarantee about where elements are located in memory.
Adrian


On 2007-09-13 13:11:15 -0400, Adrian <nn**@bluedreamer.comsaid:

Vectors guarantee that the elements are in 1 contiguous block of
memory.

So for vectors you can use iter < vector.end().

The question isn''t whether the memory is in one block, but whether the
container''s iterator is a random access iterator. That''s required for
vector and for deque (which doesn''t guarantee a contiguous block).
Random access iterators support <. Other iterators do not.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于我可以假设记忆是连续的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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