Are there hashes in C++, or something similar?

Hi,

I'm practicing with arrays and I was wondering if there is something that in Ruby is called a hash, where you can basically add the name and the value to something. The reason for this question is because I'm trying to relate values from two different arrays.

For instance the following program asks for the monthly rainfall for a complete year (this example only shows 4), then it sort the inputs out and finally sums all months and show the total. The problem is that as soon as the array holding the inputs is sorted the months are no longer related to its corresponding values.

Is there a way to relate the input value array with the months arrays?

Is there a better way to simplify this in C++?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()// start main
{
    const int MONTHS = 4;
    string monthName[MONTHS] = {"January","February", "March","April"};
    double monthArrey[MONTHS];    
    double total = 0;
    
    cout<< "Enter rainfall for each month: " << endl;
    cout<< endl;
    
    for(int i = 0; i < MONTHS; i++ )
    {       
        cout<< "Enter info for the month of " << monthName[i]  << endl;
        cin >> monthArrey[i];
    }
    
    
    cout<< "Rainfall numbers: " << endl;
    int j =0;
    while (j<MONTHS) {
        total += monthArrey[j];
        j++;
        
    }
    
    sort(monthArrey, monthArrey+MONTHS);    
    for (int i = 0; i < MONTHS; i++ ) 
    {        
        cout << monthArrey[i] << " " << monthName[i] << endl;        
    }
    
    cout << "Total rainfall for the year: "<< total <<" ";    
    
    return 0;
    
} // end main 


Thanks a lot for your help, learning a lot.
You can always implement your own hash table. Just read all your keys as use all your keys as ints and us a modulus as your hash function.
Hmm, it sounds a little complicted or at least I dont think I have the knowladge to create that myself.

Thanks a lot
No need, there already is std::map and std::unordered_map.
Topic archived. No new replies allowed.