注册 登录
编程论坛 数据结构与算法

求解释

刘潘敏 发布于 2012-10-24 23:42, 438 次点击
#include<iostream>
#include<string>
using namespace std;
const int StackSize=10;
typedef struct
{
 int notask; char name[10];}DataType;
class SeqStack
{
public:
    SeqStack( ) {top = 0;}
    ~SeqStack( ) { }     
    void Push( DataType x )     
    {
    if (top== StackSize-1) throw "溢出";
        top++;
        data[top] = x;   
}
    DataType Pop( )   
    {    if (top==-1) throw "溢出";
        DataType x=data[top--];
        return x;}
    DataType  data[StackSize];  
    int top;           
};
int main()
{    SeqStack a;
    DataType b[5];
    for(int i = 1 ; i <= 5; i++)
    {cin>> b[i].name;
        b[i].notask = i;    }
    for( i = 1 ; i <= 5; i++)
    {    a.Push(b[i]);}
    for( i = 1 ;i  <= 5; i++)
    {DataType k ;
        k = a.Pop();
        cout<< k.name <<"  "<<k.notask << endl;}}
看不懂typedef struct
{
 int notask; char name[10];}DataType;
作用是什么,c++中typedef 作用是什么


2 回复
#2
hgand2012-10-25 09:24
那是定义一个结构体类型DataType,然后DataType就可以像int,char一样去定义变量,DataType x,x就是一个拥有两个结构体成员(int notask; char name[10];)的结构体变量,DataType x[10];结构体数组,那x[0]-x[9]都拥有两个结构体成员,调用的话,就直接是例如
x[0].notask,x[0].name 等。所以是说typedef 就是定义一个新的类型!
#3
刘潘敏2012-10-25 23:03
哦哦那有什么好处啊
1