我能夠運行程序,但沒有顯示任何東西。 如果我替換.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