C++大学教程(第九版)7.19 将7.10节vector对象的例子转换成array对象

news2024/9/25 1:23:37

文章目录

  • 题目
  • 代码
  • 运行截图

题目

(将7.10节vector 对象的例子转换成array 对象)将图7.26中 vector 对象的例子转换成使用array
对象。请消除任何 vector 对象仅有的特性。

分析:

vector对象独有的特性:
1.vector对象长度可变
2.长度不同的vector对象可以直接赋值,以及比较(!=) (==)可以直接使用
3.函数形参无需包括vector对象的长度

转换成array对象需要修改的地方:
1.array对象大小不可变,所以不同长度的array对象需要不同的输入输出函数
2.长度不同的array对象不可以直接赋值;长度相同可以直接赋值;
长度不同的array对象比较时首先比较array对象长度,
若array对象长度不同,则两个array对象一定不同;
若array对象长度相同,才可以比较array对象是否相同(比较方法: 遍历,逐个比较元素大小)
3.不同的输入输出函数面对不同的array对象的长度,自动调用相应大小的函数。
例如:定义了四个函数(两个输入,两个输出)

代码

#include <iostream>
#include <iomanip>
#include <array>
#include <stdexcept>

using namespace std;

const int arraySize1 = 7;
const int arraySize2 = 10;

void outputVector(const array<int, arraySize1> &);
void outputVector(const array<int, arraySize2> &);
void inputVector(array<int, arraySize1> &);
void inputVector(array<int, arraySize2> &);

int main()
{
    array<int, arraySize1> integers1 = {};
    array<int, arraySize2> integers2 = {};

    cout << "Size of array integers1 is " << integers1.size()
         << "\narray after initialization:" << endl;
    outputVector(integers1);

    cout << "Size of array integers2 is " << integers2.size()
         << "\narray after initialization:" << endl;
    outputVector(integers2);

    cout << "\nEnter 17 integers:" << endl;
    inputVector(integers1);
    inputVector(integers2);

    cout << "\nAfter input,the arrays contain:\n"
         << "integers1:" << endl;
    outputVector(integers1);
    cout << "integers2:" << endl;
    outputVector(integers2);

    cout << "\nEvaluating:integers1 != integers2" << endl;

    // 比较integers1和integers2是否相同
    if (integers1.size() != integers2.size()) // 如果两个array对象大小不同,则一定不同
    {
        cout << "integers1 and integers2 are not equal" << endl;
    }
    else
    {
        bool isEqual = true;

        for (size_t i = 0; i < integers1.size(); i++)
        {
            if (integers1[i] != integers2[i])
            {
                isEqual = false;
                break;
            }
        }

        if (isEqual)
        {
            cout << "integers1 and integers2 are equal." << std::endl;
        }
        else
        {
            cout << "integers1 and integers2 are not equal." << std::endl;
        }
    }

    array<int, arraySize1> integers3 = integers1; // integers3进行初始化,用integers1赋值

    cout << "\nSize of array integers3 is " << integers3.size()
         << "\narray after initialization:" << endl;
    outputVector(integers3);

    cout << "\nAssigning integers2 to integers1:" << endl;
    for (size_t i = 0; i < arraySize1; ++i)
    { // 将integers2的值赋给integers1
        integers1[i] = integers2[i];
    }

    cout << "integers1:" << endl;
    outputVector(integers1);
    cout << "integers2:" << endl;
    outputVector(integers2);

    cout << "\nEvaluating:integers1 == integers2" << endl;

    if (integers1.size() != integers2.size()) // 如果两个array对象大小不同,则一定不同
    {
        cout << "integers1 and integers2 are not equal" << endl;
    }
    else
    {
        bool isEqual = true;

        for (size_t i = 0; i < integers1.size(); i++)
        {
            if (integers1[i] != integers2[i])
            {
                isEqual = false;
                break;
            }
        }

        if (isEqual)
        {
            cout << "integers1 and integers2 are equal." << std::endl;
        }
        else
        {
            cout << "integers1 and integers2 are not equal." << std::endl;
        }
    }

    cout << "\nintegers1[5] is " << integers1[5] << endl;

    cout << "\n\nAssigning 1000 to integers1[5]" << endl;
    integers1[5] = 1000;
    cout << "integers1:" << endl;
    outputVector(integers1);

    try
    {
        cout << "\nAttempt to display integers1.at(15)" << endl;
        cout << integers1.at(15) << endl;
    }
    catch (out_of_range &ex)
    {
        cerr << "An exception occurred: " << ex.what() << endl;
    }

    cout << "\nCurrent integers3 size is: " << integers3.size() << endl;
    // integers3.push_back(1000);//array对象无法添加新元素,所以此处代码直接注释掉
    // cout << "New integers3 size is:" << integers3.size() << endl;
    outputVector(integers3);
    return 0;
}

void outputVector(const array<int, arraySize1> &items)
{ // integers1和integers3输出
    for (int item : items)
    {
        cout << item << " ";
    }
    cout << endl;
}

void outputVector(const array<int, arraySize2> &items)
{ // integers2输出
    for (int item : items)
    {
        cout << item << " ";
    }
    cout << endl;
}

void inputVector(array<int, arraySize1> &items)
{ // integers1和integers3输入
    for (int &item : items)
        cin >> item;
}

void inputVector(array<int, arraySize2> &items)
{ // integers2输入
    for (int &item : items)
        cin >> item;
}

运行截图

在这里插入图片描述
在这里插入图片描述

如有问题,欢迎评论一起交流,正在努力学习中~~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1415149.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于springboot校友社交系统源码和论文

校友社交系统提供给用户一个校友社交信息管理的网站&#xff0c;最新的校友社交信息让用户及时了解校友社交动向,完成校友社交的同时,还能通过论坛中心进行互动更方便。本系统采用了B/S体系的结构&#xff0c;使用了java技术以及MYSQL作为后台数据库进行开发。系统主要分为系统…

为什么选择快速应用开发:提高业务响应速度与竞争力的关键

如今&#xff0c;企业想要持续蓬勃发展&#xff0c;就需要具备快速满足客户期望的能力。无论是十几年历史的重要市场占有者推出新的APP&#xff0c;还是在疫情期间从线下转向线上电商营销&#xff0c;企业都需要主动适应市场。随着为客户提供新的服务方式&#xff0c;员工也需要…

以前年度资产价值导入以及汇率自动计算解决方案

文章目录 1 Introduction2 Code3 Summary 1 Introduction In the example we will finish ABLDT function and modify asset value . 2 Code DATA: key TYPE bapi1022_key,generaldata TYPE bapi1022_feglg001,generaldatax TYPE bapi1…

品牌突围|内容营销「共创公式」全面讲解

为什么品牌要扎根小红书&#xff1f;除了种草投放&#xff0c;品牌还能做些什么&#xff1f; 在小红书&#xff0c;迎接消费者共创的时代&#xff0c;激活品牌营销的无限潜能。 拥抱多元 在新机遇中预见未来 2023年&#xff0c;各大社交媒体平台涌现出了许多热点&#xff0c…

keil使用教程

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pandas是什么&#xff1f;二、使用步骤 1.引入库2.读入数据 总结 前言 例如&#xff1a;随着人工智能的不断发展&#xff0c;机器学习这门技术也越来越重…

jupyter python笔记杂乱

问题产生的原因: 无法执行sess.run()的原因是tensorflow版本不同导致的&#xff0c;tensorflow版本2.0无法兼容版本1.0 解决办法: tf.compat.v1.disable_eager_execution() 确保tf’2能运行tf1的代码 notebok打开指定文件夹 直接解决

代码随想录刷题笔记-Day12

1. 二叉树的递归遍历 144. 二叉树的前序遍历https://leetcode.cn/problems/binary-tree-preorder-traversal/94. 二叉树的中序遍历https://leetcode.cn/problems/binary-tree-inorder-traversal/145. 二叉树的后续遍历https://leetcode.cn/problems/binary-tree-postorder-tra…

鸿蒙:@Link装饰器-父子双向同步

子组件中被Link装饰的变量与其父组件中对应的数据源建立双向数据绑定。从API version 9开始&#xff0c;该装饰器支持在ArkTS卡片中使用。 需要注意&#xff1a;Link装饰的变量与其父组件中的数据源共享相同的值。Link装饰器不能在Entry装饰的自定义组件中使用。 一、装饰器使…

【word】论文、报告:①插入图表题注,交叉引用②快速插入图表目录③删改后一键更新

【word】①插入图表题注&#xff0c;②删改后一键更新 写在最前面插入题注交叉引用修改插入题注的文字格式快速插入图表目录 插入题注后有删改&#xff0c;实现编号一键更新 &#x1f308;你好呀&#xff01;我是 是Yu欸 &#x1f30c; 2024每日百字篆刻时光&#xff0c;感谢你…

按配置数据绘制配置型地图marker的icon,自定义marker

一、需求 需要自定义配置数据的marker&#xff0c;其中图片内容要灵活可配置自动生成。此处项目用的百度地图。 效果图&#xff1a; 二、思路 用背景图canvas绘制数字的方式生成icon的图片资源。 再将icon生成对应地图marker。 三、代码 canvasImg.js <!-- * descrip…

【misc | CTF】攻防世界 适合作为桌面

天命&#xff1a;这题还挺繁琐的&#xff0c;知识点还不少 目录 步骤1&#xff1a;图片隐写 步骤2&#xff1a;Winhex查看ascii码 步骤1&#xff1a;图片隐写 拿到这张图片&#xff0c;不可能扔进ps会有多图层&#xff0c;普通图片也就一个图层而已 但居然可以有隐写图片这…

数据结构之生成树及最小生成树

数据结构之生成树及最小生成树 1、生成树概念2、最小生成树 数据结构是程序设计的重要基础&#xff0c;它所讨论的内容和技术对从事软件项目的开发有重要作用。学习数据结构要达到的目标是学会从问题出发&#xff0c;分析和研究计算机加工的数据的特性&#xff0c;以便为应用所…

Linux-ROS学习之旅-话题编程(二)

##承接上一篇文章的知识&#xff0c;有下面的实例操作 通过代码新生一个海龟&#xff0c;放置在(5,5)点&#xff0c;命名为turtle2&#xff0c;通过代码订阅turtle2的实时位置并打印在终端&#xff0c;控制turtle2实现旋转运动 步骤&#xff1a; 1.创建一个工作空间和一个功…

可以实时监控电脑的软件有哪些?好用的四款电脑监控软件【高人气收藏分享】

在当今数字化时代&#xff0c;电脑已经成为我们工作和生活中不可或缺的工具。然而&#xff0c;有时候我们需要对电脑进行监控&#xff0c;以确保工作效率和保护个人隐私。因此&#xff0c;选择一款好的电脑监控软件非常重要。本文将介绍四款好用的电脑监控软件&#xff0c;并探…

行测-资料:1. 速算技巧、基期与现期

1、速算技巧 1.1 截位直除 1.1.1 截位 1.1.2 截谁 1.1.3 截几位 选项差距大&#xff1a; 四个选项首位均不同首位相同&#xff0c;第二位差大于首位 选项差距小&#xff1a; 首位相同且第二位差小于等于首位 例子 C&#xff0c;截两位。 C&#xff0c;截两位。 B&#xff0c;截…

第十七讲_HarmonyOS应用开发Stage模型应用组件

HarmonyOS应用开发Stage模型应用组件 1. 应用级配置2. Module级配置3. Stage模型的组件3.1 AbilityStage3.1.1 AbilityStage的创建和配置3.1.2 AbilityStage的生命周期回调3.1.3 AbilityStage的事件回调&#xff1a; 3.2 UIAbility3.2.1 UIAbility生命周期3.2.3 UIAbility启动模…

mysql INSERT数据覆盖现有元素(若存在)

INSERT...ON DUPLICATE KEY UPDATE的使用 如果指定了ON DUPLICATE KEY UPDATE&#xff0c;并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值&#xff0c;则会更新ON DUPLICATE KEY UPDATE关键字后面的字段值。 例如&#xff0c;如果列a被定义为UNIQUE&#xff0…

P1226 【模板】快速幂题解

题目 给你三个整数a,b,p&#xff0c;求 mod p。 输入输出格式 输入格式 输入只有一行三个整数&#xff0c;分别代表a,b,p。 输出格式 输出一行一个字符串a^b mod ps&#xff0c;其中a,b,p分别为题目给定的值&#xff0c;s为运算结果。 输入输出样例 输入样例 2 10 9 …

三角函数、反三角函数

一、三角函数 二、反三角函数&#xff1a;已知三角函数值&#xff0c;反算角度大小 因为严格单调函数才有反函数一个y对应一个x&#xff0c;显然ysinx&#xff0c;ycosx&#xff0c;ytanx在其定义域并不是严格单调&#xff0c;所以需要人为划定范围。 1. 研究yarcsinx、yarcco…

第十六回 花和尚单打二龙山 青面兽双夺宝珠寺-FreeBSD基本操作:查找程序和文件的路径

杨志最终没有跳崖而是怅然下了冈子。那十四个人醒来&#xff0c;后悔没有听杨志的话&#xff0c;现在事情已经发生了&#xff0c;大家只好商量把所有的错推到杨志身上&#xff0c;说他和强盗合伙劫了生辰纲。 杨志到酒店吃酒没钱付账&#xff0c;跟店家打起来&#xff0c;却原…