Wow, you are right. The API of both g++ and vc++ is same; just have to insert the required headers. I have the same program working for both g++ and vc++ 2005 right now.
Here is the code:
//VC++
#include<iostream>
#include<cstdio>
#include<hash_map>
using namespace std;
using namespace stdext;
int main(int argc,char** argv)
{
hash_map<int,int> mapping;
hash_map<int,int>::const_iterator miter;
mapping.insert(pair<int,int>(1,100));
mapping.insert(pair<int,int>(2,200));
miter = mapping.find(1);
if(miter == mapping.end())
cout << "Not found";
else
cout << miter->first << " = " << miter->second;
getchar();
return 0;
}
//VC++
#include<iostream>
#include<cstdio>
#include<hash_map>
using namespace std;
using namespace stdext;
int main(int argc,char** argv)
{
hash_map<int,int> mapping;
hash_map<int,int>::const_iterator miter;
mapping.insert(pair<int,int>(1,100));
mapping.insert(pair<int,int>(2,200));
miter = mapping.find(1);
if(miter == mapping.end())
cout << "Not found";
else
cout << miter->first << " = " << miter->second;
getchar();
return 0;
}
To copy to clipboard, switch view to plain text mode
//g++
#include<iostream>
#include<cstdio>
#include<ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
int main(int argc,char** argv)
{
hash_map<int,int> mapping;
hash_map<int,int>::const_iterator miter;
mapping.insert(pair<int,int>(1,100));
mapping.insert(pair<int,int>(2,200));
miter = mapping.find(1);
if(miter == mapping.end())
cout << "Not found";
else
cout << miter->first << " = " << miter->second;
getchar();
return 0;
}
//g++
#include<iostream>
#include<cstdio>
#include<ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
int main(int argc,char** argv)
{
hash_map<int,int> mapping;
hash_map<int,int>::const_iterator miter;
mapping.insert(pair<int,int>(1,100));
mapping.insert(pair<int,int>(2,200));
miter = mapping.find(1);
if(miter == mapping.end())
cout << "Not found";
else
cout << miter->first << " = " << miter->second;
getchar();
return 0;
}
To copy to clipboard, switch view to plain text mode
However I am running into some problem with hash_map<string,string> for both the compilers. I am sure there is some compare function that needs to be defined for strings or is there something already available ?
Bookmarks