#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(); }
#include<iostream> #include<fstream> #include<array> #define N 5 int main() { using namespace std; array<int, 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"; }
#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.6; output(x1); Vector<int> x2(n,88); Vector<int> x3(x2); output(x2); output(x3); x2.at(0)=66; output(x2); x3[0]=666; output(x3); } int main(){ test(); }
#pragma once #include<iostream> using namespace std; template<typename T> class Vector{ public: Vector(int n):size{n}{ value=new T[n]; } Vector(int n,T v):size{n}{ value=new T[n]; for(int i=0;i<n;i++) value[i]=v; } Vector(const Vector<T> &a):size{a.size}{ value=new T[size]; for(int i=0;i<size;i++) value[i]=a.value[i]; } T &operator[](int i){ return value[i]; } int get_size() const{ return size; } T &at(int index){ return value[index]; } friend void output(const Vector<T> &a){ for(int i=0;i<a.size;i++) cout<<a.value[i]<<","; cout<<"\b\b\n"; } private: int size; T *value; };
#include<iostream> #include<string> #include<fstream> #include<iomanip> using namespace std; void output(ostream &out) { cout<<" "; out<<" "; for( int i=97;i<123;i++) { cout<<char(i)<<" "; out<<char(i)<<" "; } cout<<endl; out<<endl; int count=1; for(count=1;count<=26;count++) { cout<<setw(2)<<count<<" "; out<<setw(2)<<count<<" "; for(int i=65+count ,k=1;k<=26;i++,k++) {int temp=(i-65)%26+65; cout<<char(temp)<<" "; out<<char(temp)<<" "; } cout<<endl; out<<endl; } }
标签:size,int,namespace,Vector,实验,include,out From: https://www.cnblogs.com/djy769855989/p/16948863.html