函数模板

  std::remove                    头文件<algorithm>

template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);

  从一个范围中删除某值

  [注释:本文是 std::remove 算法的参考手册,关于 <cstdio.h> 头文件下的 remove 函数,请参见:http://www.cplusplus.com/reference/cstdio/remove/]

  对范围 [first , last ) 进行转换(transform),移除其中所有与 val 相等的元素,返回一个指向新范围的 end 的迭代器。

  (译注:transform 是 STL 的一个算法,不知道 std::remove 的底层是否经由 std::transform 实现。)

  函数不改变所包含的“范围对象”的属性(换句话说,函数不改变数组或者容器的真实 size ):元素被移动是通过下一个不等于 val 的元素,来替换当前等于 val 的元素,而且会通过返回一个指向新 end 的迭代器,来指示出新的稍“短”一些的范围(返回的迭代器所指向的元素是新的 past-the-end 元素)。

  元素间的顺序不会改变。介于返回的迭代器和 last 迭代器间的元素仍然有效,但是状态是未定义的。

  函数使用 operator== 来逐个比较元素与 val 是否相等。

  C++11下或如何实现

  元素的替换通过 move-assigning 一个新值来实现。

  可能通过如下函数模板来实现:

 template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
*result = move(*first);
++result;
}
++first;
}
return result;
}

  函数形参

  # first,last

  指向 move-assignable 序列首部和尾部的前向迭代器,用它们来支持元素和类型为 T 的值进行比较。迭代器指向的范围是 [ first, last ),这包括了 first 和 last 之间—— first 所指向的元素和 last 之前的元素。

  # val

  将被移除的元素的值。

  返回值

  一个指向最后一个没被移除的元素的迭代器。

  first 和这个迭代器的范围之间,包括了所有不等于 val 的元素。

  使用示例

 // remove algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::remove int main () {
int myints[] = {,,,,,,,}; // 10 20 30 30 20 10 10 20 // bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = std::remove (pbegin, pend, ); // 10 30 30 10 10 ? ? ?
// ^ ^
std::cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
std::cout << ' ' << *p;
std::cout << '\n'; return ;
}

  输出:

range contains:     

  复杂度

  复杂度是与 first 和 last 之间的距离(C++ STL 的函数)线性相关,即函数调用会与每一个元素进行比较,而且可能对其中的一些元素进行赋值。

  数据簇

  函数会访问、可能修改 [ first , last ) 范围之间的对象。

  异常

  元素比较(element comparisons)时可能抛出异常,元素赋值或者赋值操作作用于迭代器时,可能抛出异常。

  注意,对于无效的参数会造成未定义的行为。

  参阅

  remove_if  

  remove_copy  

  replace

  count

  find

  原文地址:http://www.cplusplus.com/reference/algorithm/remove/

【翻译】std::remove - C++ Reference的更多相关文章

  1. Error response from daemon: conflict: unable to remove repository reference 解决方案

    由于前一章演示用的镜像没什么用准备删除 docker image rm hello-world:latest Error response from daemon: conflict: unable ...

  2. C++ 标准库 std::remove

    参见:https://zh.cppreference.com/w/cpp/algorithm/remove std::remove 不会改变输入vector / string 的长度.其过程,相当于去 ...

  3. 【翻译】std::list::remove - C++ Reference

    公有成员函数 std::list::remove void remove(const value_type& val); 删除与给定值相等的元素 从容器中删除所有与 val 值相等的元素.li ...

  4. std::remove

    #include <algorithm> template< class ForwardIt, class T > ForwardIt remove( ForwardIt fi ...

  5. 令人疑惑的 std::remove 算法

    摘自<Effective STL>第32条 remove的声明: template<class ForwardIterator, class T> ForwardIterato ...

  6. docker删除镜像Error response from daemon: conflict: unable to remove repository reference

    Docker无法删除images,由于是依赖container. 1.进入root权限   sudo su 2. 列出所有运行或没有运行的镜像  docker  ps  -a 3.停止containe ...

  7. LLVM 编码规范 - 中文翻译

    LLVM 编码规范 导论 语言.库和标准 C++ 标准版本 C++ 标准库 Go 代码准则 机械的代码问题 代码格式化 注释 头文件 类概述 method information 注释格式化 使用Do ...

  8. (翻译)什么是Java的永久代(PermGen)内存泄漏

    http://www.codelast.com/?p=7248 转载请注明出处:http://www.codelast.com/ 本文是我对这篇文章的翻译:What is a PermGen leak ...

  9. Effective Modern C++翻译(7)-条款6:当auto推导出意外的类型时,使用显式的类型初始化语义

    条款6:当auto推导出意外的类型时,使用显式的类型初始化语义 条款5解释了使用auto来声明变量比使用精确的类型声明多了了很多的技术优势,但有的时候,当你想要zag的时候,auto可能会推导出了zi ...

随机推荐

  1. wxWidgets与其他工具库的比较(下)

    2009-07-25 12:37:51   GTK+       ● GTK+的网站:www.gtk.org:     ● GTK+原本是Gimp的一个工具库,是在LGPL协议下发布的Unix系统GU ...

  2. oracle行转列,列转行

    多行转字符串这个比较简单,用||或concat函数可以实现 SQL Code select concat(id,username) str from app_userselect id||userna ...

  3. UIStoryboard跳转界面

    /**1.创建Storyboard,加载Storyboard的名字,这里是自己创建的Storyboard的名字*/ UIStoryboard *storyboard = [UIStoryboard s ...

  4. 006-spring cloud gateway-GatewayAutoConfiguration核心配置-GatewayProperties初始化加载、Route初始化加载

    一.GatewayProperties 1.1.在GatewayAutoConfiguration中加载 在Spring-Cloud-Gateway初始化时,同时GatewayAutoConfigur ...

  5. js-jquery-SweetAlert【二】配置方法

    一.配置 Argument Default value 含义   Description title null (required) 模态对话框的标题.它可以在参数对象的title参数中设置,也可以在 ...

  6. vue项目中px自动转换为rem

    .安装 postcss-pxtorem : npm install postcss-pxtorem -D .修改 /build/utils.js 文件 找到 postcssLoader const p ...

  7. 在sublime3中docblockr插件配置apidoc接口文档注释模板

    写在前面: 将进行3个步骤配置 1.在sublime3中安装插件docblockr,可以参考http://www.cnblogs.com/jiangxiaobo/p/8327709.html 2.安装 ...

  8. JavaScript加强

    1.Aptana简介 Aptana是一个非常强大,开源,专注于JavaScript的Ajax开发IDE它的特性包括 1.JavaScript,JavaScript函数,HTML,CSS语言的Code  ...

  9. 2018-2019-2 网络对抗技术 20165324 Exp5:MSF基础应用

    2018-2019-2 网络对抗技术 20165324 Exp5:MSF基础应用 MSF基础知识: MSF基础框架: 主要模块模块(Module).模块是指Metasploit框架中所使用的一段软件代 ...

  10. windows下编译和安装boost库

    boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...