171020 C++学习笔记-6.1

来源:互联网 发布:迈尔斯特纳数据 编辑:程序博客网 时间:2024/06/03 09:37

注:代码部分来源于http://www.xuetangx.com/,仅为个人学习笔记之用,不得用于任何商业用途。

例 6-1 数组的申明与使用

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。// 6_1.cpp#include "stdafx.h"#include<iostream>  using namespace std;int main() {    int a[10], b[10];    for (int i = 0; i < 10; i++) {        a[i] = i * 2 - 1;        b[10 - i - 1] = a[i];    }    for (int i = 0; i < 10; i++) {        cout << "a[" <<i << "]"<< a[i] << " ";        cout << "b[" <<i << "]" << b[i]<<endl;      }    return 0;}

例 一维数组应用举例-1 求Fabonacci数组的前20项,将结果存放于数组中

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。// 6_1.cpp#include "stdafx.h"#include<iostream>  using namespace std;int main() {    int f[20] = { 1,1 };    //初始化第0,1个数    for (int i = 2; i < 20; i++) {  //求第2~19个数        f[i] = f[i - 2] + f[i - 1];     }    for (int i = 0; i < 20; i++) {        if (i % 5 == 0)cout << endl;    //输出,每行5个数        cout.width(12);     //设置输出宽度为12,保证对齐好看        cout << f[i];    }    return 0;}

例 一维数组应用举例-2

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。// 6_1.cpp//循环从键盘读入若干组选择题答案,计算并输出每组答案的正确率,直到输入Ctrl+Z为止。//每组连续输入5个答案,每个答案可以是"a"、"b"、"c"、"d"#include "stdafx.h"#include<iostream>  using namespace std;int main() {    const char key[] = { 'a','c','b','a','d' };//存放正确答案    const int NUM_QUES = 5;//存放题目个数    char c;    int ques = 0, numCorrect = 0;    cout << "Enter the " << NUM_QUES << " question tests:" << endl;    while (cin.get(c)) { // ctrl+z即结束        if (c != '\n') { // 回车换行,答题结束,开始计算正确率            if (c == key[ques]) {                numCorrect++; cout << " "; //答对输出空格            }            else                cout << '*';    //答错输出‘*’            ques++;        }        else        {            cout << "Score " << static_cast<float>(numCorrect) / NUM_QUES * 100 << "%";            ques = 0; numCorrect = 0; cout << endl; //变量归零,为下一答题者准备        }    }    return 0;}

例 一维数组应用举例-3:数组明即为地址,加*表示第一个元素

#include "stdafx.h"#include<iostream>  using namespace std;int main() {    int arr[5] = { 1,2,3,4,5 };    cout << *arr + 1<< endl;    cout << *(arr + 1)<<endl;    cout<<arr[1]<<endl;    cout << *arr<<endl;     cout << arr;    return 0;}

这里写图片描述

数组元素作实参,与单个变量一样。
数组名作参数,形、实参数都应是数组名(实质上是地址,关于地址详见6.2),类型要一样,传送的是数组首地址。对形参数组的改变会直接影响到实参数组。

例 6-2 使用数组名作为函数参数

传入的是table的地址,将table地址传递给a,在函数体内对形参a的任意修改都将改变原table中的实参数据。

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。// 6_2.cpp#include "stdafx.h"#include<iostream>  using namespace std;void rowSum(int a[][4], int nRow) {for (int i = 0; i < nRow; i++) {    for (int j = 1; j < 4; j++)        a[i][0] += a[i][j];    }}int main() {   //主函数               //定义并初始化数组int table[3][4] = { { 1, 2, 3, 4 },{ 2, 3, 4, 5 },{ 3, 4, 5, 6 } };//输出数组元素for (int i = 0; i < 3; i++) {    for (int j = 0; j < 4; j++)        cout << table[i][j] << "   ";    cout << endl;}rowSum(table, 3);     //调用子函数,计算各行和                      //输出计算结果for (int i = 0; i < 3; i++)cout << "Sum of row " << i << " is " << table[i][0] << endl;return 0;}

这里写图片描述

例6-3 对象数组应用举例

//Point.h#ifndef _POINT_H#define _POINT_Hclass Point { //类的定义public: //外部接口 Point();Point(int x, int y);~Point();void move(int newX,int newY);int getX() const { return x; }int getY() const { return y; }static void showCount(); //静态函数成员private: //私有数据成员int x, y;};#endif //_POINT_H
//Point.cpp#include <iostream>#include "Point.h"using namespace std;Point::Point() : x(0), y(0) { //初始化默认构造函数cout << "Default Constructor called." << endl;}Point::Point(int x, int y) : x(x), y(y) {cout << "Constructor called." << endl;}Point::~Point() { //调用默认析构函数cout << "Destructor called." << endl; }void Point::move(int newX,int newY) {cout << "Moving the point to (" << newX << ", " << newY << ")" << endl;x = newX;y = newY;}
//6-3.cpp#include "Point.h"#include <iostream>using namespace std;int main() {cout << "Entering main..." << endl;Point a[2];for(int i = 0; i < 2; i++) a[i].move(i + 10, i + 20);cout << "Exiting main..." << endl;return 0;}

这里写图片描述

使用Point a[3]={Point(1,2),Point(3,4),Point()};时,系统调用了1次Point类的默认构造函数?

例 基于范围的for循环举例

#include "stdafx.h"#include<iostream>  using namespace std;int main(){    int arr[3] = { 1,2,3 };    int *p; //定义指针p,定义整数型变量*p    for (p = arr; p < arr + sizeof(arr) / sizeof(int); ++p)    {        *p += 2; // 等价于 p = p+2        cout << *p << endl;    }return 0;   }

这里写图片描述

#include "stdafx.h"#include<iostream>  using namespace std;int main(){    int arr[3] = { 1,2,3 };    for (int & e : arr) //for循环依次处理容器中的所有数据    {        e += 2;//等价于e = e + 2        cout << e << endl;    }return 0;   }

这里写图片描述

例 6-5 指针的定义、赋值与使用

// 6_5.cpp#include "stdafx.h"#include<iostream>  using namespace std;int main(){    int i;             //定义int型数i    int * ptr = &i;    //取i的地址赋给ptr    i = 10;            //int型数赋初值    cout << "i= " << i << endl;     //输出int型数的值    cout << "&i= " << *ptr << endl; //输出int型指针所指地址的内容    return 0;}

结果:
i = 10
*ptr = 10

例 6-6 void类型指针的使用

#include "stdafx.h"#include<iostream>  using namespace std;int main(){ // void voidPbject; 错,不能申明 void类型的变量,不能定义无类型的变量,否则编译器无法知道该赋予多少内存空间给变量    void *pv; //对,可以申明void类型的指针,只能存放指针,不能访问对象,因为不知道具体对象的类型    int i = 5;    pv = &i;  //void类型指针指向整型变量    int *pint = static_cast<int*>(pv);//void指针转换为int指针    cout << "*pint= " << *pint << endl;//通过指针运算访问对象    return 0;}
原创粉丝点击