zoukankan      html  css  js  c++  java
  • 十一、 C++特性之begin()与end()

    非成员begin()和end()

    也许你注意到了,我在前面的例子中已经用到了非成员begin()和end()函数。他们是新加入标准库的,除了能提高了代码一致性,还有助于更多 地使用泛型编程。它们和所有的STL容器兼容。更重要的是,他们是可重载的。所以它们可以被扩展到支持任何类型。对C类型数组的重载已经包含在标准库中 了。

    我们还用上一个例子中的代码来说明,在这个例子中我打印了一个数组然后查找它的第一个偶数元素。如果std::vector被替换成C类型数组。代码可能看起来是这样的:

    1 int arr[] = {1,2,3}; 
    2 std::for_each(&arr[0], &arr[0]+sizeof(arr)/sizeof(arr[0]), [](int n) {std::cout << n << std::endl;});
    3 
    4 auto is_odd = [](int n) {return n%2==1;}; 
    5 auto begin = &arr[0]; 
    6 auto end = &arr[0]+sizeof(arr)/sizeof(arr[0]); 
    7 auto pos = std::find_if(begin, end, is_odd); 
    8 if(pos != end) 
    9     std::cout << *pos << std::endl; 

    如果使用非成员的begin()和end()来实现,就会是以下这样的:

    1 int arr[] = {1,2,3}; 
    2 std::for_each(std::begin(arr), std::end(arr), [](int n) {std::cout << n << std::endl;}); 
    3 
    4 auto is_odd = [](int n) {return n%2==1;}; 
    5 auto pos = std::find_if(std::begin(arr), std::end(arr), is_odd); 
    6 if(pos != std::end(arr)) 
    7     std::cout << *pos << std::endl;  

    这基本上和使用std::vecto的代码是完全一样的。这就意味着我们可以写一个泛型函数处理所有支持begin()和end()的类型。

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 template <typename Iterator> 
     7 void bar(Iterator begin, Iterator end) 
     8 { 
     9     std::for_each(begin, end, [](int n) {std::cout << n << std::endl;}); 
    10 
    11     auto is_odd = [](int n) {return n%2==1;};//返回满足条件的n 
    12     auto pos = std::find_if(begin, end, is_odd);// 
    13     if(pos != end) 
    14         std::cout <<"满足条件的第一奇数是:"<< *pos << std::endl; 
    15 } 
    16 
    17 template <typename C> 
    18 void foo(C c) 
    19 { 
    20     bar(std::begin(c), std::end(c)); 
    21 } 
    22 
    23 template <typename T, size_t N> 
    24 void foo(T(&arr)[N]) 
    25 { 
    26     bar(std::begin(arr), std::end(arr)); 
    27 } 
    28 
    29 
    30 int main()
    31 {
    32     int arr[] = {10,12,13}; 
    33     foo(arr); 
    34 
    35     std::vector<int> v; 
    36     v.push_back(4); 
    37     v.push_back(5); 
    38     v.push_back(6); 
    39     foo(v); 
    40     return 0;
    41 }

    输出:

  • 相关阅读:
    状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely
    简单几何(推公式) UVA 11646 Athletics Track
    简单几何(四边形形状) UVA 11800 Determine the Shape
    简单几何(求交点) UVA 11437 Triangle Fun
    计算几何模板
    简单几何(相对运动距离最值) UVA 11796 Dog Distance
    简单几何(求划分区域) LA 3263 That Nice Euler Circuit
    覆盖的面积 HDU
    Desert King 最小比率生成树 (好题)
    约会安排 (区间合并)毒瘤题
  • 原文地址:https://www.cnblogs.com/zenseven/p/4187657.html
Copyright © 2011-2022 走看看