STL_01: remove和remove_if函数

STL_01: remove和remove_if函数

0x00 remove()函数

定义于头文件 <algorithm> 
template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );(C++20) [1]

template< class ForwardIt, class T >
constexpr ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );(C++20)[2] 

template< class ExecutionPolicy, class ForwardIt, class T >
ForwardIt remove( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value );(C++17) [3]

从范围 [first, last) 移除所有满足特定判别标准的元素,并返回范围新结尾的尾后迭代器。

  • [1] [2]移除所有等于 value 的元素。
  • [3] ,但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t>为 true 才参与重载决议。

通过以满足不移除的元素出现在范围起始的方式,迁移(以移动赋值的方式)范围中的元素进行移除。保持剩余元素的相对顺序,且不更改容器的物理大小。指向范围的新逻辑结尾和物理结尾之间元素的迭代器仍然可解引用,但元素自身拥有未指定值(因为可移动赋值 (MoveAssignable) 的后置条件)。调用 remove 典型地后随调用容器的 erase 方法,它擦除未指定值并减小容器的物理大小,以匹配其新的逻辑大小。

简言之,remove 只是通过迭代器的指针向前移动来删除,将没有被删除的元素放在链表的前面,并返回一个指向新结尾的尾后迭代器。由于remove()函数不是成员,因此不能调整链表的长度。remove()函数并不是真正的删除,要想真正删除元素则可以使用erase()或者resize()函数。用法如下:

string str1 = "Tomorrow will be fine ";
str1.erase(std::remove(str1.begin(), str1.end(), ' '),  str1.end());  // "Tomorrowwillbefine"

函数原型 :

template< class ForwardIt, class T >
ForwardIt remove(ForwardIt first, ForwardIt last, const T& value)
{
    first = std::find(first, last, value);
    if (first != last)
        for(ForwardIt i = first; ++i != last; )
            if (!(*i == value))
                *first++ = std::move(*i);
    return first;
}

0x01 remove_if()函数

定义于头文件 <algorithm>
template< class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );(C++20)[1] 

template< class ForwardIt, class UnaryPredicate >
constexpr ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );(C++20)[2]

template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );(C++17)[3] 

从范围 [first, last) 移除所有满足特定判别标准的元素,并返回范围新结尾的尾后迭代器。

  • [1] [2]移除所有 p 对于它返回 true 的元素。
  • [3] 同[1] [2],但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t>为 true 才参与重载决议

remove_if 只是通过迭代器的指针向前移动来删除,将没有被删除的元素放在链表的前面,并返回一个指向新结尾的尾后迭代器,因此remove_if函数无法真正删除元素,可以通过erase成员函数来真正删除。用法如下:

bool IsSpace(char x) { return x == ' '; }
string str2 = "Tomorrow will be fine ";
str2.erase(remove_if(str2.begin(),  str2.end(), IsSpace), str2.end()); //"Tomorrowwillbefine"

函数原型 :

template<class ForwardIt, class UnaryPredicate>
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
{
    first = std::find_if(first, last, p);
    if (first != last)
        for(ForwardIt i = first; ++i != last; )
            if (!p(*i))
                *first++ = std::move(*i);
    return first;
}

0x02 测试未指定值

/*********************************************************************************************
    *  @Copyright (c) , All rights reserved.
    *  @file:       remove_if.cpp
    *  @version:    ver 1.0
    *  @author:   zbb
    *  @brief:  
    *  @change:
    *  @email: 	binbin_erices@163.com
    *  Date             Version    Changed By      Changes 
    *  2020/3/8 12:07    1.0       zbb             create
**********************************************************************************************/
#include <algorithm>
#include <iostream>
#include <string>

void TestRemove()
{
	std::string str = "Tomorrow will be fine ";
	int length = str.length();
	std::remove(str.begin(), str.end(), 'o');
	for (auto i : str)
	{
		std::cout << i;
	}
	std::cout << std::endl;

	for (int i = 0; i < length; i++)
	{
		std::cout << str[i];
	}
	std::cout << std::endl;

	//str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
	std::cout << "str: " << str << std::endl;
}

int main()
{
	TestRemove();
	
	return 0;
}

输出结果:

Tmrrw will be fine ne
Tmrrw will be fine ne
str: Tmrrw will be fine ne

分析: remove函数实现源码

template< class ForwardIt, class T >
ForwardIt remove(ForwardIt first, ForwardIt last, const T& value)
{
    first = std::find(first, last, value);
    if (first != last)
        for(ForwardIt i = first; ++i != last; )
            if (!(*i == value))
                *first++ = std::move(*i);
    return first;
}

std::string str = "Tomorrow will be fine ";
std::remove(str.begin(), str.end(), ‘o’);

移除str串中的’o’, str串变成Tmrrw_will_be_fine_ne_, _代表空格

Remove系列函数保持剩余元素的相对顺序,且不更改容器的物理大小。指向范围的新逻辑结尾和物理结尾之间元素的迭代器仍然可解引用,但元素自身拥有未指定值(因为可移动赋值(MoveAssignable) 的后置条件)。调用 remove 典型地后随调用容器的 erase 方法,它擦除未指定值并减小容器的物理大小,以匹配其新的逻辑大小。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值