天天看點

STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)C++ Primer 學習中。。。

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;
}
           

繼續閱讀