📜  c ++中的std :: remove,std :: remove_if

📅  最后修改于: 2021-05-30 11:54:11             🧑  作者: Mango

std ::删除

它在库中定义。它从范围中删除值。将范围[first,last)转换为一个范围,并删除所有等于val的元素,然后将迭代器返回到该范围的新末端。

  • 该函数不能更改包含元素范围的对象的属性(即,它不能更改数组或容器的大小)。
  • 保留未删除元素的相对顺序,同时将返回的迭代器和last之间的元素保留为有效但未指定的状态。
  • 该函数使用运算符==将各个元素与val进行比较。

函数模板:

ForwardIterator remove  (ForwardIterator first,
ForwardIterator last, const T& val)

first,last :
  The range used is [first,last), which contains all the
elements between first and last, including the element
pointed by first but not the element pointed by last.

val :
Value to be removed.

Return value :
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.

例子:

Input : 10 20 30 30 20 10 10 20
Output : 10 30 30 10 10    // Value removed is 20.

性病:: remove_if

将范围[first,last)转换为一个范围,其中删除了pred返回true的所有元素,并将迭代器返回到该范围的新末端。
函数模板:

ForwardIterator remove_if (ForwardIterator first,
  ForwardIterator last, UnaryPredicate pred);

pred
Unary function that accepts an element in the range as
argument, and returns a value convertible to bool. The
value returned indicates whether the element is to be
removed (if true, it is removed).
The function shall not modify its argument.
This can either be a function pointer or a function object.

例子:

Input : 1 2 3 4 5 6 7 8 9 10
Output : 2 4 6 8 10    // Odd elements removed. 
CPP
// CPP program to illustrate
// std::remove and std::remove_if
// algorithm
#include 
 
// Function to check whether
// the element is odd or not.
bool IsOdd(int i) { return ((i % 2) == 1); }
 
// Driver code
int main()
{
    std ::vector vec1{
        10, 20, 30, 30, 20, 10, 10, 20
    };
    std ::vector vec2{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
    // Print original vector
    std ::cout << "Original vector : ";
    for (int i = 0; i < vec1.size(); i++)
        std ::cout << " " << vec1[i];
    std ::cout << "\n";
 
    // Iterator that store the position of last element
    std ::vector::iterator pend;
 
    // std ::remove function call
    pend = std ::remove(vec1.begin(), vec1.end(), 20);
 
    // Print the vector
    std ::cout << "After remove : ";
    for (std ::vector::iterator p = vec1.begin();
         p != pend; ++p)
        std ::cout << ' ' << *p;
    std ::cout << '\n';
 
    // Print original vector
    std ::cout << "\nOriginal vector : ";
    for (int i = 0; i < vec2.size(); i++)
        std ::cout << " " << vec2[i];
    std ::cout << "\n";
 
    // std ::remove_if function call
    pend = std ::remove_if(vec2.begin(), vec2.end(), IsOdd);
 
    // the same of the above can be done using lambda
    // function in 1 line
    pend = std ::remove_if(
        vec2.begin(), vec2.end(),
        [](int i) { return ((i % 2) == 1); });
 
    // Print the vector
    std ::cout << "After remove_if : ";
    for (std ::vector::iterator q = vec2.begin();
         q != pend; ++q)
        std ::cout << ' ' << *q;
    std ::cout << '\n';
 
    return 0;
}


输出:

Original vector :  10 20 30 30 20 10 10 20
After remove :  10 30 30 10 10

Original vector :  1 2 3 4 5 6 7 8 9 10
After remove_if :  2 4 6 8 10
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”