C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ remove_if()

C++算法remove_if()

C++算法 remove_if()函数用于从给定范围内消除所有满足谓词的元素。 ,last)而不会干扰其余元素的顺序。
此功能无法更改容器的大小。 它将迭代器返回到范围的新末端。 删除是稳定的,表示未删除的元素的相对顺序保持不变。

语法

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

参数

first: 前向迭代器,指向要删除元素范围内第一个元素的位置。
last: 前向迭代器,指向要从中删除元素的范围中的最后一个元素经过的位置。
pred : 一元谓词函数,它接受元素作为要替换的元素值必须满足的参数。

返回值

正向迭代器指向修改后的范围或第一个元素的新结束位置(最后一个)(如果first和last相等)。

复杂度

复杂度在[first, last): 对每个元素应用pred,并可能对其中的一些元素进行赋值。

数据竞争

[first,last)范围内的对象是

异常安全性

此功能如果pred,元素分配或迭代器上的任何操作引发异常,则n引发异常。
注意: 无效的参数会导致未定义的行为。

示例1

让我们看一个简单的示例来演示remove_if()的用法:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool IsOdd(int i) 
{ 
    return ((i % 2) == 1); 
} 
  
int main ()  
{ 
    vector <int> vec2 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
    
    //store the position of last element 
    vector <int>::iterator pend; 
        
    // Print original vector 
    cout << "\nOriginal vector : "; 
    for(int i=0; i < vec2.size(); i++) 
        cout << " " << vec2[i]; 
    cout << "\n"; 
  
    // remove_if function call 
    pend = remove_if (vec2.begin(), vec2.end() , IsOdd); 
      
    // Print the vector 
    cout << "After remove_if : "; 
    for ( vector<int> :: iterator q=vec2.begin(); q != pend; ++q) 
        cout << ' ' << *q; 
    cout << '\n'; 
  
return 0; 
}
输出:
Original vector :  1 2 3 4 5 6 7 8 9 10
After remove_if : 2 4 6 8 10

示例2

让我们看另一个简单的示例:
#include <vector>  
#include <algorithm>  
#include <iostream>  
using namespace std;  
  
bool greater6 ( int value ) {  
   return value >6;  
}  
  
int main( ) {  
   vector <int> v1, v2;  
   vector <int>::iterator Iter1, Iter2, new_end;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
      v1.push_back( i );  
  
   int ii;  
   for ( ii = 0 ; ii <= 3 ; ii++ )  
      v1.push_back( 7 );  
  
   random_shuffle ( v1.begin( ), v1.end( ) );  
   cout << "Vector v1 is ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Remove elements satisfying predicate greater6  
   new_end = remove_if (v1.begin( ), v1.end( ), greater6 );  
  
   cout << "Vector v1 with elements satisfying greater6 removed is\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // To change the sequence size, use erase  
   v1.erase (new_end, v1.end( ) );  
  
   cout << "Vector v1 resized elements satisfying greater6 removed is\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}
输出:
 Vector v1 is ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
Vector v1 with elements satisfying greater6 removed is
 ( 4 0 5 1 6 3 2 1 6 9 3 7 8 2 ).
Vector v1 resized elements satisfying greater6 removed is
 ( 4 0 5 1 6 3 2 ).

示例3

让我们看另一个简单的示例:
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
 
typedef std::vector<std::string>::iterator iterator;
 
struct startsWithA : public std::unary_function<std::string, bool> {
    bool operator() (std::string s)
    {
        if(s[0] == 'A')
        {
            return true;
        }
        else
            return false;
    }
};
 
void print(iterator b, iterator e)
{
    iterator i;
    for(i = b; i != e; i++)
    {
        std::cout << *i << "    ";
    }
    std::cout << std::endl;
}
 
int main()
{
    startsWithA s;
    std::vector<std::string> v;
    v.push_back("China");
    v.push_back("India");
    v.push_back("Korea");
    v.push_back("America");
    v.push_back("Australia");
    v.push_back("Pakistan");
 
    std::cout << "Vector : ";
    print(v.begin(), v.end());
    iterator i = remove_if(v.begin(), v.end(), s);
    std::cout << "Vector : ";
    print(v.begin(), i);
    
    return 0;
}
输出:
Vector : China    India    Korea    America    Australia    Pakistan    
Vector : China    India    Korea    Pakistan    

示例4

让我们看另一个简单的示例,通过将所有非空格字符向左移动然后擦除多余的空格来删除字符串中的所有空格。
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
 
using namespace std; 
 
int main()
{
    string str1 = "Text with some   spaces";
    str1.erase(remove(str1.begin(), str1.end(), ' '),
               str1.end());
    cout << str1 << '\n';
 
    string str2 = "Text\n with\tsome \t  whitespaces\n\n";
    str2.erase(remove_if(str2.begin(), 
                              str2.end(),
                              [](unsigned char x){return std::isspace(x);}),
               str2.end());
    cout << str2 << '\n';
    
    return 0;
}
输出:
Textwithsomespaces
Textwithsomewhitespaces

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4