实验任务4:
代码:
Vector.hpp
查看代码
#pragma once
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
class Vector {
public:
Vector(int size);
Vector(int size, T value);
Vector(const Vector<T>& other);
~Vector();
int get_size() const;
T& at(int index);
const T& at(int index) const;
T& operator[](int index);
const T& operator[](int index) const;
template <typename U>
friend void output(const Vector<U>& v);
private:
int size;
T* data;
};
template <typename T>
Vector<T>::Vector(int size) : size(size) {
if (size < 0) throw std::length_error("Vector constructor: negative size");
data = new T[size]{};
}
template <typename T>
Vector<T>::Vector(int size, T value) : size(size) {
if (size < 0) throw std::length_error("Vector constructor: negative size");
data = new T[size];
for (int i = 0; i < size; ++i) data[i] = value;
}
template <typename T>
Vector<T>::Vector(const Vector<T>& other) : size(other.size) {
data = new T[size];
for (int i = 0; i < size; ++i) data[i] = other.data[i];
}
template <typename T>
Vector<T>::~Vector() {
delete[] data;
}
template <typename T>
int Vector<T>::get_size() const {
return size;
}
template <typename T>
T& Vector<T>::at(int index) {
if (index < 0 || index >= size) throw std::out_of_range("Vector: index out of range");
return data[index];
}
template <typename T>
const T& Vector<T>::at(int index) const {
if (index < 0 || index >= size) throw std::out_of_range("Vector: index out of range");
return data[index];
}
template <typename T>
T& Vector<T>::operator[](int index) {
return at(index);
}
template <typename T>
const T& Vector<T>::operator[](int index) const {
return at(index);
}
template <typename U>
void output(const Vector<U>& v) {
for (int i = 0; i < v.size; ++i) {
std::cout << v.data[i] << " ";
}
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;
}
}
}
int main() {
cout << "测试1: 模板类接口测试\n";
test1();
cout << "\n测试2: 模板类异常处理测试\n";
test2();
}
运行结果:
实验任务5:
代码:
task5.cpp
查看代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
class Student {
public:
int id;
string name;
string major;
int score;
friend istream& operator>>(istream& in, Student& s);
friend ostream& operator<<(ostream& out, const Student& s);
};
istream& operator>>(istream& in, Student& s) {
in >> s.id >> s.name >> s.major >> s.score;
return in;
}
ostream& operator<<(ostream& out, const Student& s) {
out << left << setw(10) << s.id
<< setw(10) << s.name
<< setw(10) << s.major
<< fixed << setprecision(2) << s.score;
return out;
}
int main() {
vector<Student> students;
const string input_file = "data5.txt";
const string output_file = "ans5.txt";
ifstream infile(input_file);
ofstream outfile(output_file);
string header;
getline(infile, header);
Student temp;
while (infile >> temp) {
students.push_back(temp);
}
infile.close();
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
if (a.major == b.major) return a.score > b.score;
return a.major < b.major;
});
for (const auto& s : students) {
cout << s << endl;
for (const auto& s : students) {
outfile << s << endl;
}
outfile.close();
}
return 0;
}
运行结果:
标签:文件,const,index,int,Vector,实验,template,模板,size From: https://www.cnblogs.com/xiarihua/p/18612883