首页 > 其他分享 >实验6 模板类和文件IO

实验6 模板类和文件IO

时间:2022-12-06 19:44:16浏览次数:39  
标签:cout int Vector 实验 IO include open 模板 out

task3_1.cpp

#include<iostream>
#include<fstream>
#include<array>
#define N 5

int main() {
	using namespace std;

	array<int, N>x{ 97,98,99,100,101 };

	ofstream out;
	out.open("datal.dat", ios::binary);
	if (!out.is_open()) {
		cout << "fail to open datal.dat\n";
		return 1;
	}

	out.write(reinterpret_cast<char*>(&x), sizeof(x));
	out.close();
}

  task3_2.cpp

#include<iostream>
#include<fstream>
#include<array>
#define N 5

int main() {
	using namespace std;
	array<int, N>x;

	ifstream in;
	in.open("datal.dat", ios::binary);
	if (!in.is_open()) {
		cout << "fail to open datal.dat\n";
	}

	in.read(reinterpret_cast<char*>(&x), sizeof(x));
	in.close();

	for (auto i = 0;i < N;++i)
		cout << x[i] << ",";
	cout << "\b \n";
}

  

int占4字节,char读取1字节,读取了ab之间的三个字节的" "。

task4.cpp

#pragma once
#include<iostream>

using namespace std;
template<typename T>
class Vector {
public:
	int s;
	T* p;

	Vector(int m) 
	{
		p = new T[m];
		s = m;
	}
	Vector(int m,T value)
	{
		p = new T[m];
		s = m;
		for (int i = 0;i < m;i++)
			p[i] = value;
	}
	Vector(const Vector& obj) 
	{
		s = obj.s;
		p = new T[s];
		for (int i = 0;i < s;i++)
			p[i] = obj.p[i];
	}
	~Vector() = default;

	int get_size()
	{
		return s;
	}
	T &at(int m)
	{
		return p[m];
	}

	T &operator[](T i)
	{
		return p[i];
	}

	friend void output(const Vector& x)
	{
		for (int i = 0;i < x.s;i++)
			cout << x.p[i] << ",";
		cout << "\b" << " " << endl;
	}
};

  

task5.cpp

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

void output(ostream& out)
{
	char a[30][30];
	char x;
	cout << "  ";
	out << "  ";
	for (x = 'a';x <= 'z';x++)
	{
		cout << setw(2) << x;
		out << setw(2) << x;
	}
	cout << endl;
	out << endl;
	for (int i = 0;i <= 26;i++)
	{
		cout << setw(2) << i;
		out << setw(2) << i;
		for (int j = 0;j < 26;j++)
		{
			a[i][j] = 'A' + i+ j;
			if (a[i][j] > 'Z')
				a[i][j] -= 26;
			cout << setw(2) << a[i][j];
			out << setw(2) << a[i][j];
		}
		cout << endl;
		out << endl;
	}

}

int main()
{
	ofstream out;
	out.open("cipher_key.txt", ios::out);
	if (!out.is_open())
		return 1;
	output(out);
	out.close();
}

  

 

标签:cout,int,Vector,实验,IO,include,open,模板,out
From: https://www.cnblogs.com/Samapoketto/p/16960304.html

相关文章

  • 实验六
    3.1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101}......
  • hdu1814 Peaceful Commission--dfs
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1814​​题意:n个党派参加会议,每个党派有2名代表,且每个党派必须只能派一个人去,但是有m对人是有仇的,他们不能同时参......
  • nginx: [emerg] no "events" section in configuration
    添加events,如下图http{includemime.types;default_typeapplication/octet-stream;keepalive_timeout65;server{listen80......
  • 实验六 模板类和文件IO
    1.1Vector.hpp:#include<iostream>#include<string>#include<iomanip>usingnamespacestd;template<classT>classVector{public:Vector(intn):s......
  • 实验6
    实验四vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public:Vector(intn):size{n}{p=newT[n......
  • Spring Session for Apache Geode 教程
    1.简介Spring会话提供了用于管理用户会话信息的API和实现。它还提供与以下各项的透明集成:HttpSession-使它能够被集群化(即复制)实现高可用性),而无需绑定到特定于应用程......
  • Python异步爬虫(aiohttp版)
    异步协程不太了解的话可以去看我上篇博客:https://www.cnblogs.com/Red-Sun/p/16934843.htmlPS:本博客是个人笔记分享,不需要扫码加群或必须关注什么的(如果外站需要加群或关......
  • 详解redis网络IO模型
    前言"redis是单线程的"这句话我们耳熟能详。但它有一定的前提,redis整个服务不可能只用到一个线程完成所有工作,它还有持久化、key过期删除、集群管理等其它模块,redis会通......
  • <五>模板的完全特例化和非完全特例化
    模板作为C++泛型编程的基础十分重要,其使得一份代码能用于处理多种数据类型。而有些时候,我们会希望对一些特定的数据类型执行不同的代码,这时就需要使用模板特例化(template......
  • C++的region代码块折叠
    之前用C#,有比较方便的#region功能:#region代码块名//...代码块#endregion 原来C++也有类似功能,示例如下:#pragmaregion代码块名//...#pragmaendregion......