C++里怎样用map将枚举类转换到string类
#include<stdio.h>#include<stdlib.h>#include<string>#include<map>#include<iostream>usi...
#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main (void)
{
enum Name{AMY,JOHN,JOY};
map<Name, string> nameMap;
nameMap[AMY] = "AMY";
nameMap[JOHN] = "JOHN";
nameMap[JOY] = "JOY";Name salesname=JOHN;
string nameStr = nameMap[salesname];
cout<<nameStr<<endl;}这里的error怎样解决? 展开
#include<stdlib.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main (void)
{
enum Name{AMY,JOHN,JOY};
map<Name, string> nameMap;
nameMap[AMY] = "AMY";
nameMap[JOHN] = "JOHN";
nameMap[JOY] = "JOY";Name salesname=JOHN;
string nameStr = nameMap[salesname];
cout<<nameStr<<endl;}这里的error怎样解决? 展开
4个回答
2013-07-28
展开全部
将map<Name, string> nameMap;改成map<int, string> nameMap; 改后为:#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main ()
{
enum Name{AMY,JOHN,JOY};
map<int, string> nameMap;
nameMap[AMY] = "AMY";
nameMap[JOHN] = "JOHN";
nameMap[JOY] = "JOY"; Name salesname=JOHN;
string nameStr = nameMap[salesname];
cout<<nameStr<<endl;
return 0;}
#include<stdlib.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main ()
{
enum Name{AMY,JOHN,JOY};
map<int, string> nameMap;
nameMap[AMY] = "AMY";
nameMap[JOHN] = "JOHN";
nameMap[JOY] = "JOY"; Name salesname=JOHN;
string nameStr = nameMap[salesname];
cout<<nameStr<<endl;
return 0;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-28
展开全部
#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;typedef enum {
AMY,
JOHN,
JOY
}Name;int main (void)
{map<Name, string> nameMap;
nameMap[AMY] = "AMY";
nameMap[JOHN] = "JOHN";
nameMap[JOY] = "JOY";Name salesname=JOHN;
string nameStr = nameMap[salesname];
cout<<nameStr<<endl;} 会出现很多警告,不要管他,直接运行就可以了,我用得是vc++6.0
#include<stdlib.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;typedef enum {
AMY,
JOHN,
JOY
}Name;int main (void)
{map<Name, string> nameMap;
nameMap[AMY] = "AMY";
nameMap[JOHN] = "JOHN";
nameMap[JOY] = "JOY";Name salesname=JOHN;
string nameStr = nameMap[salesname];
cout<<nameStr<<endl;} 会出现很多警告,不要管他,直接运行就可以了,我用得是vc++6.0
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
反过来,把字符串转成enum值可以这样:
enum Graph { cicle=1 , rectangle, square}graph;
string cgraph;
map<string, Graph> namegraph;
namegraph["cicle"] = cicle;
namegraph["rectangle"] = rectangle;
namegraph["square"] = square;
cout << "please input the type of graph:(cicle/rectangle/square)";
cin >> cgraph;
switch (namegraph[cgraph]) {
case 1: ·····
enum Graph { cicle=1 , rectangle, square}graph;
string cgraph;
map<string, Graph> namegraph;
namegraph["cicle"] = cicle;
namegraph["rectangle"] = rectangle;
namegraph["square"] = square;
cout << "please input the type of graph:(cicle/rectangle/square)";
cin >> cgraph;
switch (namegraph[cgraph]) {
case 1: ·····
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-28
展开全部
这里没有看到error
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询