当在Linux下cpp文件包含hash_map或hash_set时,会出现"‘hash_map’ was not declared in this scope"问题。
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;
int main(void)
{
hash_map<int, string> hmap;
hmap[1] = "hi hdu1";
hmap[2] = "hi hdu2";
hmap[3] = "hi hdu3";
hash_map<int, string>::iterator iter;
iter = hmap.find(2);
if (iter != hmap.end())
{
cout << iter->second << endl;
}
else
{
cout << "not find" << endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;
using namespace __gnu_cxx;
int main(void)
{
hash_map<int, string> hmap;
hmap[1] = "hi hdu1";
hmap[2] = "hi hdu2";
hmap[3] = "hi hdu3";
hash_map<int, string>::iterator iter;
iter = hmap.find(2);
if (iter != hmap.end())
{
cout << iter->second << endl;
}
else
{
cout << "not find" << endl;
}
return 0;
}
本文介绍了解决在Linux环境下使用hash_map时遇到的编译错误问题。通过引入__gnu_cxx命名空间,成功避免了‘hash_map’wasnotdeclaredinthisscope错误,使程序能够正确编译并运行。
1087

被折叠的 条评论
为什么被折叠?



