YYW'S BLOG

知识的分享就是知识的获得
随笔 - 80, 文章 - 0, 评论 - 290, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
复制代码
#include <cstdlib>
#include 
<iostream>

using namespace std;

// Takes a pointer to the array, and the size of the array.
void print_arr1(const int *arr, size_t size)
{
    
for (size_t ix = 0; ix != size; ++ix) {
        cout 
<< arr[ix] << ' ';
    }
    cout 
<< endl;
}

// Takes 2 pointers. One to the beginning of the array, 
// and one to 1 past the last element - just like iterating through a vector.
void print_arr2(const int *beg, const int *end)
{
    
for (/* empty */; beg != end; ++beg) {
        cout 
<< *beg << ' ';
    }
    
    cout 
<< endl;
}

// Takes an array of size 10 and uses it as a const reference.
void print_arr3(const int (&arr)[10])
{
    size_t size 
= 10;
    
for (size_t ix = 0; ix != size; ++ix) {
        cout 
<< arr[ix] << ' ';
    }
    cout 
<< endl;
}

int main() 
{
    
int arr[] = {0123456789};
    
int *parr = arr;
        
    print_arr1(parr, 
10);
    print_arr2(parr, parr
+10);
    print_arr3(arr);    
    
    
return EXIT_SUCCESS;
}
复制代码
点击右上角即可分享
微信分享提示