How to get the smallest value group by string variable ?

Hi,

I'm new at c++, I've an output as follows got by looping over a vector of an objects vector<lable>,

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
int i = 0;
vector<Label> myVect;
for(auto & v: myVect)
{
    cout <<"label : "<< v[i].getLable() <<" result " <<  v[i].getValue() << endl;
    i++;
}

label : A07 result 0
label : A07 result 0
label : A07 result 0
label : A07 result 0
label : A07 result 0
label : A02 result 232
label : A02 result 232
label : A02 result 233
label : A02 result 234
label : A02 result 230
label : A02 result 233
label : A02 result 234
label : A02 result 229
label : A03 result 379
label : A03 result 377
label : A03 result 379
label : A03 result 381
label : A03 result 380
label : A03 result 377
label : A03 result 381
label : A03 result 372


and I need a help to get the small value grouped by another variable, whatever is type might be.

like so
1
2
3
label : A07 result 0
label : A02 result 229
label : A03 result 372
Last edited on
Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string labelText;
string colon;
string A_Value;
string resultText;
int number;

map<string, int> values;  // for storing the values we care about

while (readingInput) // or however you can fetch each line
{
  // input next line
  fileInput >> labelText >> colon>> A_Value>> resultText>> number;
  if (number> values[A_Value])
  {
     values[A_Value] = number;
   }
}


Now the map is a map of A values to numbers; it will look like this on the inside:

A07 0
A02 229
A03 372

You can fetch the values from the map.
Last edited on
@Repeater, in fact, I'm not using maps, is there a way to make this aggragation with vector<lable> ?
thank you by the way
Last edited on
Yes, but it's much harder.
@ysf007,

By the time I had finished debugging this ... you had changed the problem!

I, too, went for a solution with an associative container (map).
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

//=====================================================================

struct Label
{
   string code;
   int data;
};

bool sortByData( Label a, Label b )
{
   if ( a.data < b.data ) return true;
   if ( a.data == b.data && a.code < b.code ) return true;
   return false;
}

//=====================================================================

int main()
{
// ifstream in( "input.txt" );                        // Real input
   stringstream in( "label : A07 result 0   "      // Simulated input
                    "label : A07 result 0   "
                    "label : A07 result 0   "
                    "label : A07 result 0   "
                    "label : A07 result 0   "
                    "label : A02 result 232 "
                    "label : A02 result 232 "
                    "label : A02 result 233 "
                    "label : A02 result 234 "
                    "label : A02 result 230 "
                    "label : A02 result 233 "
                    "label : A02 result 234 "
                    "label : A02 result 229 "
                    "label : A03 result 379 "
                    "label : A03 result 377 "
                    "label : A03 result 379 "
                    "label : A03 result 381 "
                    "label : A03 result 380 "
                    "label : A03 result 377 "
                    "label : A03 result 381 "
                    "label : A03 result 372 " );


   // First put in an associative container to find the minimum for each code
   map<string,int> smallest;
   string dummy;
   char colon;
   string code;
   int data;
   while( in >> dummy >> colon >> code >> dummy >> data )
   {
      bool occurredBefore = smallest.count( code );
      if ( occurredBefore )
      {
         if ( data < smallest[code] ) smallest[code] = data;
      }
      else
      {
         smallest[code] = data;
      }
   }


   // Then put in a vector to sort by data
   vector<Label> V;
   for ( auto e : smallest ) V.push_back( { e.first, e.second } );
   sort( V.begin(), V.end(), sortByData );
   for ( Label L : V ) cout << L.code << '\t' << L.data << '\n';
}


A07	0
A02	229
A03	372
Last edited on
@lastchance thank you
Topic archived. No new replies allowed.