try c++11 foreach(1)

来源:互联网 发布:出发台湾软件 编辑:程序博客网 时间:2024/06/15 07:21
#include<iostream>using namespace std;/*int f(int a[10]){//实际上插入的参数是指针 <=>int f(int *a)for(int b : a){//error  'begin' was not declared in this scope; 'end' was not declared in this scopeb = 10;}//return a;}*/int main(){int a[10];for(int &b : a){//必须是引用,否则本就是个副本,没有改变a中元素的值b = 10;}for(int i = 0;i < 10 ;i++){cout << a[i] << endl;}}

C:\Users\jackz\Desktop\codes\cpp>g++ -std=c++11 returnArrayFunction.cppC:\Users\jackz\Desktop\codes\cpp>a10101010101010101010

0 0
原创粉丝点击