I want to learn about mapping functions in c/c++ in general so this is a basic program on unordered mapping. I use unordered mapping because my input data are not sorted and I read that unordered_map is very efficient. Here I’ve an array with which I’m creating the hash table and use the lookup function to find if the elements in another array are in the hash table or not. I’ve several questions regarding this implementation:
#include <stdio.h>
#include <unordered_map>
using namespace std;
typedef std::unordered_map<int,int> Mymap;
int main()
{
int x,z,l=0;
int samplearray[5] = {0,6,4,3,8};
int testarray[10] = {6,3,8,67,78,54,64,74,22,77};
Mymap c1;
for ( x=0;x< sizeof(samplearray)/sizeof(int);x++)
c1.insert(Mymap::value_type(samplearray[x], x));
for ( z=0;z< sizeof(testarray)/sizeof(int);z++)
if((c1.find(testarray[z]) != c1.end()) == true)
l++;
printf("The number of elements equal are : %d\n",l);
printf("the size of samplearray and testarray are : %d\t%d\n",sizeof(samplearray)/sizeof(int),sizeof(testarray)/sizeof(int));
}
- First of all, is this a right way to
implement it? I’m getting the
answers right but seems that I use
too much of for loop. - This seems fairly okay with very small data but if I’m dealing with files of size > 500MB then this seems that, if I create a hash table for a 500MB file then the size of the hash table itself will be twice as much which is 1000MB. Is this always the case?
- What is the difference between std::unordered map and boost::unordered map?
Finally, a small request. I’m new to C/C++ so if you are giving suggestions like using some other typedef/libraries, I’d highly appreciate if you could use a small example or implement it on my code. Thanks
You’re starting off on the wrong foot. A
map(ordered or otherwise) is intended to store a key along with some associated data. In your case, you’re only storing a number (twice, as both the key and the data). For this situation, you want aset(again, ordered or otherwise) instead of a map.I’d also avoid at least the first
forloop, and usestd::copyinstead:If you only need to count how many items are common between the two sets, your for loop is fairly reasonable. If you need/want to actually know what items are common between them, you might want to consider using
std::set_intersection:Note that you don’t need to have an actual
setto useset_intersection— all you need is a sorted collection of items, so if you preferred to you could just sort your two arrays, then useset_intersectionon them directly. Likewise, the result could go in some other collection (e.g., avector) if you prefer.