std :: vector<> :: clear语义 [英] std::vector<>::clear semantics

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

问题描述




[这是我在

alt.comp.lang.learn.c-c ++中提出的一个类似问题的转贴]


因为我侦察,std :: vector :: clear()的语义从MS VC ++ 6.0改为

MS''DOT.NET - 编译器。


在6.0版本中,vector的capacity()没有随着

调用clear()而改变,在DOT.NET中的容量()减少到0.


我在调用clear()时依赖capacity()不会改变。这是因为

我不希望在下面的push_back()调用中有任何realloc,因为

我使用的迭代器应该保持有效。我在下面有一个例子。


正如我发现的,有些人声称clear()不需要保持

capacity()不变(正如我所想的那样)。所以我可能无法承担M $(但是我确实如此,因为他们默默地改变语义

并且因为传统而且)需要一个解决方法。


我知道我可以将clear()与reserve()结合使用。但是这个

会增加一些开销,分配和取消分配大量的内存,我想避免这种情况。


我也想避免给std :: vector的元素一个默认的

构造函数。目前,我使用resize(0)而不是clear(),但是这个

需要MS VC ++的lib中的默认构造函数。


是否有(保证工作)方式缩小std :: vector'的大小

没有

- 减少其容量()

- 要求向量元素是默认值 - 可构造




任何人都有线索?


Stefan。

示例:

---

std :: vector< int> v;

v.reserve(...);

while(...){

v.clear();

//填充矢量:

for(...)

v.push_back(...);


for(std :: vector< int> :: iterator i = v.begin(); i!= v.end(); / * nothing * /){

if( ......)

v.push_back(...); //我应该幸存下来因为预留()

if(...)

++ i;

else

i = v.erase(i);

}


/ *进一步使用v * /

}

---

Hi,

[this is a repost of a simmilar question I asked in
alt.comp.lang.learn.c-c++ recently]

as I recon, std::vector::clear()''s semantics changed from MS VC++ 6.0 to
MS'' DOT.NET - compiler.

In the 6.0 version the capacity() of the vector did not change with the
call to clear(), in DOT.NET the capacity() is reduced to 0.

I relied on capacity() not changing when calling clear(). This is because
I do not want any reallocs within the following push_back() calls because
I use iterators that should keep valid. I''ve got an example below.

As I dicovered, some people claim that clear() is not required to keep
capacity() unchanged (as I thought it was). So I probably can not
blame M$ (but I do, nevertheless, because they changed semantics
silently and because of tradition) and need a workaround.

I know that I can use clear() in combination with reserve(). But this
would add some overhead with allocating and deallocating huge amounts
of memory and I want to avoid that.

I also want to avoid to give the elements of the std::vector a default
constructor. In the moment, I use resize(0) instead of clear(), but this
needs an default constructor in MS VC++''s lib.

Is there a (guaranteed to work-) way to do shrink a std::vector''s size
without
- reducing its capacity()
- requiring the vectors elements to be default - constructable
?

Anybody a clue?

Stefan.
Example:
---
std::vector<int> v;
v.reserve(...);
while (...) {
v.clear();
//fill vector:
for (...)
v.push_back(...);

for(std::vector<int>::iterator i=v.begin(); i!=v.end();/*nothing*/) {
if(...)
v.push_back(...); // i should survive this because of reserve()
if(...)
++i;
else
i=v.erase(i);
}

/* some further use of v */
}
---

推荐答案

(但我确实如此,因为他们改变了语义

默默地,因为传统而且需要一个解决方法。


我知道我可以将clear()与reserve()结合使用。但是这个

会增加一些开销,分配和取消分配大量的内存,我想避免这种情况。


我也想避免给std :: vector的元素一个默认的

构造函数。目前,我使用resize(0)而不是clear(),但是这个

需要MS VC ++的lib中的默认构造函数。


是否有(保证工作)方式缩小std :: vector'的大小

没有

- 减少其容量()

- 要求向量元素是默认值 - 可构造




任何人都有线索?


Stefan。

示例:

---

std :: vector< int> v;

v.reserve(...);

while(...){

v.clear();

//填充矢量:

for(...)

v.push_back(...);


for(std :: vector< int> :: iterator i = v.begin(); i!= v.end(); / * nothing * /){

if( ......)

v.push_back(...); //我应该幸存下来因为预留()

if(...)

++ i;

else

i = v.erase(i);

}


/ *进一步使用v * /

}

---

(but I do, nevertheless, because they changed semantics
silently and because of tradition) and need a workaround.

I know that I can use clear() in combination with reserve(). But this
would add some overhead with allocating and deallocating huge amounts
of memory and I want to avoid that.

I also want to avoid to give the elements of the std::vector a default
constructor. In the moment, I use resize(0) instead of clear(), but this
needs an default constructor in MS VC++''s lib.

Is there a (guaranteed to work-) way to do shrink a std::vector''s size
without
- reducing its capacity()
- requiring the vectors elements to be default - constructable
?

Anybody a clue?

Stefan.
Example:
---
std::vector<int> v;
v.reserve(...);
while (...) {
v.clear();
//fill vector:
for (...)
v.push_back(...);

for(std::vector<int>::iterator i=v.begin(); i!=v.end();/*nothing*/) {
if(...)
v.push_back(...); // i should survive this because of reserve()
if(...)
++i;
else
i=v.erase(i);
}

/* some further use of v */
}
---


" Stefan H?hne" <做******************** @ 127.0.0.1>在消息中写道

news:bm ********** @ news1.wdf.sap-ag.de ...
"Stefan H?hne" <do********************@127.0.0.1> wrote in message
news:bm**********@news1.wdf.sap-ag.de...
[...]
是否有(保证工作)方式缩小std :: vector'的大小而不用
- 减少其容量()
- 需要vector元素默认 - 可构造
[...]
[...]
Is there a (guaranteed to work-) way to do shrink a std::vector''s
size without
- reducing its capacity()
- requiring the vectors elements to be default - constructable
[...]




一个极端的解决方案是编写一个自定义分配器

pre -allocates一个大块,永远不会释放它,直到你明确调用分配器的一个函数。也就是说,这将是
荣誉deallocate()调用,但不会取消分配它保留的实际

存储空间。


Dave


---

外发邮件经过无病毒认证。

由AVG反病毒系统检查( http://www.grisoft.com)

版本: 6.0.521 /病毒库:319 - 发布日期:2003年9月23日



An extreme solution would be to write a custom allocator that
pre-allocates a large chunk and never frees it until you make
an explicit call to a function of the allocator. That is, it would
honor deallocate() calls, but wouldn''t deallocate the actual
storage it reserved.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


2003年10月16日星期四09:24:56 + 0200,Stefan H?hne

< do ******************** @ 127.0.0.1>写道:
On Thu, 16 Oct 2003 09:24:56 +0200, Stefan H?hne
<do********************@127.0.0.1> wrote:


[这是我在
alt.comp.lang.learn.c-中提出的一个类似问题的转贴最近的c ++]

当我重新调试时,std :: vector :: clear()的语义从MS VC ++ 6.0变为了MS''DOT.NET - 编译器。


这是主题,我的意思是这不是MS文档的地方。

在6.0版本中,vector的容量()没有随着
调用clear()而改变,在DOT.NET中,capacity()减少到0.


这有点好笑。

我在调用clear()时依赖capacity()不会改变。这是因为我不想在下面的push_back()调用中使用任何realloc,因为
我使用的迭代器应该保持有效。我在下面有一个例子。


试图通过清除保持迭代器有效是另一个漂亮的技巧,

很好。

正如我发现的,有些人声称clear()不需要保持
capacity()不变(正如我想的那样)。所以我可能不能责怪M
Hi,

[this is a repost of a simmilar question I asked in
alt.comp.lang.learn.c-c++ recently]

as I recon, std::vector::clear()''s semantics changed from MS VC++ 6.0 to
MS'' DOT.NET - compiler.
This is of topic here, I mean this is not a place for MS documentation.

In the 6.0 version the capacity() of the vector did not change with the
call to clear(), in DOT.NET the capacity() is reduced to 0.
that''s kind of funny.

I relied on capacity() not changing when calling clear(). This is because
I do not want any reallocs within the following push_back() calls because
I use iterators that should keep valid. I''ve got an example below.
Trying to keep iterators valid through clear was another nifty trick,
nice.

As I dicovered, some people claim that clear() is not required to keep
capacity() unchanged (as I thought it was). So I probably can not
blame M


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

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