Linux包括hash_map和hash_set的not declared问题

当在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;
}


原因:hash_map是声明在__gnu_cxx命名空间的,所以仅仅要在程序中加入using namespace __gnc_cxx就能够了^_^

#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;
}

原文地址:https://www.cnblogs.com/clnchanpin/p/7213984.html