关于STL擦除功能的问题 [英] Question about STL erase function

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

问题描述

在有效的STL第9项在删除选项中仔细选择中,它有一个例子:


bool badValue(int x); //返回x是否为''坏''

c.erase(remove_if(c.begin(),c.end(),badValue),c.end()); //这个

是摆脱badValue返回true的对象的最好方法

c是vector,string或者dequeue


c.remove_if(badValue); //这是摆脱对象的最好方法

当c是列表时badValue返回true


我的问题是对于第一种情况,为什么我们需要再次调用c.erase()

并传入remove_if()的返回值?"没有调用

remove_if()从容器中移除项目(在这种情况下是向量)?

为什么我们需要再次调用erase()?


谢谢。

In Effective STL item 9 "Choose carefully among erasing options", it
has this example:

bool badValue(int x); // returns whether x is ''bad''
c.erase ( remove_if(c.begin(), c.end(), badValue), c.end()); // this
is the best way to get rid of objects where badValue returns true when
c is a vector, string, or dequeue

c.remove_if(badValue); // this is the best way to get rid of objects
where badValue returns true when c is a list

My question is "For the first case, why we need to call c.erase() again
and pass in the return value of remove_if()?" Doesn''t the call to
remove_if() remove the item from the container (in this case a vector)?
why we need to call erase() again?

Thank you.

推荐答案

Piotr写道:
在有效的STL第9项在删除选项中仔细选择中,它有这个例子:

bool badValue(int x); //返回x是否为''坏''
c.erase(remove_if(c.begin(),c.end(),badValue),c.end()); //当
c是向量,字符串或出队时,这是摆脱badValue返回true的对象的最佳方法

c.remove_if(badValue); //这是摆脱对象的最好方法
当c是列表时badValue返回true

我的问题是对于第一种情况,为什么我们需要调用c。擦除()再次
并传入remove_if()的返回值?"难道没有调用
remove_if()从容器中删除项目(在这种情况下是向量)吗?
为什么我们需要再次调用erase()?
In Effective STL item 9 "Choose carefully among erasing options", it
has this example:

bool badValue(int x); // returns whether x is ''bad''
c.erase ( remove_if(c.begin(), c.end(), badValue), c.end()); // this
is the best way to get rid of objects where badValue returns true when
c is a vector, string, or dequeue

c.remove_if(badValue); // this is the best way to get rid of objects
where badValue returns true when c is a list

My question is "For the first case, why we need to call c.erase() again
and pass in the return value of remove_if()?" Doesn''t the call to
remove_if() remove the item from the container (in this case a vector)?
why we need to call erase() again?




我相信你正在阅读的书有你的问题的答案。

std :: remove_if不会改变容器的大小 - 它不能,

因为你只传递了两个迭代器指定的范围。

相反,它返回一个新的迭代器,表示范围的新结束。

请看这里的解释:

http://www.sgi.com/tech/stl/remove_if.html


祝你好运,


Tom



I believe the book you are reading has the answer to your question.
std::remove_if does not change the size of the container - it couldn''t,
since you are only passing it a range specified by two iterators.
Instead, it returns a new iterator indicating the new end of the range.

See here for an explanation:

http://www.sgi.com/tech/stl/remove_if.html

Best regards,

Tom


" Piotr" < RA ************ @ gmail.com> schrieb im Newsbeitrag

新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ...
"Piotr" <ra************@gmail.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com...
在有效的STL第9项在删除选项中仔细选择中,它有这个例子:

bool badValue(int x); //返回x是否为''坏''
c.erase(remove_if(c.begin(),c.end(),badValue),c.end()); //当
c是向量,字符串或出队时,这是摆脱badValue返回true的对象的最佳方法

c.remove_if(badValue); //这是摆脱对象的最好方法
当c是列表时badValue返回true

我的问题是对于第一种情况,为什么我们需要调用c。擦除()再次
并传入remove_if()的返回值?"难道没有调用
remove_if()从容器中删除项目(在这种情况下是向量)吗?
为什么我们需要再次调用erase()?
In Effective STL item 9 "Choose carefully among erasing options", it
has this example:

bool badValue(int x); // returns whether x is ''bad''
c.erase ( remove_if(c.begin(), c.end(), badValue), c.end()); // this
is the best way to get rid of objects where badValue returns true when
c is a vector, string, or dequeue

c.remove_if(badValue); // this is the best way to get rid of objects
where badValue returns true when c is a list

My question is "For the first case, why we need to call c.erase() again
and pass in the return value of remove_if()?" Doesn''t the call to
remove_if() remove the item from the container (in this case a vector)?
why we need to call erase() again?




remove_if(first,last,pred)从序列中删除不需要的元素

[first,last]并将剩余的元素复制到
的位置
删除了元素。但它实际上并没有从序列的末尾丢弃未使用的空间

。对于删除的每个元素,您将获得另一个

元素的副本。基本上remove_if有点像


iterator free = first;

while(first!= last)

{

if(badValue(* first))

++ first;

else

* free ++ = * first ++;

}

免费退货;


HTH

Heinz



remove_if(first, last, pred) removes unwanted elements from the sequence
[first, last) and it copies the remaining elements to the places of the
removed elements. But it does not actually discard unused space from the end
of the sequence. For each element removed, you get a copy of another
element. Basically remove_if does something like

iterator free = first;
while (first != last)
{
if (badValue(*first))
++first;
else
*free++ = *first++;
}
return free;

HTH
Heinz

Piotr写道:

我的问题是对于第一种情况,为什么我们需要再次调用c.erase()
并传递返回值remove_if()?"难道没有调用
remove_if()从容器中删除项目(在这种情况下是向量)吗?
为什么我们需要再次调用erase()?

My question is "For the first case, why we need to call c.erase() again
and pass in the return value of remove_if()?" Doesn''t the call to
remove_if() remove the item from the container (in this case a vector)?
why we need to call erase() again?




算法适用于由迭代器对指定的序列。他们确实没有在集装箱上工作。 remove_if"删除"序列中的元素

通过在被拒绝的

之上复制替换元素来传递给它,并返回一个新的结束迭代器,指定结束

新序列。通常,该序列比原始序列短,

并且结束迭代器及其后指向的任何元素都与算法无关。

算法对序列的来源一无所知,

并且无法修改可能是

序列来源的容器那是传递给它的。但是因为它重新排列元素,你可以使用它返回的结束迭代器告诉你容器中剩余的

元素在哪里:它们是来自最终迭代器

算法返回到容器的实际末尾。所以你

擦掉''如果那是合适的话。


如果我没有做太多错别字,试试这个:


int data [] = {1,2,3,4,5,6,7}


struct is_odd

{

bool operator()(int val)

{

return val& 1;

}

};


int main()

{

int * begin = data;

int * end = data + sizeof(data)/ sizeof(* data);


std :: cout<< 原始序列:" ;;

std :: copy(begin,end,std :: ostream_iterator< int>(std :: cout,"");

std :: cout<<''\ n'';


int * new_end = std :: remove_if(begin,end,is_odd());

//无需擦除,''导致容器的大小固定


std :: cout<<""修改后的序列:" ;

std :: copy(begin,new_end,std :: ostream_iterator< int>(std :: cout,"");

std :: cout< ;<''\ n'';


std :: cout<<"原始容器:" ;;

std :: copy(begin,end,std :: ostream_iterator< int>(std :: cout,"");

std :: cout<<''\ n'';


返回0;

}


现在修改它以使用向量< int>而不是数组。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


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

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