C++学习笔记——第七天 数组

目标

  • 了解数组的概念
  • 熟练掌握一维和多维数组的声明与引用
  • 掌握数组的多种赋值方法
  • 熟悉数组在实际程序中的应用

声明数组

声明一维数组
<类型名><数组名>[<下标表达式>]=[<初值表>]
- 类型名 数组中各元素的数据类型。
- 数组名 是一个标识符,表示数组在内存中的起始位置,通常是个常量,不能赋值,代表着数组元素在内存中的起始位置。
- 下标表达式 一维数组中元素的个数
- 初值表 可缺省的,并不是声明数组时就需要赋初值。

声明多维数组
<类型名><数组名>[<下标表达式1>][<下标表达式2>]……[<下标表达式n>]
二维数组语法格式简化为
类型 数组名[常量表达式1][常量表达式2]
常量表达式1表示行数,常量表达式2表示列数。
二维数组在内存中的排列顺序是“先行后列”即在内存中先存第一行元素,再存第二行……

引用数组

引用一维数组
一维数组的数组元素引用的一般形式
<数组名>[<下标>]

//倒序输出输入的5个数字
#include "stdafx.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[5];
    int i,j;
    cout<<"please input 5 number:"<<endl;
    for(i=0;i<5;i++)
        cin>>a[i];
    cout<<"convert into:"<<endl;
    for(j=4;j>=0;j--)
        cout<<a[j]<<"\t";
    cout<<endl;
    getchar();
    getchar();
    return 0;
}

这里写图片描述

引用多维数组

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[3][3];
    int i,j;
    cout<<"please input 9 number:"<<endl;
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            cin>>a[i][j];
    cout<<"Output:"<<endl;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            cout<<a[i][j]<<"\t";
    cout<<endl;
    }
    cout<<endl;


    getchar();
    getchar();
    return 0;
}

这里写图片描述

数组的赋值

数组的赋值包括初始化数组和在应用中对数组进行赋值。
赋值方法:
初始化数组
数组的初始化:在定义数组时对其中的全部或部分指定初始值。只有存储类别为静态或外部的数组才可以进行初始化。格式为:
类型 数组名[数组范围]={值1,值2,……值n}


//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char a[5]={'a','b','c','d','e'};//定义包含5个元素的数组并全部初始化
    char b[]={'a','b','c'};//定义未知长度的数组并初始化
    int c[5]={1,2};
    char d[]={'a','b'};
    cout<<"a:";
    for(int i=0;i<5;i++)
        cout<<a[i]<<"\t";
    cout<<endl;
    cout<<"b:";
    for(int i=0;i<5;i++)
        cout<<b[i]<<"\t";
    cout<<endl;
    cout<<"c:";
    for(int i=0;i<5;i++)
        cout<<c[i]<<"\t";
    cout<<endl;
    cout<<"d:";
    for(int i=0;i<5;i++)
        cout<<d[i]<<"\t";
    cout<<endl;
    getchar();
    getchar();
    return 0;
}

这里写图片描述

通过赋值表达式赋值
这种方式既可以在初始化时给数组赋值,也可以在程序运行过程中赋值。

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[3];//定义数组
    a[0]=2;//通过赋值表达式给数组元素赋值
    a[1]=4;
    a[2]=6;
    cout<<a[0]<<"\t"<<a[1]<<"\t"<<a[2]<<endl;


    getchar();
    getchar();
    return 0;
}

这里写图片描述

通过输入语句赋值

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[3];//定义数组
    cout<<"please input 1st number:";
    cin>>a[0];//给第一个元素赋值
    cout<<"please input 2nd number:";
    cin>>a[1];
    cout<<"please input 3rd number:";
    cin>>a[2];
    cout<<a[0]<<"\t"<<a[1]<<"\t"<<a[2]<<endl;
    getchar();
    getchar();
    return 0;
}

这里写图片描述

通过循环语句赋值

//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[10],i;
    for(i=0;i<=9;i++)//通过循环语句给数组赋值
        a[i]=i;
    for(i=9;i>=0;i--)
        cout<<setw(4)<<a[i];//使用setw()函数进行输出格式控制
        cout<<endl;
    getchar();
    getchar();
    return 0;
}

这里写图片描述

循环语句与输入语句相结合

//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[4];
    int i;
    for(i=1;i<4;i++)//for循环语句
        cin>>a[i];//与输入相结合
    cout<<"output ";
    for(i=1;i<4;i++)
        cout<<a[i]<<"\t";
    cout<<endl;

    getchar();
    getchar();
    return 0;
}

这里写图片描述

多维数组的赋值
采用循环输入为其赋值是需要使用双重循环

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
//从键盘接收9个数字,将其存入二维数组中,并以矩阵的形式输出,
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[2][3];
    int i,j;
    cout<<"请输入6个数组元素:"<<endl;
    for(i=0;i<2;i++)    //行列循环接收输入
        for(j=0;j<3;j++)
            cin>>a[i][j];//数组赋值 
    cout<<endl<<"输入的数组如下:"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
        {
            cout<<a[i][j];//输出二维数组元素
            cout.width(5);//控制输出格式,宽度为5
        }
        cout<<endl;
    }
    getchar();
    getchar();
    return 0;
}

这里写图片描述

字符串

C++中,字符串可以通过两种方法表示:传统字符串和字符数组。
传统字符串
关于字符串的几个函数:

1.strcpy()函数
用于复制字符串,主要功能是将源字符串复制到目标字符串,调用的形式如下:
strcpy()(char destination[],const char source[]);

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char ch1[] = {"welcome"};
    char ch2[] = {"to China"};//ch2要大于ch1
    cout<<"the result is"<<endl<<strcpy(ch1,ch2)<<endl;//调用strcpy函数复制字符串


    getchar();
    getchar();
    return 0;
}

这里写图片描述

2.strcat()函数
用于连接字符串,功能是将字符串source连接到字符串target的后面,调用形式为:
strcat(char target[],const char source[]);

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char ch1[] = {"to China"};
    char ch2[] = {"Welcome"};
    cout<<strcat(ch1,ch2)<<endl;//调用strcat函数复制字符串


    getchar();
    getchar();
    return 0;
}

这里写图片描述

3.strlen函数
用于计算字符串的实际长度,一般形式为
strlen(const char string[]);

#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char ch[]={"welcome"};
    int num;
    num=strlen(ch);
    cout<<"the length of ch is:"<<num<<endl;


    getchar();
    getchar();
    return 0;
}

这里写图片描述

字符数组
char a[] ={“hello”};
char b[]={‘h’,’e’,’l’,’l’,’o’};
这两个语句代表的含义是不同的。例:从键盘上接收用户不超过10个字符的输入字符,以输入字符x表示结束,并将其倒序输出。

#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char a[10];
    int i,j;
    cout<<"请输入字符";
    for(i=0;i<10;i++)
    {
        cin>>a[i];
        if(a[i]=='x')
            break;
    }
    cout<<endl;
    for(j=i-1;j>=0;j--)
        cout<<a[j]<<" ";
    cout<<endl;



    getchar();
    getchar();
    return 0;
}

这里写图片描述
例:字符串与字符数组的区别

#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char a[] = {"Hello"};
    char b[] = {'H','e','l','l','o'};
    int la,lb;
    la=sizeof(a)/sizeof(char);
    lb=sizeof(b)/sizeof(char);
    cout<<"the length of a is"<<la<<endl;//计算字符串长度
    cout<<"the length of b is"<<lb<<endl;//计算字符数组长度


    getchar();
    getchar();
    return 0;
}

这里写图片描述
sizeof(a)表示数组a在内存中所占字节数,sizeof在实际应用中应将字符数组的长度除以本机一个字符所占的长度,因为具体的机器其字符型数据类型所占长度不一样。

数组与函数

数组经常作为函数参数,将数组中数据传送到另一个函数中。两种传递方法
1.数组元素作为函数的参数:值传送,只能从实参传送给形参,而不能从形参传送给实参。
2.数组名作为函数的参数:地址传送,把实参数组的起始地址传送给形参数组。
例:
实现输入10个学生的成绩,求出平均成绩,并将低于平均成绩的分数打印出来。

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

void readdata(float score[10])//定义函数接收用户输入
{
    cout<<"please input 10 student's score:"<<endl;
    for(int i=0;i<10;i++)
        cin>>score[i];
    return;
}
float aver(float score[10])//定义函数求平均值
{
    float sum=0;
    for(int i=0;i<10;i++)
        sum +=score[i];
    return(sum/10);
}
void print(float score[10],float ave)//定义函数输出低于平均分的分数
{
    int i;
    cout<<"the score which are below the average:";
    for(i=0;i<10;i++)
        if(score[i]<ave)
            cout<<score[i]<<" ";
        return;
}

int _tmain(int argc, _TCHAR* argv[])
{
    void readdata(float score[10]);
    float aver(float score[10]);
    void print(float score[10],float ave);
    float ave,score[10];
    readdata(score);
    ave=aver(score);
    cout<<"average"<<ave<<endl;
    print(score,ave);
    cout<<endl;


    getchar();
    getchar();
    return 0;
}

这里写图片描述

数组应用

顺序查找
例:从键盘接收一个输入从数组a中找出与该输入相同的元素值,并将该元素所在数组的位置输出。

#include <iostream>
#include <iomanip>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
    int a[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    cout<<"please input the number"<<endl;
    int iNum;
    cin>>iNum;
    for(int i=0;i<15;i++)
    {
        if(a[i] ==iNum)
        {
            cout<<"the location is:"<<i+1<<endl;
            break;
        }
    }
    if(i == 15)
    {
        cout<<"it is not in the array"<<endl;
    }
    return 0;


    getchar();
    getchar();
    return 0;
}

这里写图片描述

折半查找

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
    int a[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    cout<<"please input the number"<<endl;
    int key;
    cin>>key;
    int low = 0;
    int high= 14;
    while(low<high)
    {
        int mid=(low+high)/2;
        if(key==a[mid])
        {
            cout<<"the location is"<<mid+1<<endl;
            break;
        }
        else
        {
            if(key<a[mid])
                high=mid-1;
            else
                low=mid+1;
        }
        if(low>=high)
            cout<<"it's not in the array"<<endl;
    }


    getchar();
    getchar();
    return 0;
}

这里写图片描述

排序
冒泡排序

// LianxiProject.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a[8]={44,55,22,33,99,11,66,77};
    int iTemp;
    cout<<"before sort"<<endl;
    for(int i=0;i<8;i++)
        cout<<a[i]<<"\t";
    for(int i=1;i<8;i++)
    {
        for(int j=7;j>=i;j--)
        {
            if(a[j]<a[j-i])
            {
                iTemp=a[j-1];
                a[j-1]=a[j];
                a[j]=iTemp;
            }
        }

    }
    cout<<endl<<"after sort"<<endl;
    for(int i=0;i<8;i++)
        cout<<a[i]<<"\t";
    cout<<endl;


    getchar();
    getchar();
    return 0;
}

这里写图片描述

参考:《21天学通C++》作者:刘蕾

猜你喜欢

转载自blog.csdn.net/weixin_39795049/article/details/82529287