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

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

spring AMQP代码生成rabbitmq的exchange and queue教程

《springAMQP代码生成rabbitmq的exchangeandqueue教程》使用SpringAMQP代码直接创建RabbitMQexchange和queue,并确保绑定关系自动成立,简... 目录spring AMQP代码生成rabbitmq的exchange and 编程queue执行结果总结s

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基