[精品][精品]C++大学教程

上传人:jiups****uk12 文档编号:45249818 上传时间:2018-06-15 格式:PPT 页数:82 大小:363.50KB
返回 下载 相关 举报
[精品][精品]C++大学教程_第1页
第1页 / 共82页
[精品][精品]C++大学教程_第2页
第2页 / 共82页
[精品][精品]C++大学教程_第3页
第3页 / 共82页
[精品][精品]C++大学教程_第4页
第4页 / 共82页
[精品][精品]C++大学教程_第5页
第5页 / 共82页
点击查看更多>>
资源描述

《[精品][精品]C++大学教程》由会员分享,可在线阅读,更多相关《[精品][精品]C++大学教程(82页珍藏版)》请在金锄头文库上搜索。

1、C+大学教程 2007.9第七章 数组与C+标准库类模板数组 数组声明,初始化,引用 字符数组 数组传递给函数 数组存储,排序,查找多维数组 C+标准库- vector类模板 总结C+大学教程 2007.9数组的用途v程序设计要处理的数据往往是许多数据而非简单的 几个数据,对单个数据分别声明变量显露其局限性例如,如果要处理3个学生考试成绩,我们可以声 明: int grade0,grade 1,grade2;但如果有很多成绩(例100个),该怎么办?v对一组由相同类型数据组成的数据集合使用数组C+大学教程 2007.9数组的特征v有序性:数组中的所有元素都有自身的 序号0,1,2v相同类型:全

2、部是整数、浮点数、字符 型,也可以数组的数组(一维数组、二 维数组、多维数组), 后面所说数组是 特指一维数组。C+大学教程 2007.9第七章 数组与C+标准库类模板数组 数组声明,初始化,引用 字符数组 数组传递给函数 数组存储,排序,查找多维数组 C+标准库- vector类模板 总结C+大学教程 2007.9一维数组的声明int a 5 ; 数据类型数组名数组元素的个数:必须 是正值常量表达式,编 译时就知值见到数组声明后,编译器保留恰当容量内存, 可以存储5个整型变 量,每个变量有自己的表示方式: a0, a1, a2, a3, a4C+大学教程 2007.9内存空间占用情况int

3、a5; 1、设一个int需要占用4个字节的空间2、存放a0的地址为1000,此时1000被成为数组a的基地址 a0a1a2a3a4值78111623地址10001004100810121016C+大学教程 2007.9 初始化v声明数组时可以对数组初始化float x5 = -1.1, 0.2, 33.0, 4.4, 5.05 ;v初始化表的长度短于要被初始化的数组元素数目, 那么剩余元素被初始化为0。取巧 int a10=0.v外部或静态数组没有被初始化,系统自动把所有元 素初始化为0。v没有被初始化的自动或常量数组含有的值是无用的 。v数组的存储类型不能是寄存器类型。初始化表C+大学教程

4、2007.9初始化vint a = 2,3,4,5vint a4 = 2,3,4,5当数组大小被省略,编译器计算初始化表中 元素个数来确定数组元素的个数C+大学教程 2007.9 使用一维数组例:对数组赋值 (也达到初始化目的) #include const int N= 5 int main(void) int aN;int i, sum = 0;for (i = 0; i using std:cout; using std:endl;#include using std:setw;int main() int n 10 ; / n is an array of 10 integers/ i

5、nitialize elements of array n to 0for ( int i = 0; i using std:cout; using std:endl; #include using std:setw; #include using std:rand; using std:srand; #include using std:time;C+大学教程 2007.9int main() const int arraySize = 7; / ignore element zeroint frequency arraySize = 0 ;srand( time( 0 ) ); / see

6、d random number generator/ roll die 6,000,000 times; use die value as frequency indexfor ( int roll = 1; roll string2;输入时以空格为终止, 并在数组后自动加空字 符0; 注意输入时长度不要超过数组声明时 的大小,这里为19. 否则系统并不会检查. 输出:cout using std:cout; using std:cin; using std:endl;C+大学教程 2007.9例子: int main() char string1 20 ; / reserves 20 cha

7、racterschar string2 = “string literal“; / reserves 15 characters/ read string from user into array string1cout string1; / reads “hello“ space terminates input/ output stringscout string1; / reads “there“cout / program uses C+ Standard Library string class using std:string;/ GradeBook class definitio

8、n class GradeBook public:/ constant - number of students who took the testconst static int students = 10; / note public dataC+大学教程 2007.9/ constructor initializes course name and array of gradesGradeBook( string, const int );/ function to set the course namevoid setCourseName( string );/function to

9、retrieve the course namestring getCourseName(); / display a welcome messagevoid displayMessage(); / perform various operations on the grade datavoid processGrades(); C+大学教程 2007.9int getMinimum(); / find the minimum grade for the testint getMaximum(); / find the maximum grade for the testdouble getA

10、verage(); / determine the average grade for the testvoid outputBarChart(); / output bar chart of grade distributionvoid outputGrades(); / output the contents of the grades arrayprivate:string courseName; / course name for this grade bookint grades students ; / array of student grades ; / end class G

11、radeBook 参见 Fig.7.16-18, 要求学生课后运行并分析该程序中数组用法C+大学教程 2007.9 注释:vstatic 成员表示是一个类变量,即只有在 类这一级上有空间分配, 该类的每个对 象并没有该成员副本.v static const成员表示是一个类常量, 在 整个类包括各个对象都不能修改其值有关static 数据成员详细介绍见第十章C+大学教程 2007.9第七章 数组与C+标准库类模板数组 数组声明,初始化,引用数组传递给函数 数组存储,排序,查找多维数组 C+标准库- vector类模板 总结C+大学教程 2007.9查找v在数组存放的大量数据中, 常遇到需要 判断

12、这组数据中是否与某个关键字相匹 配, 即试图在数组中发现一个特定的元 素. 此过程称为查找v查找的方法有很多, 这里基于数组是否 有序介绍两种查找方法:无序时采用线性查找法有序时采用二分法查找C+大学教程 2007.9查找v线性查找法将待查找的关键字和数组中的每个元素进行 比较, 找到时返回其下标,否则宣布未找到查找次数: 平均必须比较一半元素. 时间复杂程度O(n)v线性查找法适用于小型数组或未排序数组C+大学教程 2007.9查找线性查找法: int linearSearch( const int array, int key,int sizeOfArray ) for ( int j =

13、 0; j 0 ) moveItem-; data moveItem = insert;比 较 移动C+大学教程 2007.9第七章 数组与C+标准库类模板数组 数组声明,初始化,引用数组传递给函数 数组存储,排序,查找多维数组 C+标准库- vector类模板 总结C+大学教程 2007.9二维数组声明:数据类型 数组名MN ; M,N为常 量表达式int a34; 编译器分配3*4*2个int型的连续存储空间第0列第1列第2列第3列 第0行a00a01a02a03 第1行a10a11a12a13 第2行a20a21a22a23C+大学教程 2007.9二维数组使用方法const int M

14、=3; const int N=4 int main( ) int aMN, i,j, sum = 0;for(i=0;i / program uses C+ Standard Library string class using std:string;/ GradeBook class definition class GradeBook public:/ constant - number of students who took the testconst static int students = 10; / note public dataconst static int tests

15、 = 3;C+大学教程 2007.9/ constructor initializes course name and array of gradesGradeBook( string, const int tests);/ function to set the course namevoid setCourseName( string );/function to retrieve the course namestring getCourseName(); / display a welcome messagevoid displayMessage(); / perform various operations on the grade datavoid processGrades(); C+大学教程 2007.9int getMinimum(); / find the minimum grade for the testint getMaximum(); /

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号