每次使用这几个算法时都要去查CPP reference,为了能够加深印象,整理一下基本应用。

cout/cout_if:  return the number of elements satisfying the condition.

count( InputIt first, InputIt last, const T &value );  // counts the elements that are equal to value.

count_if( InputIt first, InputIt last, UnaryPredicate p ); //counts elements for which predicate p returns true.

eg.

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
 
    // determine how many integers in a std::vector match a target value.
    int target1 = 3;
    int target2 = 5;
    int num_items1 = std::count(v.begin(), v.end(), target1);
    int num_items2 = std::count(v.begin(), v.end(), target2);
    std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
    std::cout << "number: " << target2 << " count: " << num_items2 << '\n';
 
    // use a lambda expression to count elements divisible by 3.
    int num_items3 = std::count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;});
    std::cout << "number divisible by three: " << num_items3 << '\n';
}

remove_if/erase 通常一起用。(使用string的erase成员函数举例)

关于remove_if/remove 移除性算法来说,是根据元素值或某一准则,在一个区间内移除某些元素。这些算法并不能改变元素的数量,它们只是以逻辑上的思考,将原本置于后面的“不移除元素”向前移动,

覆盖那些被移除元素而已,它们都返回新区间的逻辑终点(也就是最后一个“不移除元素”的下一位置)。

ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );// Removes all elements that are equal to value.

ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );//Removes all elements for which predicate p returns true.

  • 返回的是变动后的序列的新逻辑终点,也就是最后一个未被移除的元素的下一个位置。
  • 未被移除的元素在相对次序上保持不变。
  • 调用者在调用此算法之后,应保证从此采用返回的新逻辑终点,而不再使用原始终点end;

eg.

#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
 
int main()
{
    std::string str1 = "Text with some   spaces";
    str1.erase(std::remove(str1.begin(), str1.end(), ' '),
               str1.end());
    std::cout << str1 << '\n';
 
    std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
    str2.erase(std::remove_if(str2.begin(),
                              str2.end(),
                              [](char x){return std::isspace(x);}),
               str2.end());
    std::cout << str2 << '\n';
}

此例中使用的是

iterator erase( iterator first, iterator last );//remove_if 返回的是序列的新逻辑终点,然后到序列的最后,这样就把满足条件的都删除了。

C++ count_if/erase/remove_if 用法详解的更多相关文章

  1. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  2. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  3. 7-set用法详解

    C++中set用法详解 转载 http://blog.csdn.net/yas12345678/article/details/52601454 C++ / set 更详细见:http://www.c ...

  4. STL map 常见用法详解

    <算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...

  5. STL string 常见用法详解

    string 常见用法详解 1. string 的定义 //定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可 string str; //如果要初始化,可以直接给stri ...

  6. STL set 常见用法详解

    <算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...

  7. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

  8. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  9. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

随机推荐

  1. .NET平台和开发.

  2. 深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise

    第一部分,Promise 加入 ES6 标准 原文地址 http://www.cnblogs.com/wangfupeng1988/p/6515855.html 未经作者允许不得转载! 从 jquer ...

  3. Java I/O之NIO Socket

    PS:本文简单介绍下旧I/O和NIO下的Socket通讯,仅以UDP来示例. TCP/IP协议 首先简单回顾下TCP/IP协议 Application:应用程序:Socket:套接字:Host:主机: ...

  4. php与mysql之间操作原理

    php和mysql相关扩展有:mysql.mysqli和pdo三种 mysql扩展从php5.5.0被废弃,并且从从php7.0.0开始被废除 mysql之前的使用---几个基本的函数:mysql_c ...

  5. 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 383  Solved: 211 ...

  6. javascript中parseint和number的区别

    本来是不想写这个的,网上也有,问题是讲得很不清楚,或者说我阅读能力差吧. 首先,解释一下定义的区别: parseInt将字符串(String)类型转为整数类型.Number() 函数把对象(Objec ...

  7. 用TTL线在CFE环境下拯救半砖wrt54g路由器

    缘起:路由器被刷成半砖 Linksys wrt54gs v4路由器,已刷入 tomato-dualwlan 1.23.使用数年,未出现任何故障. 在日用的wifi网络上,通过web界面刷入了错误的to ...

  8. Redis Sentinel中的机制与原理详解

    序言 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案.实际上这意味着你可以使用Sentinel模式创建一个可以不用人为干预而应对各种故障的Redis部署. 它的主要功能有以 ...

  9. php错误的处理

    错误的分类 通常分3种: 语法错误: 程序运行之前,都要先检查语法.如果语法有错误,就会立即报错,并且不会去执行程序. 运行时错误: 就是在程序语法检查通过后,,开始运行程序并在此过程中遇到的错误.常 ...

  10. 用js写倒计时,向列表添加数据-------2017-03-21

    一.单选按钮确定提交是否可用 <input id="a" type="radio" name="a"  onclick="c ...