赋给c++中对象类项的枚举值

Enum values assigned to the object class entries in c++

本文关键字:枚举 c++ 对象 赋给      更新时间:2023-10-16
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace System;
using namespace std;
class gate
{
public:
    enum mod {single=12,local,global} type;
}gatearry[9];

通过重复赋值枚举值

gatearry[1].type=gate::global;
gatearry[2].type=gate::single;

代替上面的代码。

分配问题枚举值到对象类条目。

困难在于在main中使用cin

for(i=0;i<9;i++)
{
    cin>>gatearry[i].type;
}

"此代码是用Visual Studio 10编写的"

目标:

减少循环中的命令的分配对象类

的枚举元素

或类似且更紧凑的代码,例如循环中的命令

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace System;
using namespace std;
class gate
{
    public:
        enum mod {single,local,global} type;
}gatearry[9];
int main()
{
    int i,x=0;
    string d;
    for(i=0;i<9;i++)
    {
        cout<<"n input gate type"<<i<<"n";
        cin>>d;
        if (d=="single")
            gatearry[i].type=gate::single;
        else if(d=="local")
            gatearry[i].type=gate::local;
        else if(d=="global")
            gatearry[i].type=gate::global;
        if(gatearry[i].type==gate::global)
            x++;
        cout<<"n"<<gatearry[i].type;
    }
    cout<<"n"<<x;
    return 0;
}