11、 C++特性之begin()與end()

非成員begin()和end()

也許你注意到了,我在前面的例子中已經用到了非成員begin()和end()函數。他們是新加入標準庫的,除了能提升了代碼一致性,還有助於更多 地使用泛型編程。它們和全部的STL容器兼容。更重要的是,他們是可重載的。因此它們能夠被擴展到支持任何類型。對C類型數組的重載已經包含在標準庫中 了。ios

咱們還用上一個例子中的代碼來講明,在這個例子中我打印了一個數組而後查找它的第一個偶數元素。若是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 }

輸出:spa

相關文章
相關標籤/搜索