myvector.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:
C#编程
开发平台:
Visual C++
- //=====================================
- // myvector.cpp
- //=====================================
- #include"myvector.h"
- #include<iostream>
- using namespace std;
- //-------------------------------------
- void Vector::set(int s){
- sz = s;
- if(s<=0){
- cerr<<"bad Vector size.n";
- exit(1);
- }
- v = new int[s];
- }//------------------------------------
- int& Vector::operator[](int i){ //引用返回的目的是返回值可以作左值
- if(i<0 || i>=sz){
- cerr <<"Vector index out of range.n";
- exit(1);
- }
- return v[i];
- }//------------------------------------
- void Vector::display(){
- for(int i=0; i<sz; ++i)
- cout<<v[i]<<" ";
- cout<<"n";
- }//------------------------------------