task3_1:
#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:
#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"; }
//array<int, 5> 共有5*4=20个字节,in.read(reinterpret_cast<char *>(&x), sizeof(x)); 可以读出20个字节
//array<char, 5>共有5个字节, in.read(reinterpret_cast<char *>(&x), sizeof(x)); 可以读出5个字节
//但out.write(reinterpret_cast<char *>(&x), sizeof(x)); 都是写入20个字节,其中修改后为'97',0,0,0,'98',0,0,0,等
//因此在读的时候只能读出前五个字节 '97'对应a,0对应空,结果为 a, , , , b
task4:
Vector.hpp
#include<iostream> using namespace std; template<typename T> class Vector { public: Vector(int n1); Vector(int n1, T x); Vector(const Vector<T> &y); ~Vector(); int get_size() const { return n; } T& at(int i); T& operator[](int i); template<typename T1> friend void output(const Vector<T1> &x); private: int n; T *p; }; template<typename T> T& Vector<T>::operator[](int i) { return p[i]; } template<typename T> T& Vector<T>::at(int i) { return p[i]; } template <typename T> Vector<T>::Vector(int n1) { n = n1; p = new T[n]; } template <typename T> Vector<T>::Vector(int n1, T x) { n = n1; p = new int[n]; for(int i = 0; i < n1; i++) { p[i] = x; } } template <typename T> Vector<T>::Vector(const Vector<T> &y) { n = y.n; p = new int[n]; for(int i = 0; i < n; i++) { p[i] = y.p[i]; } } template <typename T> Vector<T>::~Vector() { delete[] p; cout << "destructor called" << endl; } template<typename T1> void output(const Vector<T1> &x) { for(int i = 0; i < x.n; i++) { cout << x.p[i] << " "; } cout << 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(); }
task5.cpp
#include<iostream> #include<fstream> #include<iomanip> using namespace std; void output(ostream& out) { out << " "; for (int i = 97; i < 123; i++) { out << setw(2) << char(i); } out << endl; for (int i = 1; i <= 26; i++) { out << setw(2) << i; for (int j = i; j < i + 26; j++) { out << setw(2) << char(65 + j % 26); } out << endl; } } int main() { ofstream out; output(cout); out.open("cipher.txt"); if (!out.is_open()) { cout << "fail to open" << endl; return 0; } output(out); out.close(); return 0; }
标签:int,Vector,实验,template,output,include,out From: https://www.cnblogs.com/miantiaooooo/p/16960345.html