我能够运行程序,但没有显示任何东西。 如果我替换.find("1")
我得到编译器错误,因为const char不能更改为const int。 如果我用.find('1')
替换,那么我得到的输出为“字符串不在地图中”。 我需要检索键值为1的字符串。我应该如何修改我的程序以获得所需的结果。从地图检索字符串的逻辑错误-C++
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
typedef map<int,string> EventTypeMap;
EventTypeMap EventType;
EventType[1]="beata";
EventType[2]="jane";
EventType[3]="declan";
if(EventType.find(1)==EventType.end())
{
cout<<"string is not in the map!"<<endl;
}
return 0;
}
我试着用你告诉我的并且得到Debug Assertion失败!错误。 – Angus 2011-04-24 10:18:24
@Beata你使用什么编译器?我用GCC 4.5试了一下,一切都很好。通过'#include“stdafx.h”'我想它是Visual C++,但是什么版本? – Archie 2011-04-24 10:20:33
我不知道如何查看我使用的编译器版本...获取此信息Microsoft Visual Studio 2008版本9.0.21022.8 RTM Microsoft .NET Framework版本3.5安装版:VC Express Microsoft Visual C++ 2008 91909-152- 0000052-60784 Microsoft Visual C++ 2008 – Angus 2011-04-24 10:28:58