zoukankan      html  css  js  c++  java
  • 第5章 C++STL泛化技术分析

    /*
    
    第5章 C++STL泛化技术分析
       5.1 算法和迭代器
       5.2 内存分配器和容器
       5.3 概念
       5.4 本章小结
    
    
    第5章 C++STL泛化技术分析
       5.1 算法和迭代器
       5.1.1 算法
       5.1.2 迭代器
       5.1.3 函数对象
       5.1.4 适配器
       5.2 内存分配器和容器
       5.2.1 内存分配器
       5.2.2 容器
       5.3 概念
       5.3.1 基础性概念
       5.3.2 容器概念
       5.3.3 迭代器概念
       5.3.4 函数对象概念
       5.4 本章小结
    
    */
    
    
    //第5章 C++STL泛化技术分析
    //   5.1 算法和迭代器 ---------------------------------------------------------------------------------------------
    
    //61
    #include <algorithm>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      double a[8] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }; //8个元素的数组
      double val = 3.0;
      double *result = find(a, a + 8, val); //等价于find(&a[0],&a[7]+1,val)
      if(result == a + 8)
        cout << "数组没有一个元素的值等于" << val << endl;
      else
        cout << "数组有一个元素值等于" << val << endl;
      return 0;
    }
    
    
    
    //66
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    struct print
    {
      void operator()(int x)
      {
        cout << x << ' ';
      }
    };
    
    int main()
    {
      int a[] = {68, 1, 17, 6, 3, 31, 6, 5, 30};
      const int length = sizeof(a) / sizeof(int);
      //对每个数组元素进行打印
      for_each(a, a + length, print());
      cout << endl;
      
      return 0;
    }
    
    
    // 70 ,与书中代码有所不同,改过了。
    #include <iterator>
    #include <iostream>
    #include <vector>
    int main(void)
    {
      using namespace std;
      vector < int > v;
      v.push_back(3);
      v.push_back(6);
      v.push_back(9);
      vector<int>::reverse_iterator rfirst(v.end());
      vector<int>::reverse_iterator rend(v.begin());
      while(rfirst != rend)
      {
        cout <<  *rfirst << ' ';
        ++rfirst;
      }
      return 0;
    }
    
    
    //73
    /*
    前面说书不代码不对,可能说错了。bind可以绑定第一个参数,也可以绑定第二个参数
    再来解读bind。
    bool greater(T&x, T&y){return x>y;}
    bind1st,是将常数值,绑定给greater的第一个参数,那么第二个参数就是*it,即引用的容器中的值。
    以下程序,将常数7,绑定给greater的第一个参数x,那么return的是:
    7>(*it)
    那么容器中的小于7的数,将被首先找到。
    
    将常数绑定到第一个参数,程序不容易阅读。所以bind2nd要比bind1st常用得多。
    */
    
    #include <vector>
    #include <iostream>
    #include <algorithm>   //find_if算法
    #include <functional>
    int main(void)
    {
      using namespace std;
      vector < int > v;
      v.push_back(20);
      v.push_back(13);
      v.push_back(6);
      v.push_back(3);
      v.push_back(29);
      vector < int > ::iterator less7_iter;
      less7_iter = find_if(v.begin(), v.end(), bind1st(greater < int > (), 7));
      cout <<  *less7_iter << endl; //将打印数字6
      return 0;
    }
    
    
    // my test
    /*
    直接用bind2nd,找小于7的数,要直观得多。
    绑定,是将常数值,绑定给greater等二元函数对象的第一,或第二个参数。
    如非特殊,绑定给第二个参数吧。
    */
    
    #include <vector>
    #include <iostream>
    #include <algorithm>   //find_if算法
    #include <functional>
    int main(void)
    {
      using namespace std;
      vector < int > v;
      v.push_back(20);
      v.push_back(13);
      v.push_back(6);
      v.push_back(3);
      v.push_back(29);
      vector < int > ::iterator less7_iter;
      less7_iter = find_if(v.begin(), v.end(), bind2nd(less<int>(),7));
      cout <<  *less7_iter << endl; //将打印数字6
      return 0;
    }
    
    
    //74。当然,不用函数对象,直接用函数,也是可以的。
    #include <vector>
    #include <iostream>
    #include <algorithm>   //find_if算法
    bool less7(int x)
    {
      return x < 7;
    }
    int main(void)
    {
      using namespace std;
      vector < int > v;
      v.push_back(20);
      v.push_back(13);
      v.push_back(6);
      v.push_back(3);
      v.push_back(29);
      vector < int > ::iterator less7_iter = find_if(v.begin(), v.end(), less7);
      cout <<  *less7_iter << endl; //将打印数字6
      return 0;
    }
    
    //   5.2 内存分配器和容器 ---------------------------------------------------------------------------------------------
    
    //   5.3 概念 ---------------------------------------------------------------------------------------------
    
    //   5.4 本章小结 ---------------------------------------------------------------------------------------------
    
    // 这章有点解读源码的味道,不必细读。

    TOP

  • 相关阅读:
    MS SQL2000 && 2005转出数据字典
    基于角色的访问控制'的权限管理的数据库的设计实现
    ANSI SQL / T SQL / PLSQL
    MS SQL系統資料表內容
    关闭不需要服务 为Windows系统提速
    Form.Enctype屬性
    流程圖
    ASPSmartUpload祥解
    数据排序常见算法(JS版)
    如何实现定时开机
  • 原文地址:https://www.cnblogs.com/xin-le/p/4110291.html
Copyright © 2011-2022 走看看