首页 > 其他分享 >实验六

实验六

时间:2022-12-01 21:55:21浏览次数:39  
标签:int x2 Vector 实验 template n0 output

任务三:

 任务四:

#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();
}
#pragma once
#include<iostream>
#include<vector>
using namespace std;
template<typename T>
class Vector {
public:
    Vector(int n0);
    Vector(int n0, T value0);
    Vector(const Vector<T>& v1);
    ~Vector();
    int get_size()const { return n; }
    T &at(int i) { return p[i]; }
    T &operator[](int i) {return p[i]; }
    template<typename T>
    friend void output(Vector<T>&x);
    T value;
private:
    int n;
    T *p;
    
};
template<typename T>
Vector<T>::Vector(int n0) {
    n = n0;
    p = new T[n];
}
template<typename T>
Vector<T>::Vector(int n0,T value0){
    n = n0;
    value = value0;
    p = new T[n];
    for (auto i = 0; i < n; i++)
        p[i] = value;
}
template<typename T>
Vector<T>::Vector(const Vector<T>& v1) {
    n = v1.n;
    p = new T[n];
    for (int i = 0; i < n; i++) {
        p[i] = v1.p[i];
    }
}
template<typename T>
Vector<T>::~Vector(){
    delete p;
}
template<typename T>
void output(Vector<T>& x) {
    for (int i = 0; i < x.n; i++)
        cout << x.at(i) << ' ';
    cout << endl;
}

 

标签:int,x2,Vector,实验,template,n0,output
From: https://www.cnblogs.com/hjr123/p/16942127.html

相关文章

  • 实验六
     任务四、vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public:Vector(intn):size{n}{p=ne......
  • 实验6 模板类和文件I/O
    实验任务3#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • Android实验——使用Intent在Activity间传输数据
    一、实验要求和目的理解Activity组件的功能与作用;掌握使用Intent在多个Activity组件间传输数据的方法;掌握在AndroidManifest.xml中配置Activity组件的方法。二、实验......
  • Android实验——事件处理:显示持续触摸时间
    一、实验要求和目的掌握基于监听的事件处理机制,根据需求能够编写相应的事件处理程序。能够熟练应用各种布局管理器和控件进行界面设计。二、实验环境部署有Android......
  • web实验2
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范ubuntu运行截图1.......
  • web实验1-socket
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.time服务器的客户端服务器,提交程序运行截图timeserver代码#include<stdio.h>#include<unistd.h>#inc......
  • 实验六
    实验四#include<iostream>#include<cassert>usingstd::cout;usingstd::endl;template<classT>classVector{public:Vector(intn,intm);Vector(i......
  • 实验六
    TASK3:int:char: char和int的的字节数不一样,int占一个字节数,插入占4个,改为char后,按4个数据写入,则写入了97“ 空格”“空格”“ 空格”“空格”,然后写入98,在写......
  • 实验四 Web服务器1-socket编程
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:time服务器的客户端服务器,提交程序运行截图echo服务器的客户端服务器,提交程序运行截图,服务器把客......
  • 实验6-模板类和文件
    task3:task3.1.cpp1#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain()6{7usingnamespacestd;8array<int......