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; } // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt 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<int, N> x; /*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(); // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元 for (auto i = 0; i < N; ++i) cout << x[i] << ", "; cout << "\b\b \n"; }
实验结果为:
array<int, N> x; ----> 修改成: array<char, N> x; 修改之后的实验结果为:出现该结果的主要原因为,修改之前array进行读取文件的时候,针对的是整型,从文本中就依次读取97 98 99 100 101
但在修改之后array在读取的时候,针对的是单个字符,其在内存中所占取的大小为一个字节,而文件中的为整数,而每个整数占取得是4个字节,在读取的会依次读取97 0 0 0 98,所以出现了上述的情况。
task4 Vector.h
#pragma once #include<iostream> using namespace std; template<typename T> class Vector { public: Vector(int n); Vector(int n, T value); Vector(const Vector& vp); ~Vector(); public: T& at(int index); T get_size(T p[]) { return sizeof(p); } friend void output(Vector& x){ for (auto i = 0; i < x.size; i++) cout << x.p[i] << ", "; cout << "\b\b \n"; } T& operator[](int index) { return p[index]; } private: int size; T* p; }; template<typename T> Vector<T>::Vector(int n) :size{ n } { p = new T[n]; } template<typename T> Vector<T>::Vector(int n, T value) :size{ n } { p = new T[n]; for (auto i = 0; i < n; i++) { p[i] = value; } } template<typename T> Vector<T>::Vector(const Vector& vp) :size{ vp.size } { p = new int[size]; for (auto i = 0; i < size; ++i) { p[i] = vp.p[i]; } } template <typename T> Vector<T>::~Vector() { delete[] p; } template<typename T> T& Vector<T>::at(int index) { return p[index]; }
task4.cpp
#include <iostream> #include "Vector.h" 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.8; output(x1); Vector<int> x2(n, 44); Vector<int> x3(x2); output(x2); output(x3); x2.at(0) = 88; output(x2); x3[0] = 888; output(x3); } int main() { test(); }
实验结果:
task 6
#include<iostream> #include<fstream> #include<iomanip> using namespace std; void output(ostream& out) { char a[27][27]; a[0][0] = ' '; for (int i = 1; i < 27; i++) { a[0][i] = 'a' + i - 1; a[i][0] = '0' + i; } int n = 0; for (int i = 1; i < 27; i++) { n = i; for (int j = 1; j < 27; j++) { if (65 + n > 90) { a[i][j] = 'A' + n - 26; } else { a[i][j] = 'A' + n; } n++; } } for (int i = 0; i < 27; i++) { if (i == 0) { cout <<setw(2) <<' '; } else { cout << setw(2) << a[i][0]-'0'; out << setw(2) << a[i][0]-'0'; } for (int j = 1; j < 27; j++) { cout << setw(2) << a[i][j]; out << setw(2) << a[i][j]; } cout << endl; out << endl; } } int main() { ofstream out; out.open("cipher_key.txt"); if (!out.is_open()) { cout << "fail to open data1.dat\n"; return 1; } output(out); out.close(); }
实验结果:
标签:OL,int,++,Vector,实验,output,include,模板,size From: https://www.cnblogs.com/lyjlyjlyj/p/16948934.html