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

本文主要是介绍C++大学教程(第九版)7.19 将7.10节vector对象的例子转换成array对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 题目
  • 代码
  • 运行截图

题目

(将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的值赋给integers1integers1[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;
}

运行截图

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

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

这篇关于C++大学教程(第九版)7.19 将7.10节vector对象的例子转换成array对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/652612

相关文章

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地

电脑提示d3dx11_43.dll缺失怎么办? DLL文件丢失的多种修复教程

《电脑提示d3dx11_43.dll缺失怎么办?DLL文件丢失的多种修复教程》在使用电脑玩游戏或运行某些图形处理软件时,有时会遇到系统提示“d3dx11_43.dll缺失”的错误,下面我们就来分享超... 在计算机使用过程中,我们可能会遇到一些错误提示,其中之一就是缺失某个dll文件。其中,d3dx11_4

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤