数据结构(二)collection、OrderedCollection、Rectangle、matrix
程序员文章站
2022-05-21 09:46:19
...
1、Collection类模板
代码:
/*
Collection类模板
*/
#include <iostream>
#include <vector>
#include<stdlib.h>
using namespace std;
template <typename Object>
class Collection
{
private:
vector<Object> vs;
public:
Collection()
{
vs.reserve(10);
}
int Get_Size() const
{
return vs.size();
}
void insert(const Object& temp)
{
vs.push_back(temp);
}
bool isEmpty() const
{
return Get_Size() == 0;
}
void makeEmpty()
{
vs.clear();
}
void remove(int index)
{
if (isEmpty())
{
cout << "Vector is empty, so it can't be removed." << endl;
exit(1);
}
vs.erase(vs.begin() + index);
}
Object& operator [](int index)
{
return vs[index];
}
const Object& operator [](int index) const
{
return vs[index];
}
int contains(const Object& temp)
{
for(int i=0;i<vs.size();i++)
{
if(temp==vs[i])
{
cout<<"x位于当前序列中"<<endl;
return 1;
}
}
cout<<"x不位于当前序列中"<<endl;
return 0;
}
};
int main()
{
Collection<int> cell;
cell.insert(2);
cell.insert(1);
cell.insert(5);
cell.insert(10);
int x;
cin>>x;
cell.contains(x);
cell.makeEmpty();
cell.remove(0);
cout << cell.isEmpty() << endl;
return 0;
}
运行:
2、OrderedCollection模板类
代码:
/*
OrderedCollection模板类
*/
#include <iostream>
#include <vector>
#include<stdlib.h>
using namespace std;
template <typename Comparable>
class OrderedCollection
{
private:
vector<Comparable> vs;
public:
OrderedCollection()
{
vs.reserve(10);
}
int Get_Size() const
{
return vs.size();
}
void insert(const Comparable& temp)
{
vs.push_back(temp);
}
bool isEmpty() const
{
return Get_Size() == 0;
}
void makeEmpty()
{
vs.clear();
}
void remove(int index)
{
if (isEmpty())
{
cout << "Vector is empty, so it can't be removed." << endl;
exit(1);
}
vs.erase(vs.begin() + index);
}
Comparable& operator [](int index)
{
return vs[index];
}
const Comparable& operator [](int index) const
{
return vs[index];
}
const Comparable& findMin() const
{
if (isEmpty())
{
cout << "Vector is empty, so it can't be compared." << endl;
exit(1);
}
int index = 0;
for(unsigned int i = 1; i < vs.size(); i++)
{
if (vs[i] < vs[index])
{
index = i;
}
}
return vs[index];
}
const Comparable& findMax() const
{
if (isEmpty())
{
cerr << "Vector is empty, so it can't be compared." << endl;
exit(1);
}
int index = 0;
for (unsigned int i = 1; i < vs.size(); i++)
{
if (vs[i] > vs[index])
{
index = i;
}
}
return vs[index];
}
};
int main()
{
OrderedCollection<int> cell;
cell.insert(2);
cell.insert(1);
cell.insert(5);
cell.insert(10);
cout << cell.findMin() << endl;
cout << cell.findMax() << endl;
cell.makeEmpty();
cell.remove(0);
cout << cell.isEmpty() << endl;
cout << cell.findMax() << endl;
return 0;
}
运行:
3、使用 Rectangle类
代码:
/*
使用 Rectangle类
*/
#include <iostream>
#include <vector>
using namespace std;
/**
* Return the maximum item in array a.
* Assumes a.size( ) > 0.
* Comparable objects must provide operator< and operator=
*/
template <typename Comparable>
const Comparable & findMax(const vector<Comparable> & a)
{
int maxIndex = 0;
for (int i = 1; i < a.size(); ++i)
if (a[maxIndex] < a[i])
maxIndex = i;
return a[maxIndex];
}
class Rectangle
{
public:
explicit Rectangle(double l=0.0,double w=0.0) : length{l},width{w}
{ }
double getLength() const
{
return length;
}
double getWidth() const
{
return width;
}
double getArea() const
{
return length*length;
}
double getPerimeter() const
{
return 2*(length+width);
}
void print(ostream & out = cout) const
{
out << "(Rectangle " <<"长"<< getLength()<<","<<"宽"<<getWidth() << ")";
}
bool operator< (const Rectangle & rhs) const
{
if(getArea()<rhs.getArea())
return getArea()<rhs.getArea();
else
if(getPerimeter()<getPerimeter())
return getPerimeter()<getPerimeter();
}
private:
double length,width;
};
// Define an output operator for Rectangle
ostream & operator<< (ostream & out, const Rectangle & rhs)
{
rhs.print(out);
return out;
}
int main()
{
vector<Rectangle>v(3);
v[0]=Rectangle{ 3.0,2.0 };
v[1]=Rectangle{ 2.5,2.5 };
v[2]=Rectangle{ 2.3,2.7 };
cout << "Largest Rectangle: " << findMax(v) << endl;
return 0;
}
运行:
4、完善matrix类模板
代码:
/*
完善matrix类模板
*/
#include <iostream>
#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
using namespace std;
template <typename Object>
class matrix
{
public:
//无参构造函数,将array初始化为 2*2的matrix
matrix()
{
array.resize(2);
for(vector<Object> & thisRow : array)
thisRow.resize(2);
}
matrix(int rows, int cols) : array(rows)
{
for (auto & thisRow : array)
thisRow.resize(cols);
}
matrix(vector<vector <Object> > v) : array{ v }{}
matrix(vector<vector <Object> > && v) : array{ std::move(v) }{ }
//增加的resize方法
void resize(int rows,int cols)
{
array.resize(rows);
for(vector<Object> & thisRow : array)
{
thisRow.resize(cols);
}
}
const vector<Object> & operator[](int row) const
{
return array[row];
}
vector<Object> & operator[](int row)
{
return array[row];
}
int numrows() const
{
return array.size();
}
int numcols() const
{
return numrows() ? array[0].size() : 0;
}
private:
vector<vector<Object>> array;
};
#endif
int main()
{
matrix<char> a(3,2);
cout<<a.numrows()<<' '<<a.numcols()<<endl;
a.resize(4,2);
cout<<a.numrows()<<' '<<a.numcols()<<endl;
matrix<int> b;
cout<<b.numrows()<<' '<<b.numcols()<<endl;
return 0;
}
运行: