删除元素的算法 [英] Algorithm for removing elements

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

问题描述

我知道c ++有擦除删除习惯用法。 < algorithm> 下的删除方法会将目标元素移到范围的后面。



但是,以下输出使我感到困惑。

  #include< iostream> 
#include< vector>
#include< algorithm>

使用命名空间std;

int main(){
vector< int> vec = {10,20,30,20,30,20,10,10,20};

auto pend = remove(vec.begin(),vec.end(),20);
cout<< 删除20后:<<恩德尔
for(const auto& x:vec){
cout<< x<< ;
}
cout<<恩德尔

cout<< 请使用:<<恩德尔
for(auto p = vec.begin(); p!= pend; p ++){
cout<< << * p;
}

cout<<恩德尔
返回0;
}

输出为:

 删除20后:
10 30 30 10 10 20 10 10 20
需使用:
10 30 30 10 10

此处有两个问题:


  1. 对于删除20之后,为什么后面有10和20混合?
    10 30 30 10 10 20 10 10 20


  2. 对于使用pend: ,为什么它不能再打印最后两个10?原始向量中有五个10,而不应删除10?


从库中删除()方法返回垂悬的迭代器



模板
ForwardIterator remove(ForwardIterator首先,ForwardIterator最后,const T& val);
到最后一个未删除元素之后的元素的迭代器。
first和此迭代器之间的范围包括序列中所有不等于val的元素。

解决方案

从数组中:

  10 20 30 20 30 20 10 10 20 

删除所有 20 时,您期望得到:

  10 30 30 10 10 

但是 std :: remove 只是移动项目,它让其余值未指定


迭代器指向新逻辑端与逻辑端之间的元素范围的物理末端仍可取消引用,但元素本身具有未指定的值


因此,您得到:

  10 30 30 10 10 xx xx xx xx 
^
pend

哪个解释了您的r

如果需要删除项目,请致电 vec.erase(pend,vec.end())



< blockquote>

通常先调用remove,然后再调用容器的擦除方法,该方法将擦除未指定的值并减小容器的物理大小以匹配其新的逻辑大小。



I understand there is erase-remove idiom for c++. And the remove method under <algorithm> will move target elements to the back of the range.

However, the output below is confusing to me.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    vector<int> vec = {10, 20, 30, 20, 30, 20, 10, 10, 20};

    auto pend = remove(vec.begin(), vec.end(), 20);
    cout << "After removing 20: " << endl;
    for (const auto& x : vec) {
        cout << x << " ";
    }
    cout << endl;

    cout << "use pend: " << endl;
    for (auto p = vec.begin(); p != pend; p++) {
        cout << " " << *p;
    }

    cout << endl;
    return 0;
}

The output is:

After removing 20:
10 30 30 10 10 20 10 10 20
use pend:
10 30 30 10 10

There are two questions here:

  1. For "After removing 20", why are there 10 mixed with 20 at the back? 10 30 30 10 10 20 10 10 20

  2. For "use pend:", why does it fail to print the last two more 10's? There are five 10's in the original vector, and 10's are not supposed to be removed?

From the library, remove() method returns the iterator pend

template ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); An iterator to the element that follows the last element not removed. The range between first and this iterator includes all the elements in the sequence that do not compare equal to val.

解决方案

From the array:

10 20 30 20 30 20 10 10 20

When you remove all 20, you expect to get:

10 30 30 10 10

But std::remove just shifts the items, it lets the remaining values unspecified:

Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values

So you get:

10 30 30 10 10 xx xx xx xx
               ^
               pend

Which explains your results.

If you need to erase the items, call vec.erase(pend, vec.end()):

A call to remove is typically followed by a call to a container's erase method, which erases the unspecified values and reduces the physical size of the container to match its new logical size.

这篇关于删除元素的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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