c++简单程序设计-5

编程实验部分
1.vector3.cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;// 函数声明 
void output1(vector<string> &);  
void output2(vector<string> &);  int main()
{vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
    likes.push_back("favorite book");// 为vector<string>数组对象likes添加元素值likes.push_back("music");likes.push_back("film");likes.push_back("anime");cout << "-----I like these-----" << endl;output1(likes);// 调用子函数输出vector<string>数组对象likes的元素值 dislikes.push_back("sport");// 为vector<string>数组对象dislikes添加元素值 dislikes.push_back("sportsman");cout << "-----I dislike these-----" << endl;output1(dislikes);// 调用子函数输出vector<string>数组对象dislikes的元素值 
    likes.swap(dislikes);// 交换vector<string>对象likes和dislikes的元素值 
cout << "-----I likes these-----" << endl;output2(likes);// 调用子函数输出vector<string>数组对象likes的元素值 cout << "-----I dislikes these-----" << endl;output2(dislikes);// 调用子函数输出vector<string>数组对象dislikes的元素值 return 0; }// 函数实现 // 以下标方式输出vector<string>数组对象v的元素值 void output1(vector<string> &v) {int i;for(i=0;i<v.size();i++){cout<<v[i]<<endl;} }// 函数实现 // 以迭代器方式输出vector<string>数组对象v的元素值 void output2(vector<string> &v) {int i;for(i=0;i<v.size();i++){cout<<v[i]<<endl;} }

 

 

 2. 6-17的修改

#include<iostream>
using namespace std;int main(){                   //法1 int i=9; int *p;p=&i;cout<<"The value at p:"<<*p;return 0;
}int main(){                   //法2 int i=9; int *p=&i;cout<<"The value at p:"<<*p;return 0;
}int main(){                   //法3 int i; int *p=&i;*p=9;cout<<"The value at p:"<<*p;return 0;
}
//原题指针没有初始化会随机指向某处内存,导致程序崩溃 
//根据书上的模板写了三种方法 

3. 6-18的修改

#include<iostream>
using namespace std;int fnl(){int *p=new int(5);return *p;delete p;   //原程序未用delete加以释放,会导致内存泄漏 
}int main(){int a=fnl();cout<<"the value of a is:"<<a;return 0;
}

4.动态矩阵类Matrix

 

//matrix.h
#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
public:Matrix(int n); // 构造函数,构造一个n*n的矩阵 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 ~Matrix(); //析构函数 void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 void printMatrix() const; // 显示矩阵inline float &element(int i, int j) { return *(p + ((i - 1)*cols) + j - 1); }; //返回矩阵第i行第j列元素的引用inline float element(int i, int j) const ;// 返回矩阵第i行第j列元素的值 void setElement(int i, int j, int value) ; //设置矩阵第i行第j列元素值为valueinline int getLines() const { return lines; }; //返回矩阵行数 inline int  getCols() const { return cols; }; //返回矩阵列数 
private:int lines;    // 矩阵行数int cols;      // 矩阵列数 float *p;   // 指向存放矩阵数据的内存块的首地址 
};
#endif

 

//main.cpp
#include "Matrix.h"
#include<iostream>
using namespace std;
int main() {Matrix A(3);Matrix B(3, 2);Matrix C(B);const float a[9] = { 1,2,3,4,5,6,7,8,9 }, b[6] = { 10,20,30,40,50,60 };A.setMatrix(a);B.setMatrix(b);C.setMatrix(b);cout << "矩阵A为:" << endl;A.printMatrix();cout << "矩阵B为:" << endl;B.printMatrix();cout << "B的复制构造函数矩阵C为:" << endl;C.printMatrix();float *x = &A.element(1, 1);cout << "矩阵A第1行第1列元素的引用:" << x<<endl;cout << "矩阵第1行第1列元素的值:" << A.element(1, 1) << endl;A.setElement(1, 1, 6);A.setElement(2, 1, 6);A.setElement(3, 1, 6);cout << "矩阵A的第1列全设为6:" << endl;A.printMatrix();cout << "A的行列分别为:" << A.getLines()  << " " << A.getCols() << endl;cout << "B的行列分别为:" << B.getLines()  << " " << B.getCols() << endl;
}
//matrix.cpp
#include "Matrix.h"
#include<iostream>
using namespace std;Matrix::Matrix(int n) : lines(n) {                                     // 构造函数,构造一个n*n的矩阵cols = n;p = new float[lines*cols];
}Matrix::Matrix(int n,int m) : lines(n),cols(m) {                       // 构造函数,构造一个n*m的矩阵p = new float[lines*cols];
}Matrix::Matrix(const Matrix &X): lines(X.lines),cols (X.cols){         //复制构造函数的实现p = new float[lines*cols];
}Matrix::~Matrix() {                                                   //析构函数delete[]p;
}void Matrix::setMatrix(const float *pvalue) {        // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值for (int i = 0; i < lines*cols; i++)*(p + i) = *(pvalue + i);
}void Matrix::printMatrix() const {                                      // 显示矩阵for (int i = 0; i < lines; i++) {for (int j = 0; j < cols; j++) {cout << p[i*cols + j] << " ";}cout << endl;}
}inline float Matrix::element(int i, int j) const {                 // 返回矩阵第i行第j列元素的值 return *(p + ((i - 1)*cols) + j - 1);
}void Matrix::setElement(int i, int j, int value) {               //设置矩阵第i行第j列元素值为value*(p + ((i - 1)*cols) + j - 1) = value;
}

 

期中考试:https://www.cnblogs.com/tensheep/p/9079345.html

实验总结与体会:

书上有关vector模板的介绍还是太少了

我找了一些概括了vector模板的用法的CSDN博客

实验是大概完成了,但迭代器方式的输出还有些疑问

这次实验最难写的就是最后一题了

写的过程中经常遇见无法解析的外部符号的错误

我查了些资料,也看了看其他同学的博客

发现只要把matrix.cpp里的函数放进matrix.h里就行了

虽然我并不知道原因...

 

 



转载于:https://www.cnblogs.com/tensheep/p/9073851.html

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

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

相关文章

关于JavaScript的编译原理

引擎&#xff1a;负责整个js程序的编译和执行过程编译器&#xff1a;负责语法分析和代码生成作用域&#xff1a;收集和维护一系列查询&#xff08;由所有声明的标识符组成&#xff09; 【例子&#xff1a;声明一个变量并赋值 var a value&#xff1b;】 Step1.编译器对该程序段…

safari检查元素_如何防止Safari检查是否使用Apple Pay

safari检查元素Apple Pay’s incorporation into macOS Sierra makes it really easy to pay using the service on your Mac with your iPhone or iPad. But that doesn’t mean just because you can, you will, or will want to use Apple Pay in the future. 通过将Apple P…

spring boot中servlet启动原理

启动过程及原理 1 spring boot 应用启动运行run方法 StopWatch stopWatch new StopWatch();stopWatch.start();ConfigurableApplicationContext context null;FailureAnalyzers analyzers null;configureHeadlessProperty();SpringApplicationRunListeners listeners getRu…

某乎有人问--微软会抛弃C#吗,有点担心?

在某乎有人问&#xff1a;微软会抛弃C#吗&#xff0c;有点担心&#xff1f;&#xff0c;类似这样的问题&#xff0c;一直都有很多人在问&#xff0c;今天我们就来聊聊这个问题。没必要担心微软倒闭了&#xff0c;C#都不会消失&#xff0c;其实.Net已经不属于微软的了。C#是属于…

mailing list的原理

1 发往mailing list邮箱的邮件会被所有订阅了该邮箱的人收到 说白了&#xff0c;就是一种邮件群发机制&#xff0c;为了简化群发&#xff0c;不是将所有的收件人放到收件人列表中&#xff0c;而是发往总的邮箱即可。 2 要向该mailing list邮箱中发送邮件需要先要订阅 但是&…

icloud上传错误_如何修复HomeKit“地址未注册到iCloud”错误

icloud上传错误While Apple has made serious improvements to the HomeKit smarthome framework, there are still more than a few ghosts in the machine. Let’s look at how to banish the extremely frustrating “Address is not registered with iCloud” error to get…

Jenkins安装部署

Jenkins安装部署 Jenkins简介 Jenkins是一个开源软件项目&#xff0c;是基于Java开发的一种持续集成工具&#xff0c;用于监控持续重复的工作&#xff0c;旨在提供一个开放易用的软件平台&#xff0c;使软件的持续集成变成可能。 安装步骤 本文以CentOS7为环境&#xff0c;安装…

Angular2中的路由(简单总结)

Angular2中建立路由的4个步骤&#xff1a; 1、路由配置&#xff1a;最好新建一个app.toutes.ts文件&#xff08;能不能用ng命令新建有待调查&#xff09; Angular2中路由要解决的是URL与页面的对应关系&#xff08;比如URL是http://localhost:4200/all-people&#xff0c;那么页…

受 SQLite 多年青睐,C 语言到底好在哪儿?

SQLite 近日发表了一篇博文&#xff0c;解释了为什么多年来 SQLite 一直坚持用 C 语言来实现&#xff0c;以下是正文内容&#xff1a; C 语言是最佳选择 从2000年5月29日发布至今&#xff0c;SQLite 一直都是用 C 语言实现。C 一直是实现像 SQLite 这类软件库的最佳语言。目前&…

为什么 Random.Shared 是线程安全的

在多线程环境中使用 Random 类来生成伪随机数时&#xff0c;很容易出现线程安全问题。例如&#xff0c;当多个线程同时调用 Next 方法时&#xff0c;可能会出现种子被意外修改的情况&#xff0c;导致生成的伪随机数不符合预期。为了避免这种情况&#xff0c;.NET 框架引入了 Ra…

(3)Python3笔记之变量与运算符

一、变量 1&#xff09;. 命名规则&#xff1a; 1. 变量名不能使用系统关键字或保留关键字 2. 变量区分大小写 3. 变量命名由字母&#xff0c;数字&#xff0c;下划线组成但不能以数字开头 4. 不需要声明变量类型 是 a 1 非 int a 1 5. 查看变量内存地址 id(a), id(b) 6…

some demos

import ../css/detail.css;// 找到字符串中重复次数最多的字符 function findMax(str) {let maxChar ;let maxValue 1;if (!str.length) return;let arr str.replace(/\s/g, ).split();let obj {};for (let i 0; i < arr.length; i) {if (!obj[arr[i]]) {obj[arr[i]] …

WPF 实现视频会议与会人员动态布局

WPF 实现视频会议与会人员动态布局控件名&#xff1a;SixGridView作 者&#xff1a;WPFDevelopersOrg - 驚鏵原文链接[1]&#xff1a;https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用.NET40&#xff1b;Visual Studio 2019;接着上一篇是基于Grid实现的视频查看感…

汉三水属国(北地属国、安定属国)

汉三水属国&#xff08;北地属国、安定属国&#xff09; 两汉&#xff08;西汉、东汉&#xff09;400年中&#xff0c;由于各种原因&#xff0c;经常有成批的匈奴归附汉朝&#xff0c;两汉政府对他们采取了较为妥善的安置政策&#xff0c;其中最主要的措施是为他们设立专门的居…

《爆发》作者:大数据领域将有新赢家

本文讲的是《爆发》作者&#xff1a;大数据领域将有新赢家,全球复杂网络研究专家日前到访中国&#xff0c;为其新作《爆发》作宣传。他在接受国内媒体采访时表示&#xff0c;未来可能有新公司取代谷歌、Facebook等公司&#xff0c;成为大数据领域的赢家。 《爆发》一书是一本讨…

chromebook刷机_如何获取Android应用以查看Chromebook上的外部存储

chromebook刷机Android apps are a great way to expand the sometimes limited capabilities of Chromebooks, but they can be a problem if you store most of your data on an external medium—like an SD card, for example. Android应用程序是扩展Chromebook有时有限功能…

Stream流与Lambda表达式(四) 自定义收集器

一、自定义SetCustomCollector收集器 package com.java.design.Stream.CustomCollector;import java.util.*; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.Supplier; im…

ModelState.IsValid忽略型别的检查错误

Web Api在Int或DateTime如果传空值的话会自动帮忙设预设值&#xff0c;但是在ModelState.IsValid的时候&#xff0c;却会出现型别上的错误.解决方式把Model改成正确&#xff0c;也就是预设允许可以为nullpublic class DemoModel { …

android 指纹添加_如何将手势添加到Android手机的指纹扫描仪

android 指纹添加So you have a shiny new Android phone, equipped with a security-friendly fingerprint scanner. Congratulations! But did you know that, while useful on its own, you can actually make the fingerprint scanner do more than just unlock your phone…