定制操作


 #include <iostream>
 #include <string>
 #include <vector>
 #include <algorithm>
 #include <numeric>
 #include <list>
 using namespace std;

 template <typename Sequence>
 inline ostream& println(Sequence const& seq)
 {
     for (auto const& elem : seq)
         cout << elem << " ";
     cout << endl;
     return cout;
 }

 inline bool is_shorter(std::string const& lhs, std::string const& rhs)
 {
     return  lhs.size() < rhs.size();
 }

 void elimdups(vector<string> &vs)
 {
     sort(vs.begin(), vs.end());
     auto new_end = unique(vs.begin(), vs.end());
     vs.erase(new_end, vs.end());
 }

 int main()
 {
     vector<", "Hi", "alan", "wang" };
     elimdups(v);
     stable_sort(v.begin(), v.end(), is_shorter);
     cout << "ex10.11 :\n";
     println(v);
     system("pause");
     ;
 }

输出结果:

Exercise 10.14:


 int main()
 {
     auto sum = [](int a, int b) {return a + b; };
     cout << sum(, ) << endl;
     ;
 }

Exercise 10.16

 #include <iostream>
 #include <string>
 #include <vector>
 #include <algorithm>
 using namespace std;

 void elimdups(vector<string> &vs)
 {
     sort(vs.begin(), vs.end());
     for (auto c : vs)
         cout << c << " ";
     cout << endl;

     auto new_end = unique(vs.begin(), vs.end());
     vs.erase(new_end, vs.end());
     for (auto c : vs)
         cout << c << " ";
     cout << endl;

 }

 void biggies(vector<string> &vs, size_t sz)
 {
     elimdups(vs);
     stable_sort(vs.begin(), vs.end(), [](string const& lhs, string const& rhs) {
         return lhs.size() < rhs.size(); });

     auto wc = find_if(vs.begin(), vs.end(), [sz](string const& s) {
         return s.size() >= sz; });

     for_each(wc, vs.end(), [](const string &s) {
         cout << s << " ";
     });
 }

 int main()
 {
     vector<string> v{
         ","hi~", "alan", "alan", "cp"
     };
     cout << "ex10.16: " << endl;
     biggies(v, );
     cout << endl;
     system("pause");
     ;
 }

输出结果:

Exercise 10.18、10.19

 #include <iostream>
 #include <string>
 #include <vector>
 #include <algorithm>
 using namespace std;

 // from ex 10.9
 void elimdups(vector<string> &vs)
 {
     sort(vs.begin(), vs.end());
     auto new_end = unique(vs.begin(), vs.end());
     vs.erase(new_end, vs.end());
     for (auto it = vs.cbegin(); it != vs.end(); ++it)
         cout << *it << " ";
     cout << endl;
 }

 // ex10.18
 void biggies_partition(vector<string> &vs, size_t sz)
 {
     elimdups(vs);
     auto pivot = partition(vs.begin(), vs.end(), [sz](const string &s) {
         return s.size() >= sz; }
     );

     for (auto it = vs.cbegin(); it != pivot; ++it)
         cout << *it << " ";

 }

 // ex10.19
 void biggies_stable_partition(vector<string> &vs, std::size_t sz)
 {
     elimdups(vs);

     auto pivot = stable_partition(vs.begin(), vs.end(), [sz](const string& s) {
         return s.size() >= sz; });

     for (auto it = vs.cbegin(); it != pivot; ++it)
         cout << *it << " ";
 }

 int main()
 {
     // ex10.18
     vector<string> v{
         "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"
     };

     cout << "ex10.18: ";
     vector<string> v1(v);
     biggies_partition(v1, );
     cout << endl;

     // ex10.19
     cout << "ex10.19: ";
     vector<std::string> v2(v);
     biggies_stable_partition(v2, );
     cout << endl;
     system("pause");
     ;
 }

输出结果:

参数绑定

Exercise 10.22:

 #include <iostream>
 #include <vector>
 #include <string>
 #include <algorithm>
 #include <functional>

 using namespace std;
 using namespace std::placeholders;

 bool isLesserThanOrEqualTo6(const string &s, string::size_type sz)
 {
     return s.size() <= sz;
 }

 int main()
 {
     vector<string> authors{ "Mooophy", "pezy", "Queequeg90", "shbling", "evan617" };
     cout << count_if(authors.cbegin(), authors.cend(), bind(isLesserThanOrEqualTo6, _1, )) << endl;
     system("pause");
     ;
 }

Exercise 10.24:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
using namespace std::placeholders;

auto check_size(string const& str, size_t sz)
{
    return str.size() < sz;
}

int main()
{
    vector<, , , , , , ,  };
    ");
    auto result = find_if(vec.begin(), vec.end(), bind(check_size, str, _1));
    if (result != vec.end())
        cout << *result << endl;
    else
        cout << "Not found" << endl;
    system("pause");
    ;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
using namespace std::placeholders;

auto check_size(string const& str, size_t sz)
{
    return str.size() < sz;
}

int main()
{
    vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 };
    string str("123456");
    auto result = find_if(vec.begin(), vec.end(), bind(check_size, str, _1));
    if (result != vec.end())
        cout << *result << endl;
    else
        cout << "Not found" << endl;
    system("pause");
    return 0;
}

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
using namespace std::placeholders;

auto check_size(string const& str, size_t sz)
{
    return str.size() < sz;
}

int main()
{
    vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 };
    string str("123456");
    auto result = find_if(vec.begin(), vec.end(), bind(check_size, str, _1));
    if (result != vec.end())
        cout << *result << endl;
    else
        cout << "Not found" << endl;
    system("pause");
    return 0;
}

  

 

【C++ Primer | 07】泛型算法的更多相关文章

  1. c++ primer 11 泛型算法

    使用泛型算法必须包含头文件#inlucde <algorithm> 标准库还定义一组泛化的算术算法,其命名习惯与泛型算法相同,包含头文件#include <numeric> f ...

  2. C++ Primer : 第十章 : 泛型算法 之 只读、写和排序算法

    大多数算法都定义在<algorithm>头文件里,而标准库还在头文件<numeric>里定义了一组数值泛型算法,比如accumulate. ●  find算法,算法接受一对迭代 ...

  3. C++ Primer 5th 第10章 泛型算法

    练习10.1:头文件algorithm中定义了一个名为count的函数,它类似find,接受一对迭代器和一个值作为参数.count返回给定值在序列中出现的次数.编写程序,读取int序列存入vector ...

  4. C++ Primer 读书笔记:第11章 泛型算法

    第11章 泛型算法 1.概述 泛型算法依赖于迭代器,而不是依赖容器,需要指定作用的区间,即[开始,结束),表示的区间,如上所示 此外还需要元素是可比的,如果元素本身是不可比的,那么可以自己定义比较函数 ...

  5. 【足迹C++primer】30、概要(泛型算法)

    概要(泛型算法) 大多数算法的头文件中定义algorithm在. 标准库也是第一个文件numeric它定义了一套通用算法. #include<iostream> #include<n ...

  6. 【C++ Primer | 07】常用算法

    第一部分 常用泛型算法: find(beg, end, val); equal(beg1, end1, beg2); fill(beg, end, val); fill_n(beg, cnt, val ...

  7. [C++ Primer] : 第10章: 泛型算法

    概述 泛型算法: 称它们为"算法", 是因为它们实现了一些经典算法的公共接口, 如搜索和排序; 称它们是"泛型的", 是因为它们可以用于不同类型的元素和多种容器 ...

  8. C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  9. C++ Primer笔记6_STL之泛型算法

    1.泛型算法: 大多数算法定义在头文件algorithm中.标准库还在头文件numeric中定义了一组数值泛型算法 仅仅读算法: 举例: find函数用于找出容器中一个特定的值,有三个參数 int v ...

  10. C++ 泛型算法

    <C++ Primer 4th>读书笔记 标准容器(the standard container)定义了很少的操作.标准库并没有为每种容器类型都定义实现这些操作的成员函数,而是定义了一组泛 ...

随机推荐

  1. Linux 查看系统硬件信息[转]

    原文:http://www.cnblogs.com/ggjucheng/archive/2013/01/14/2859613.html linux查看系统的硬件信息,并不像windows那么直观,这里 ...

  2. 2017-2018-2 20165325 实验一《Java开发环境的熟悉》实验报告

    一.Java开发环境的熟悉-1 1.实验要求: 0 参考实验要求: 1 建立"自己学号exp1"的目录 : 2 在"自己学号exp1"目录下建立src,bin等 ...

  3. requests库入门10-超时,错误与异常

    在实际发布到生产上的接口测试代码,都会加上超时的设置,当服务器超过一定时间没有响应,会报出超时异常.因为requests会自动等待响应.如果不加上超时的设置,可能脚本会一直卡在那里.. 超时设置在请求 ...

  4. nodejs+express+mongodb简单的例子

    简单的介绍下node+express+mongodb这三个东西.node:是运行在服务器端的程序语言,表面上看过去就是javascript一样的东西,但是呢,确实就是服务器语言,个人觉得在一定层次上比 ...

  5. <第一站>人生的第一个博客

    在畅畅的疯狂暗示下(“最好”建个博客),我决定在博客园开通我的博客,在此记入我从3月23起的所学所想.在他的提醒之前,我曾经断断续续的在日记本上,手机备忘录里记录过我的学习情况和心路历程,总的来说,自 ...

  6. c 中打印格式%g

    C语言中打印float或double类型最常用的是%f格式,最近看书时看到有使用%g格式打印. %f  表示按浮点数的格式打印. 小数点后固定6位 %e 表示以指数形式的浮点数格式输出. %g 表示自 ...

  7. 【原创】大数据基础之Logstash(3)应用之http(in和out)

    一个logstash很容易通过http打断成两个logstash实现跨服务器或者跨平台间数据同步,比如原来的流程是 logstash: nginx log -> kafka 打断成两个是 log ...

  8. Python-Pool类

    目录: multiprocessing模块 Pool类 apply apply_async map close terminate join 进程实例 multiprocessing模块 如果你打算编 ...

  9. vue 轮播图插件 vue-awesome-swiper

    1.npm安装 npm install vue-awesome-swiper --save 2.vue 引入 //在main.js 中全局引入 import VueAwesomeSwiper from ...

  10. liunx 安装jdk1.8

    采用解压压缩文件形式安装 第一步:将jdk压缩文件上传至home目录下面 第二步:目录切换至/usr目录下   cd /usr 创建java目录   mkdir   java 第三步:将jdk 压缩文 ...