1. 程式人生 > >移除元素(remove,remove_if...unique...)

移除元素(remove,remove_if...unique...)

重載 name bin pla move return 而是 algorithm 叠代器

remove

  因為本算法作用的是iterator,所以並不會改變Container大小,會返回一個新的iterator new_last,是的first到new_last中的元素都不等於value,左端元素的相對位置不變

template <class ForwardIterator,class T>
ForwardIterator remove(ForwardIterator first,ForwardIterator last,const T& value);

remove_if

  移除pred(*i)為true的元素

template <class ForwardIterator,class
Predicate> ForwardIterator remove_if(ForwardIterator first,ForwardIterator last,Predicate pred);

remove_copy

  不會復制值與value相等元素,返回值是result的尾端,被復制的元素的相對位置不改變

template <class ForwardIterator,class OutputIterator,class T>
ForwardIterator remove_copy(ForwardIterator first,ForwardIterator last,OutputIterator result,
const T& value);

remove_copy_if

template <class ForwardIterator,class OutputIterator,class Predicate>
ForwardIterator remove_copy_if(ForwardIterator first,ForwardIterator last,OutputIterator result,Predicate pred);

unique

  元素去重。即”刪除”序列中所有相鄰的重復元素(只保留一個)。並不是真的刪除,而是指重復元素的位置被不重復的元素給占領。把不重復的元素移到前面來,未被移除的元素的相對位置不改變。

  返回值是一個叠代器,它指向的是去重後容器中不重復序列的最後一個元素的下一個元素

//版本一:重載operator==,如果*i==*(i-1)則有重復的元素 
template <class ForwardIterator>
ForwardIterator unique(ForwardIterator fisrt,ForwardIterator last);

//版本二:自定義函數對象cmp(*i,*(i-1))為true,有重復
template <class ForwardIterator,class BinaryPredicate>
ForwardIterator unique(ForwardIterator fisrt,ForwardIterator last,BinaryPredicate cmp); 

unique_copy

//版本一:重載operator==,如果*i==*(i-1)則有重復的元素 
template <class InputIterator,class OutputIterator>
OutputIterator unique_copy(ForwardIterator fisrt,ForwardIterator last,OutputIterator result);

//版本二:自定義函數對象cmp(*i,*(i-1))為true,有重復
template <class ForwardIterator,class OutputIterator,class BinaryPredicate>
OutputIterator unique_copy(ForwardIterator fisrt,ForwardIterator last,OutputIterator result,BinaryPredicate cmp); 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <iterator>
using namespace std;

class F
{
    public:
        bool operator()(int i)
        {
            return i&1==1;
        }    
};
class F1
{
    public:
        F1(string t):s1(t){}
        bool operator()(string s)
        {
            return !strcmp(s.c_str(),s1.c_str());
        }
    private:
        string s1;
};
int main()
{
    vector<int> v{1,2,3,4,5,6};
    //auto it=remove_if(v.begin(),v.end(),F());
    //cout<<*--it<<endl;
    //v.erase(remove_if(v.begin(),v.end(),F()),v.end());
    
    vector<string> vs{"hello"," ","word!","how"," ","you","."};
    remove_copy_if(vs.begin(),vs.end(),ostream_iterator<string>(cout," "),F1("you"));
    /*for(auto i:vs)
        cout<<i<<‘ ‘;
    cout<<endl;*/
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class F
{
    public:
        F(vector<int> t):_v(t){}
        bool operator()(int i,int j)
        {
            if(count(_v.begin(),_v.end(),i)!=1)
                return true;
            else
                return false;
        }
    private:
        vector<int> _v;
};
int main()
{
    //用sort,unique函數去除相鄰重復的元素 
    vector<int> arr{1,2,3,4,5,6,5,6};
    sort(arr.begin(),arr.end(),greater<int>());
    arr.erase(unique(arr.begin(),arr.end()),arr.end());//unique返回new_last,其中不包含重復的元素 
    for_each(arr.begin(),arr.end(),[](int i)
    {
        cout<<i<< ;
    });
    cout<<endl;
    
    vector<int> arr1{1,2,3,4,4,5,6,5,6};
    arr1.erase(unique(arr1.begin(),arr1.end(),F(arr1)),arr1.end());
    for_each(begin(arr1),end(arr1),[](int i)
    {
        cout<<i<< ;
    });
    cout<<endl;
    return 0;

}

移除元素(remove,remove_if...unique...)