C++练习:返回值指针or引用
来源:互联网 发布:苹果越狱美化软件 编辑:程序博客网 时间:2024/06/03 16:57
#include <cstddef>using std::size_t;#include <iostream>using std::cout; using std::endl;// code to illustrate declarations of array-related typesint arr[10]; // arr is an array of ten intsint *p1[10]; // p1 is an array of ten pointersint (*p2)[10] = &arr; // p2 points to an array of ten intstypedef int arrT[10]; // arrT is a synonym for the type array of ten ints// two ways to declare function returning pointer to array of ten intsarrT* func(int i); // use a type aliasint (*func(int i))[10]; // direct declaration// two arraysint odd[] = {1,3,5,7,9};int even[] = {0,2,4,6,8};// function that returns a pointer to an int in one of these arraysint *elemPtr(int i){ // returns a pointer to the first element in one of these arrays return (i % 2) ? odd : even; }// returns a pointer to an array of five int elements//返回一个指针,该指针指向5个int元素构成的数组int(*arrPtr(int i))[5]{ return (i % 2) ? &odd : &even; // returns a pointer to the array }// returns a reference to an array of five int elements//返回一个由5个int元素构成的数组的引用int (&arrRef(int i))[5]{ return (i % 2) ? odd : even;}int main(){ int *p = elemPtr(6); // p points to an int int (*arrP)[5] = arrPtr(5); // arrP points to an array of five ints int (&arrR)[5] = arrRef(4); // arrR refers to an array of five ints for (size_t i = 0; i < 5; ++i) // p points to an element in an array, which we subscript cout << p[i] << endl; for (size_t i = 0; i < 5; ++i) // arrP points to an array, // we must dereference the pointer to get the array itself cout << (*arrP)[i] << endl; for (size_t i = 0; i < 5; ++i) // arrR refers to an array, which we can subscript cout << arrR[i] << endl; return 0;}
0 0
- C++练习:返回值指针or引用
- C++,返回对象的引用,语法练习
- 返回值(引用or值传递)
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
- C++:引用作为返回值
- 函数的返回值是指针--练习
- 【从C到C++学习笔记】引用/const引用/引用传递/引用作为函数返回值/引用和指针的区别
- 指针或引用做为返回值
- 指针或引用做为返回值
- C++,常量,const,constant,引用,指针,形参,实参,函数,返回值
- c/c++返回值为指针、引用时注意的地方
- C/C++之函数返回值为指针或者是引用时常见错误总结
- 返回引用类型的函数指针(c++)
- 20161224C++阶段班02_C to C++_03引用/指针/引用传参和返回值
- C++函数参数传值OR传引用_C++函数返回值返回对象OR对象引用
- Effective C++——必须返回对象时,别妄想返回其引用或指针
- C++返回引用类型 指针的引用
- 运动框架-----改变任意的style值
- win10系统安装VM、ubuntu及ubuntu自适应桌面
- React-Native 热更新尝试(Android)
- poj 3660
- Problem G: 平面上的点和线——Point类、Line类 (III)
- C++练习:返回值指针or引用
- Hibernate-Session
- 线程类Thread详解和线程的几种状态,守护线程
- 搬瓦工的一键Shadowsocks Server(创建多用户)+wordpress改中文
- ZZL训练总结
- mybatis异常invalid comparison: java.util.Date and java.lang.String
- Android Studio中”Lambda表达式“使用方法及导入详解
- HeaderFirst
- 3月题外:关于GeoServer和OpenLayers3实用开源插件或组件的总结