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

实验6 模板类和文件IO

时间:2022-12-04 16:22:54浏览次数:28  
标签:cout int Vector 实验 IO include open 模板 size

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("data1.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<char, N> x;

    ifstream in;
    in.open("data1.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";
}

task4.cpp

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

using namespace std;

void test() {

    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();
    return 0;
}

vector.hpp

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

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

template <typename T>
Vector<T>::Vector(int n) : size{n}
{
    p = new T[n];
}

template <typename T>
Vector<T>::Vector(int n, T x) : size{n}
{
    p = new T[n];
    for (auto i = 0; i < n; i++)
    {
        p[i] = x;
    }
}

template <typename T>
Vector<T>::~Vector()
{
    delete[] p;
}

template <typename T>
Vector<T>::Vector(const Vector<T> &vp) : size{vp.size}
{
    p = new T[size];
    for (auto i = 0; i < get_size(); i++)
    {
        p[i] = vp.p[i];
    }
}

task5.cpp

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

int main(int argc, char const *argv[])
{
    ofstream outFile;
    outFile.open("cipher_key.txt");
    if (!outFile.is_open())
    {
        cout << "fail to open data1.dat\n";
        return 1;
    }
    for (int i = 0; i < 27; i++)
    {
        if (!i)
        {
            cout << "   ";
            outFile << "   ";
            for (int j = 97; j < 123; j++)
            {
                cout << char(j) << " ";
                outFile << char(j) << " ";
            }
            cout << endl;
            outFile << endl;
            continue;
        }
        cout << setiosflags(ios_base::left) << setw(2) << i << " ";
        outFile << setiosflags(ios_base::left) << setw(2) << i << " ";
        for (int j = i; j < i + 26; j++)
        {
            cout << setiosflags(ios_base::left) << setw(2) << char(65 + j % 26);
            outFile << setiosflags(ios_base::left) << setw(2) << char(65 + j % 26);
        }
        cout << endl;
        outFile << endl;
    }
    outFile.close();
    return 0;
}

 

标签:cout,int,Vector,实验,IO,include,open,模板,size
From: https://www.cnblogs.com/cflxl/p/16950056.html

相关文章

  • 实验6 模板类和文件io
    task4Vector.cpp#pragmaonceusingnamespacestd;template<typenameT>classVector{public:Vector(intn);Vector(intn,Tv);~Vector();V......
  • 实验六
    #include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};ofstreamout;ou......
  • django学习笔记-模板语法
    1.参数传递name="张三"alist=['a','b','c']adict={"name":"张三","role":"管理员","gender":"男"}data_list=[{"name":&......
  • springboot+spring-session
    1.实现背景  测试环境上部署了一个单机项目,项目的context-path为空,之后再经过nginx的转发进行部署,项目可以正常进行登录等等一系列操作;生产环境跟测试环境代码完全相......
  • Android Studio 生成 keystore 签名文件及打包验证流程
    前言Android应用必须进行数字签名才可以发布,也就是我们平时所说的使用证书打包然后上传市场,其中数字证书的私钥由应用开发者持有。生成签名的方式有很多种,今天我们来讲......
  • 实验6
    一、实验目的体验模板函数、模板类的编写,从多态角度理解模板函数和模板类(类型作为参数)体验标准I/O流类、文件I/O流类、字符串I/O流类的用法,能正确使用针对问题场景,能......
  • DataCollatorForTokenClassification
    目录DataCollatorMixin类DataCollatorForTokenClassification类DataCollatorMixin类classDataCollatorMixin:def__call__(self,features,return_tensors=None)......
  • 二十三. 单臂路由实验和三层交换实验
    1.单臂路由实验图配置流程第1个里程:配置交换机2接口配置<Huawei>sys[Huawei]sysnameS1[S1]vlanbatch1020[S1]intg0/0/2[S1-GigabitEthernet0/0/2]portl......
  • 遍历List时删除元素导致List抛出java.util.ConcurrentModificationException异常
    1publicstaticvoidmain(String[]args){2List<String>list=newArrayList<String>();3list.add("1");4list.add(......
  • Insertion Sort List
    SourceSortalinkedlistusinginsertionsort.Agraphicalexampleofinsertionsort.Thepartialsortedlist(black)initiallycontainsonlythefirstelem......