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

实验6 模板类和文件IO

时间:2022-12-04 20:33:42浏览次数:38  
标签:int Vector 实验 IO output include out 模板 size

一、实验任务三

1.源代码:

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("data.dat",ios::binary);
  if(!out.is_open()){

    cout<<"fail to open data1.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("data.dat",ios::binary);
  if(!in.is_open()){
    cout<<"fail to open data1.dat\n";
    return 1;
  }

  in.read(reinterpret_cast<char*>(&x),sizeof(x));
  in.close();
  
  for(auto i=0;i<N;i++)
  cout<<x[i]<<", ";
  cout<<"\b\b \n";
}

2.运行测试:

 

 当把array<int,N>x;修改为array<char,N>x后:

 

原因:char类型占用1字节,int类型占用4个字节,所以输出会显示三个空格,且b在第五个位置出现。

二、实验任务四

1.源代码:

Vector.hpp

#pragma once
using namespace std;
template <typename T>
class Vector
{
private:
    int size;
    T *p;

public:
    Vector(int n): size{n} {p = new T[n];};
    Vector(int n, T x):size{n}
    {
        p = new T[n];
        for (int i = 0; i < n; i++)
            p[i] = x;
    };
    Vector(const Vector<T> &y): size{y.size}
    {
        p = new T[size];
        for (int i = 0; i < y.size; i++)
        p[i] = y.p[i];
    };
    ~Vector()=default;
    
    int get_size() { return size; }
    T &at(int index){return p[index];}
    T &operator[](int index){return p[index];}
   
    friend void output(Vector &x)
    {
        int i;
        for (i = 0; i < x.size - 1; i++)
        {
            cout << x.p[i] << ", ";
        }
        cout << x.p[i] << endl;
    }
};

task4.cpp

#include <iostream>
#include "Vector.hpp"

void test() {
    using namespace std;

    int n;
    cin >> n;
    
    Vector<double> x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i * 0.7;

    output(x1);

    Vector<int> x2(n, 42);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;
    output(x2);

    x3[0] = 999;
    output(x3);
}

int main() {
    test();
}

2.运行测试:

 

更换数据后:

 

三、实验任务5

1.源代码:

task5.cpp

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

void output(ostream &out){
 char a[26][26],i;
 cout<<" ";
 out<<" ";
 for(i='a';i<'z';i++)
 {
   cout<<setw(2)<<setfill(' ')<<i;
   out<<setw(2)<<setfill(' ')<<i;
 }
  cout<<endl;out<<endl;
 for(int j=0;j<26;j++)
 {
    cout<<setw(2)<<setfill(' ')<<j+1;
    out<<setw(2)<<setfill(' ')<<j+1;
     for(int n=0;n<26;n++)
     {
       a[j][n]='A'+char(j+n+1)%26;
       cout<<setw(2)<<setfill(' ')<<a[j][n];
       out<<setw(2)<<setfill(' ')<<a[j][n];
     }
     cout<<endl;out<<endl;
 }
}

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

2.运行测试:

 

 

 

 

标签:int,Vector,实验,IO,output,include,out,模板,size
From: https://www.cnblogs.com/xiaoshuai66/p/16950620.html

相关文章

  • dremio 源码分析学习的几个方便工具 二
    主要是在以前周边工具上做一个简单的扩展说明扩展jdwp调试 可以直接配置dremio开启jdwp方便调试,对于依赖的包可以通过dremio的安装包提供,同时也有一个简单的缺点就......
  • requestAnimationFrame和setTimeOut的区别
    requestAnmationFrame也是隔一段时间调用一次。只不过它的时间间隔是浏览器最佳刷新时间,不太短,不太长。参考:https://blog.csdn.net/weixin_44730897/article/details/1165......
  • 13.C++模板初阶
    泛型编程如何实现一个通用的交换函数呢?voidSwap(int&left,int&right){ inttemp=left; left=right; right=temp;}voidSwap(double&left,double&ri......
  • 原生axjax 和axio的结合使用
    固定请求地址和方法<body><script>/*1.新闻接口gethttp://ajax-api.itheima.net/api/news无参数*/functio......
  • 实验6 模板类和文件I/O
    实验任务3task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;8array<in......
  • 七、Cookie、Session
    7.1、会话:会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;有状态会话:一个同学来过教师,下次再来教室,我们会知道这个同学,曾经来......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};o......
  • Springboot之additional-spring-configuration-metadata.json自定义提示
    【3】@ConfigurationProperties注入属性https://blog.csdn.net/qq_25614773/article/details/124788923 https://docs.spring.io/spring-boot/docs/2.4.7/reference/ht......
  • js:Vue2.js通过CDN方式引入模板
    文档https://v2.cn.vuejs.org/v2/guide/installation.html示例index.html<!--引入Vue2.7.14--><scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.......
  • SpringBoot使用@Validation校验实体参数
    在做前后端分离时,需要提供接口给前端,那么接口的字段实体校验就显得尤为重要了在需要实体校验的实体类前加上@Validated@RequestBody注解是校验的实体类的字段上加......