zoukankan      html  css  js  c++  java
  • [C++ Primer] Passing an array to a function by reference/pointers sample

    #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;
    }
  • 相关阅读:
    set, bag, list, map的语义
    ExtJs 自定义Vtype验证
    详解.NET中的动态编译技术
    IL汇编语言介绍(译)
    C# 文件操作相关
    邮件系统
    关于Nhibernate中的多数据库支持
    .NET中 用C#操纵IIS
    ExtJS日期格式
    完全详解使用Resource实现多语言的支持
  • 原文地址:https://www.cnblogs.com/yyw84/p/2091896.html
Copyright © 2011-2022 走看看