C++ 向函数传数组的方法

2018-04-14  创建人:Ruo_Xiao
邮箱:xclsoftware@163.com

1、使用标记指定数组末尾
栗子:

void print(const char *pCp)
{
    if(pCp)
    {
        while (*pCp)
        {
            cout<<*pCp++<<endl;
        }
    }
}

字符串存储在字符数组中,并且最后一个字符为空字符。

2、使用标准库规范
传递指向数组首元素和尾后元素的指针。

void print(const int *pBegin,const int *pEnd)
{
    while(pBegin!=pEnd)
    {
        cout<<*pBegin++<<endl;
    }
}

int main() 
{
    int iData[2] = {1,2};
    print(begin(iData),end(iData)); 

    cin.get();

    return 0;
}

3、传递数组首地址和数组元素个数。

(SAW:Game Over!)

猜你喜欢

转载自blog.csdn.net/itworld123/article/details/79937087