using ElemType = function<void()>;
时间: 2024-04-16 16:28:19 浏览: 24
`ElemType`是一个类型别名,使用`function<void()>`作为其类型。
`function<void()>`表示一个可调用对象(函数、函数指针、lambda表达式等),该可调用对象没有参数并且没有返回值。
通过将`ElemType`定义为`function<void()>`,可以创建一个存储无参数无返回值函数的容器,可以将各种可调用对象(满足无参数无返回值的条件)存储到这个容器中,并在需要时进行调用。
例如,可以使用如下方式定义一个存储无参数无返回值函数的容器:
```cpp
using ElemType = function<void()>;
```
然后,可以使用`ElemType`类型的变量来存储函数、函数指针、lambda表达式等,并通过调用这些可调用对象来执行相应的操作,示例如下:
```cpp
ElemType func = []() {
// 执行一些操作
};
func(); // 调用存储的可调用对象,执行相关操作
```
通过使用`ElemType`类型别名,可以方便地定义和使用无参数无返回值的可调用对象。
相关问题
将以下程序补充完整 #include <iostream> #include <fstream> #include <iomanip> using namespace std; template <typename ElemType> class myArrayList { private: int mSize; int mLen; ElemType *mpArr; public: myArrayList(int n); myArrayList(ElemType *a,int n); void show(); ElemType getMax(); //以下函数由同学完成 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> & operator =(mymyArrayList<ElemType> &other) }; template <typename ElemType> myArrayList<ElemType>::myArrayList(int n) { this->mSize=n; this->mLen=0; this->mpArr=new ElemType[mSize]; } template <typename ElemType> myArrayList<ElemType>::myArrayList(ElemType *a,int n) { this->mSize=n; this->mLen=n; this->mpArr=new ElemType[mSize]; for(int i=0;i<mLen;i++) mpArr[i]=a[i]; } template <typename ElemType> void myArrayList<ElemType>::show() { for(int i=0;i<mLen;i++) cout<<setw(4)<<mpArr[i]; cout<<endl; } template <typename ElemType> ElemType myArrayList<ElemType>::getMax() { ElemType max; max=mpArr[0]; for(int i=1;i<mLen;i++) if(max<mpArr[i]) max=mpArr[i]; return max; } //Student.h class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friendbool operator <(Student &a1,Student &a2) { if(a1.height<a2.height) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl; return out; } }; //主程序 int main() { int a[]={1,2,3,5,7,9,12,8}; double b[]={1,2.5,3.6,5,7,9,12.8,8}; myArrayList <int> list1(a,8); list1.show(); cout<<"max="<<list1.getMax()<<endl; myArrayList <double> list2(b,8); list2.show(); cout<<"max="<<list2.getMax()<<endl; Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1; myArrayList <Student> list3(s,3); list3.show(); cout<<"max="<<list3.getMax()<<endl; } 说明: 将以下程序补充完整 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> operator =(mymyArrayList<ElemType> &other) //修改补充 Student类,要求按成绩排序(从高到低)并将完整的代码发出来
以下是补充完整的程序:
```
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
template <typename ElemType>
class myArrayList {
private:
int mSize;
int mLen;
ElemType *mpArr;
public:
myArrayList(int n);
myArrayList(ElemType *a,int n);
void show();
ElemType getMax();
void sort();
myArrayList(const myArrayList<ElemType> &other);
myArrayList<ElemType> & operator =(const myArrayList<ElemType> &other);
};
template <typename ElemType>
myArrayList<ElemType>::myArrayList(int n) {
this->mSize=n;
this->mLen=0;
this->mpArr=new ElemType[mSize];
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(ElemType *a,int n) {
this->mSize=n;
this->mLen=n;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=a[i];
}
template <typename ElemType>
void myArrayList<ElemType>::show() {
for(int i=0;i<mLen;i++)
cout<<setw(4)<<mpArr[i];
cout<<endl;
}
template <typename ElemType>
ElemType myArrayList<ElemType>::getMax() {
ElemType max;
max=mpArr[0];
for(int i=1;i<mLen;i++)
if(max<mpArr[i])
max=mpArr[i];
return max;
}
template <typename ElemType>
void myArrayList<ElemType>::sort() {
for(int i=0;i<mLen-1;i++) {
for(int j=0;j<mLen-i-1;j++) {
if(mpArr[j]<mpArr[j+1]) {
ElemType tmp=mpArr[j];
mpArr[j]=mpArr[j+1];
mpArr[j+1]=tmp;
}
}
}
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(const myArrayList<ElemType> &other) {
this->mSize=other.mSize;
this->mLen=other.mLen;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=other.mpArr[i];
}
template <typename ElemType>
myArrayList<ElemType> & myArrayList<ElemType>::operator =(const myArrayList<ElemType> &other) {
if(this==&other)
return *this;
delete []mpArr;
this->mSize=other.mSize;
this->mLen=other.mLen;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=other.mpArr[i];
return *this;
}
class Student {
private:
int mId;
float height;
int score;
public:
Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { }
friend bool operator <(Student &a1,Student &a2) {
if(a1.score<a2.score)
return true;
else
return false;
}
friend ostream &operator <<(ostream &out,Student &s) {
out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl;
return out;
}
};
int main() {
int a[]={1,2,3,5,7,9,12,8};
double b[]={1,2.5,3.6,5,7,9,12.8,8};
myArrayList <int> list1(a,8);
list1.show();
cout<<"max="<<list1.getMax()<<endl;
myArrayList <double> list2(b,8);
list2.show();
cout<<"max="<<list2.getMax()<<endl;
Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1;
myArrayList <Student> list3(s,3);
list3.show();
list3.sort();
cout<<"max="<<list3.getMax()<<endl;
list3.show();
return 0;
}
```
在这个程序中,我们添加了以下功能:
- `sort()`函数用于对数组进行排序
- 拷贝构造函数 `myArrayList(const myArrayList<ElemType> &other)`用于创建新的对象并将其初始化为与另一个对象相同的值
- 赋值运算符 `myArrayList<ElemType> & operator =(const myArrayList<ElemType> &other)`用于将一个对象的值赋给另一个对象
此外,我们还修改了`Student`类的实现,使其能够按照成绩从高到低进行排序。
template<class ElemType> BinaryTreeNode<ElemType>* BinaryTree<ElemType>::CreateBinaryTree(vector<ElemType> &x, ElemType &empty, int &n){ ElemType ch = x[n]; n++; if (ch == empty) { return NULL; } else { BinaryTreeNode<ElemType> *Node = new BinaryTreeNode<ElemType>; Node->data = ch; Node->LChild = CreateBinaryTree(x, empty, n); Node->RChild = CreateBinaryTree(x, empty, n); return Node; } }解读一下这个程序
这个程序是用来创建二叉树的,其函数名为CreateBinaryTree,接收三个参数,分别是存储二叉树节点数据的vector x,表示空节点的数据empty,以及当前处理的节点在序列中的下标n。
程序的实现主要采用了递归的方式来创建二叉树。具体来说,首先从vector x中取出下标为n的元素,如果这个元素等于empty,说明此时应该返回NULL表示空节点。否则,创建一个新的二叉树节点,并将这个元素赋值给节点的data成员。接着,递归调用CreateBinaryTree函数来创建节点的左子树和右子树,将返回的指针分别赋值给节点的LChild和RChild成员。最后返回创建好的节点指针。
需要注意的是,这个程序使用了模板类来实现,ElemType表示节点数据的类型,BinaryTreeNode是二叉树节点的类。此外,这个程序还是BinaryTree类的成员函数,因此需要在类定义中进行声明和实现。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)