首页 > 其他分享 >实验六

实验六

时间:2024-12-17 17:23:01浏览次数:8  
标签:index const int Vector 实验 include size

任务4
vector.hpp

#pragma once
#include<iostream>
#include<stdexcept>
using std::cout;
using std::endl;
template <typename T>
class Vector{
	public:
		Vector(int s);
		Vector(int s,T v);
		Vector(const Vector<T> &v);
		~Vector();
		int get_size()const;
		T& at(int index){
			return static_cast<const Vector*>(this)->at(index);
		}
		 T& at(int index)const;
		T& operator[](int index)const;
		T& operator[](int index){
			return  static_cast<const Vector*>(this)->operator[](index);
		}
		template <typename T1>
		friend void output(const Vector<T1>&);
		
	private: 
		int size;//数组大小 
		T *ptr;//数组类型 
};


template <typename T>
Vector<T>::Vector(int s):size(s)
{
	if(s<0)
	{
		throw std::length_error("vector constructor: negative size"); 
	}
	ptr = new T[size];
}

template <typename T>
Vector<T>::Vector(int s,T v):size(s)
{
	if(s<0)
	{
		throw std::length_error("vector constructor: negative size"); 
	}
	ptr = new T[size];
	for(int i=0;i<size;++i)
	{
		ptr[i] = v;
	}

}

template <typename T>
Vector<T>::Vector(	const Vector &v):size(v.size),ptr(new T[size])
{
	if(v.size<0)
	{
		throw std::length_error("vector constructor: negative size"); 
	}
	for(int i=0;i<size;++i)
	{
		ptr[i] = v.ptr[i];
	}
}

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

template <typename T>
int Vector<T>::get_size()const{
	return this->size;
}

template <typename T>
	T& Vector<T>::at(int index)const{
		if(index<0 || index >=size)
		{
			throw std::out_of_range("vector::at():index out of this->size");
		}
		return ptr[index];
	}
	
template<typename T>
   T& Vector<T>::operator[](int index)const
	{
			if(index<0 || index >=size)
	{
			throw std::out_of_range("vector::operator[]:index out of this->size");
	}
	return ptr[index];
	}
	
	template<typename T1>
	 void output(const Vector<T1>& v){
		 for (int i = 0; i < v.get_size(); ++i) {
        std::cout << v.at(i);
        if (i < v.get_size() - 1) {
            std::cout << ",";
        }
    }
    std::cout << std::endl;
	}

task4.cpp

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

void test1() {
    using namespace std;

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

    cout << "x1: "; output(x1);

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

    cout << "x2: "; output(x2);
    cout << "x3: "; output(x3);

    x2.at(0) = 77;
    x2.at(1) = 777;
    cout << "x2: "; output(x2);
    cout << "x3: "; output(x3);
}

void test2() {
    using namespace std;

	
//    int n, index;
//    while(cout << "Enter n and index: ", cin >> n >> index) {
//        try {
//            Vector<int> v(n, n);
//            v.at(index) = -999;
//            cout << "v: "; output(v);
//        }
//        catch (const exception &e) {
//            cout << e.what() << endl;
//        }
//    }
test1();
}

int main() {
    cout << "测试1: 模板类接口测试\n";
    test1();

    cout << "\n测试2: 模板类异常处理测试\n";
    test2();
}

结果:


中间出现过的报错信息:
[Warning] friend declaration 'void output(const Vector&)' declares a non-template function [-Wnon-template-friend]


解决方案:这个警告信息表明,您在 Vector 类中将 output 函数声明为友元函数时,没有将其声明为模板函数。然而,从上下文来看,output 函数应该是一个模板函数,因为它需要能够处理不同类型的 Vector。但是当我们声明template 的时候,又会出现关于模板T的报错,因此我改成了:


	template <typename T1>
		friend void output(const Vector<T1>&);

任务5:

student.hpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
// 学生结构体
struct Student {
    string id;
    string name;
    string major;
    int score;

    // 用于排序的比较函数(降序比较分数)
    bool operator<(const Student& other) const {
        if (major != other.major) {
            return major < other.major; // 先按专业字典序排序
        } else {
            return score > other.score; // 专业相同则按分数降序排序
        }
    }
};

// 从文件中读取学生信息
vector<Student> readStudentsFromFile(const string& filename) {
    vector<Student> students;
    ifstream file(filename);
    string line;

    while (getline(file, line)) {
        istringstream iss(line);
        Student student;
        iss >> student.id >> student.name >> student.major >> student.score;
        students.push_back(student);
    }

    file.close();
    return students;
}

// 将学生信息写入文件
void writeStudentsToFile(const vector<Student>& students, const string& filename) {
    ofstream file(filename);
    for (const auto& student : students) {
        file << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << "\n";
    }
    file.close();
}

task5.cpp

#include "student.hpp"
#include<iostream>
#include<fstream>
int main() {
    vector<Student> students = readStudentsFromFile("data5.txt");

    // 使用STL的sort函数进行排序,这里需要提供一个比较函数或操作符重载
    sort(students.begin(), students.end());

    // 打印到屏幕
    for (const auto& student : students) {
        cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << "\n";
    }

    // 保存到文件
    writeStudentsToFile(students, "ans5.txt");

    return 0;
}

结果:

结束后的文件目录

标签:index,const,int,Vector,实验,include,size
From: https://www.cnblogs.com/mjdlearning/p/18612682

相关文章

  • 实验六
    task4:代码:1#pragmaonce23#include<iostream>4#include<stdexcept>5#include<cmath>67usingnamespacestd;89template<typenameT>10classVector{11private:12intsize;13T*ptr;14......
  • 实验六
    实验任务四代码vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<stdexcept>5#include<iostream>//确保可以使用std::cout和std::endl67template<typenameT>8classVector{9private:10T*data_;11......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4:实验代码:1#pragmaonce2#include<iostream>3#include<stdexcept>4usingnamespacestd;5template<typenameT>6classVector{7public:8Vector(intn,constT&value=T()):size(n){9if(n<0......
  • 实验6
    task1task1.cpp#include"Complex.hpp"#include<iostream>#include<fstream>#include<stdexcept>voidtest1();voidtest2();intmain(){usingnamespacestd;cout<<"测试1:复数模板类测试"<<endl;......
  • 实验6 模板类、文件I/O和异常处理
    实验内容4Vector.hpp1#pragmaonce2#include<iostream>3#include<stdexcept>45usingnamespacestd;67template<typenameT>89classVector{10private:11intsize;12T*ptr;1314publi......
  • 实验六
    1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>//用于异常处理67usingnamespacestd;89template<typenameT>10classVector{//类名修改为MyVector,避免与std::vector冲突11priva......
  • 实验 6
    实验6实验任务4//Vector.hpp#include<iostream>#include<stdexcept>#include<memory>template<typenameT>classVector{public://构造函数,动态指定大小explicitVector(intsize){if(size<0){throwstd......
  • 实验6
    实验任务4: 1#include<iostream>2#include<stdexcept>34usingnamespacestd;56template<typenameT>7classVector{8public:9Vector(intn);10Vector(intn,Tvalue);11Vector(constVector<T&g......
  • 实验6
    实验任务4Vector.hpp代码点击查看代码#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public: Vector(intn); Vector(intn,Tvalue); Vector(constVector<T>&v); ~Vector(); intget_siz......
  • 实验6
    实验任务4:实验代码:Vector.hpp:1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>9classVector{10public:11Vector(intn,intvalue=0);12Vector(const......