STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)


所有容器适用
remove(b,e,v)           //[b,e) 删value
remove_if(b,e,p)        //[b,e) 删p条件
remove_copy(b,e,r,v)    //[b,e) 删v,结果存入r
remove_copy_if(b,e,r,p) //[b,e) 删p条件,结果存入r
注意:
    1、并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素
    2、返回新的逻辑终点


/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;

/*****************************************
//所有容器适用
remove(b,e,v)           //[b,e) 删value
remove_if(b,e,p)        //[b,e) 删p条件
remove_copy(b,e,r,v)    //[b,e) 删v,结果存入r
remove_copy_if(b,e,r,p) //[b,e) 删p条件,结果存入r
注意:
    1、并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素
    2、返回新的逻辑终点
*****************************************/
/**----------------------------------------------------------------------------------

----------------------------------------------------------------------------------**/
/*************************************************************************************
std::remove                     所有排序容器适用                           algorithm
--------------------------------------------------------------------------------------
template < class ForwardIterator, class T >
  ForwardIterator remove ( ForwardIterator first, ForwardIterator last,
                           const T& value );
//eg:
template < class ForwardIterator, class T >
  ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!(*first == value)) *result++ = *first;
  return result;
}
*************************************************************************************/

/*************************************************************************************
std::remove_if                      所有排序容器适用                        algorithm
--------------------------------------------------------------------------------------
template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred );
//eg:
template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}
*************************************************************************************/

/*************************************************************************************
std::remove_copy                    所有排序容器适用                        algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy ( InputIterator first, InputIterator last,
                               OutputIterator result, const T& value );
//eg:
template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy ( InputIterator first, InputIterator last,
                               OutputIterator result, const T& value )
{
  for ( ; first != last; ++first)
    if (!(*first == value)) *result++ = *first;
  return result;
}
*************************************************************************************/

/*************************************************************************************
std::remove_copy_if                 所有排序容器适用                        algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class OutputIterator, class Predicate>
  OutputIterator remove_copy_if ( InputIterator first, InputIterator last,
                                  OutputIterator result, Predicate pred );
//eg:
template <class InputIterator, class OutputIterator, class Predicate>
  OutputIterator remove_copy_if ( InputIterator first, InputIterator last,
                                  OutputIterator result, Predicate pred )
{
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}
*************************************************************************************/
template<typename T>
void Print(T& V)
{
    typename T::iterator iter=V.begin();
    while(iter != V.end())
    {
        cout<<*iter++<<" ";
    }
    cout<<endl;
}
int main()
{
    int myints[] = {10,20,30,30,20,10,10,20};      // 10 20 30 30 20 10 10 20
    cout<<"myints : 10,20,30,30,20,10,10,20"<<endl;
    // bounds of range:
    vector<int> vec(myints,myints+8);
    vector<int>::iterator pend,iv;
//    int* pbegin = myints;                          // ^
//    int* pend = myints+sizeof(myints)/sizeof(int); // ^                       ^

    pend = remove (vec.begin(), vec.end(), 20);    // 10 30 30 10 10 ?  ?  ?
    // ^             ^
    //pend:结尾位置
    cout << "range contains:";
    for(iv=vec.begin(); iv!=pend; ++iv)
        cout<<*iv<<" ";
//    for (int* p=pbegin; p!=pend; ++p)
//        cout << " " << *p;
    cout << endl;
    /***---实际数据---***/
    cout<<"---实际数据---"<<endl;
    Print(vec);
    cout<<"表示并没有删除数据而是向前覆盖!"<<endl;
    cout<<endl;
    /**--------------------------------------------------------------------------------**/

    int myints2[] = {1,2,3,4,5,6,7,8,9};            // 1 2 3 4 5 6 7 8 9
    cout<<"myints2 : 1,2,3,4,5,6,7,8,9 "<<endl;
    // bounds of range:
//    int* pbegin = myints;                          // ^
//    int* pend = myints+sizeof(myints)/sizeof(int); // ^                 ^

//    pend = remove_if (pbegin, pend, IsOdd);        // 2 4 6 8 ? ? ? ? ?
    // ^       ^
    list<int> li(myints2,myints2+9);
    list<int>::iterator plend,il;
    cout << "range contains:";
//    for (int* p=pbegin; p!=pend; ++p)
//        cout << " " << *p;
    plend = remove_if (li.begin(),li.end(),not1(bind2nd(modulus<int>(),2)));//删除奇数
    for(il=li.begin(); il!=plend; ++il)
        cout<<*il<<" ";
    cout << endl;
    /***---实际数据---***/
    cout<<"---实际数据---"<<endl;
    Print(li);
    cout<<"表示并没有删除数据而是向前覆盖!"<<endl;
    cout<<endl;
    /**--------------------------------------------------------------------------------**/

    int myints3[] = {10,20,30,30,20,10,10,20};          // 10 20 30 30 20 10 10 20
    cout<<"myints3 : 10,20,30,30,20,10,10,20"<<endl;
    vector<int> myvector (8);
    vector<int>::iterator it,pvend;

    pvend=remove_copy (myints3,myints3+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0

    cout << "myvector contains:";
    for (it=myvector.begin(); it!=pvend; ++it)
        cout << " " << *it;
    cout << endl;
    /***---实际数据---***/
    cout<<"---实际数据---"<<endl;
    Print(myvector);
    cout<<"表示并没有删除数据而是向前覆盖!"<<endl;
    cout<<endl;
    /**--------------------------------------------------------------------------------**/

    int myints4[] = {1,2,3,4,5,6,7,8,9};
    cout<<"myints4 : 1,2,3,4,5,6,7,8,9"<<endl;
    deque<int> mydeque (9);
    deque<int>::iterator itd,id;

    id=remove_copy_if (myints4,myints4+9,mydeque.begin(),bind2nd(modulus<int>(),2));

    cout << "mydeque contains:";
    for (itd=mydeque.begin(); itd!=id; ++itd)
        cout << " " << *itd;
    cout << endl;
    /***---实际数据---***/
    cout<<"---实际数据---"<<endl;
    Print(mydeque);
    cout<<"表示并没有删除数据而是向前覆盖!"<<endl;
    cout<<endl;
    /**--------------------------------------------------------------------------------**/

    return 0;
}


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值