STL_算法_删除(removeremove_ifremove_copyremove_copy_if)

Posted 寻找星空的孩子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL_算法_删除(removeremove_ifremove_copyremove_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;



以上是关于STL_算法_删除(removeremove_ifremove_copyremove_copy_if)的主要内容,如果未能解决你的问题,请参考以下文章

STL_算法_删除(removeremove_ifremove_copyremove_copy_if)

STL容器__简化版

STL容器__简化版

STL_算法_01_排序算法

STL_算法_Heap算法(堆排)(精)

STL_算法_06_遍历算法