map...need a tweak to this code

I had a previous post about 5 weeks ago with a problem and the problem was solved perfectly!! Here is the link to that post https://www.cplusplus.com/forum/beginner/277125/
I'm looking to take that result one step further. Here is the final code that worked.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

using codeType = int;
using Vec = vector<codeType>;
using Vec2D = vector<Vec>;
using PR = pair<codeType,int>;


bool sortOrder( PR a, PR b )
{
   if ( a.second == b.second ) return a.first < b.first;   // If equal frequencies, then in ascending order of maint code
   return a.second > b.second;                             // Otherwise, descending order of frequency
}


int main ()
{
   Vec2D mcvec { {2, 9, 12, 14}, 
                 {1, 7, 9, 16},
                 {2, 8, 10, 17},   //  Start Counting mcvec[2].begin()
                 {3, 7, 11, 15},
                 {1, 10, 14, 15},
                 {4, 5, 12, 17},
                 {1, 9, 15, 17}, 
                 {4, 11, 15, 16},
                 {2, 10, 12, 14},
                 {4, 7, 11, 12},    //  End Counting mcvec[9].end()
                 {2, 5, 11, 15} };

   // Generate frequency distribution
   map<codeType,int> freq;
   for ( codeType e = 1; e <= 17; e++ ) freq[e] = 0;
   for ( int row = 2; row < 10; row++ )
   {
      for ( auto e : mcvec[row] ) freq[e]++;
   }
   
   // Reassemble in linear data structure
   vector<PR> maintFreq( freq.begin(), freq.end() );
   cout << "Original counts:\n";
   for ( PR pr : maintFreq ) cout << pr.first << ": " << pr.second << '\n'; 
   
   // Sort
   cout << "\nSorted by frequency (descending), then maintenance code (ascending):\n";
   sort( maintFreq.begin(), maintFreq.end(), sortOrder );
   for ( PR pr : maintFreq ) cout << pr.first << ": " << pr.second << '\n';
}


Here is the line that I would like to tweak for a different result.
{
for ( auto e : mcvec[row] ) freq[e]++;
}
Instead of occupying the map named freq with every maint code in the row, I just want to use an index within the row. For example to look like this:
for ( auto e : mcvec[row][1] ) freq[e]++;
So it would only use the integer at position #2 for each row.

Its probably something easy that I haven't tried. Just can't figure it out.

You have:
1
2
3
4
for ( int row = 2; row < 10; row++ )
{
    for ( auto e : mcvec[row] ) freq[e]++;
}

Lets rewrite it with other syntax:
1
2
3
4
5
6
7
8
for ( int row = 2; row < 10; row++ )
{
    for ( int col = 0; col < mcvec[row].size(); ++col )
    {
        auto e = mcvec[row][col];
        freq[e]++;
    }
}

But you don't want all values from row, just one:
1
2
3
4
5
for ( int row = 2; row < 10; row++ )
{
        auto e = mcvec[row][1];
        freq[e]++;
}

Last edited on
Thanks Keskiverto!!! Works perfectly!

The rewrite with other syntax is easier to understand. I tried something similar, just forgot to remove the "for"

Thanks again!!
Brian
Topic archived. No new replies allowed.