Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3211114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:41:23+00:00 2026-05-17T14:41:23+00:00

I want to learn about mapping functions in c/c++ in general so this is

  • 0

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));
}
  1. 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.
  2. 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?
  3. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:41:24+00:00Added an answer on May 17, 2026 at 2:41 pm

    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 a set (again, ordered or otherwise) instead of a map.

    I’d also avoid at least the first for loop, and use std::copy instead:

    // There are better ways to do this, but it'll work for now:
    #define end(array) ((array) + (sizeof(array)/sizeof(array[0]))
    
    std::copy(samplearray, 
              end(samplearray), 
              std::inserter(Myset));
    

    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:

    std::set<int> myset, test_set, common;
    
    std::copy(samplearray, end(samplearray), std::inserter(myset));
    std::copy(testarray, end(testarray), std::inserter(test_set));
    
    std::set_intersection(myset.begin(), myset.end(), 
                          test_set.begin(), test_set.end(), 
                          std::inserter(common));
    
    // show the common elements (including a count):
    std::cout <<common.size() << " common elements:\t";
    std::copy(common.begin(), common.end(), 
              std::ostream_iterator<int>(std::cout, "\t");
    

    Note that you don’t need to have an actual set to use set_intersection — all you need is a sorted collection of items, so if you preferred to you could just sort your two arrays, then use set_intersection on them directly. Likewise, the result could go in some other collection (e.g., a vector) if you prefer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to learn about how to use sub reports in RDLC in ASP.Net.
I want to use Flash to write games. I don't want to learn about
I currently learning about scrum and want to learn from experienced professionals in the
I want to go backwards and learn more about how compilers, processors and memory
I want to learn how to use Struts 2 and I created a simple
I'm not a graphics person, but I want to learn silverlight development. Which expression
I want to see a basic project to learn how to organize membership and
I want to learn about the nuts-and-bolts of using SSL to secure client/server communication.
I want to learn about compilers and some optimization techniques, and I thought it
I want to learn about debian source packaging.Is there any good tutorial for that.Please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.