实验任务3
task3_1.cpp
1 #include<iostream> 2 #include<vector> 3 template<typename T> 4 void output(const T&obj){ 5 for(auto &item:obj) 6 std::cout<<item<<", "; 7 std::cout<<"\b\b \n"; 8 } 9 int main() 10 { 11 using namespace std; 12 vector<int> v1; 13 vector<int> v2(5); 14 vector<int> v3(5,42); 15 vector<int> v4{1,9,8,4}; 16 vector<int> v5{v4}; 17 output(v2); 18 output(v3); 19 output(v4); 20 output(v5); 21 }
task3_2.cpp
1 #include<iostream> 2 #include<fstream> 3 #include<array> 4 #define N 5 5 int main(){ 6 using namespace std; 7 array<int,N> x; 8 ifstream in; 9 in.open("data1.dat",ios::binary); 10 if(!in.is_open()){ 11 cout<<"fail to open data1.dat\n"; 12 return 1; 13 } 14 in.read(reinterpret_cast<char *>(&x),sizeof(x)); 15 in.close(); 16 17 for(auto i=0;i<N;++i) 18 cout<<x[i]<<", "; 19 cout<<"\b\b \n"; 20 }
修改之后的运行结果为:
char类型一次只读取一个字节长度,int 类型占四个字符的长度,所以改为char后b会在第五个出现。
实验任务4
Vector.hpp
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 template<typename T> 5 class Vector{ 6 public: 7 Vector(int n); 8 Vector(int n,T value); 9 Vector(const Vector<T> &x); 10 ~Vector(); 11 int get_size(){return size;} 12 T &at(int i){return p[i];} 13 T &operator[](int i){return p[i];} 14 friend void output(Vector &x){ 15 for(int i=0;i<x.size-1;i++) 16 cout<<x.p[i]<<", "; 17 cout<<x.p[x.size-1]<<endl; 18 } 19 private: 20 int size; 21 T *p; 22 }; 23 template<typename T> 24 Vector<T>::Vector(int n){ 25 size=n; 26 p=new T[n]; 27 } 28 template<typename T> 29 Vector<T>::Vector(int n,T value){ 30 size=n; 31 p=new T[n]; 32 for(int i=0;i<n;i++) 33 p[i]=value; 34 } 35 template<typename T> 36 Vector<T>::Vector(const Vector<T> &x) 37 { 38 size=x.size; 39 p=new T[size]; 40 for(int i=0;i<x.size;i++) 41 { 42 p[i]=x.p[i]; 43 } 44 } 45 template<typename T> 46 Vector<T>::~Vector() 47 { 48 delete [] p; 49 }
task4.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 void test() { 4 using namespace std; 5 int n; 6 cin >> n; 7 Vector<double> x1(n); 8 for(auto i = 0; i < n; ++i) 9 x1.at(i) = i * 0.7; 10 output(x1); 11 Vector<int> x2(n, 42); 12 Vector<int> x3(x2); 13 output(x2); 14 output(x3); 15 x2.at(0) = 77; 16 output(x2); 17 x3[0] = 999; 18 output(x3); 19 } 20 int main() { 21 test(); 22 }
运行结果:
实验任务5
1 #include<iostream> 2 #include<iomanip> 3 #include<string> 4 #include<fstream> 5 using namespace std; 6 void output(std::ostream &out) 7 { 8 char a[26][26]; 9 cout<<" "; 10 out<<" "; 11 for(char i='a';i<='z';i++) 12 { 13 cout<<setw(2)<<setfill(' ')<<i; 14 out<<setw(2)<<setfill(' ')<<i; 15 } 16 cout<<endl; 17 out<<endl; 18 for(int j=0;j<26;j++) 19 { 20 cout<<setw(2)<<setfill(' ')<<j+1; 21 out<<setw(2)<<setfill(' ')<<j+1; 22 for(int k=0;k<26;k++) 23 { 24 a[j][k]='A'+char((j+k+1)%26); 25 cout<<setw(2)<<setfill(' ')<<a[j][k]; 26 out<<setw(2)<<setfill(' ')<<a[j][k]; 27 } 28 out<<endl; 29 cout<<endl; 30 } 31 } 32 int main() 33 { 34 ofstream out; 35 out.open("cipher_key.txt",ios::out); 36 if(!out.is_open()) 37 { 38 cout<<"fail to open cipher_ke.text"<<endl; 39 return 1; 40 } 41 output(out); 42 out.close(); 43 }
标签:std,文件,int,Vector,实验,output,include,模板,size From: https://www.cnblogs.com/cwj202183290470/p/16940172.html