#include <vector>
#include <map>
#include <string>
#include <cfloat>
#include <iostream>
using namespace std;


typedef vector<float> componenttype;
typedef vector<float> datatype;
typedef map<string, int> possiblenames;
typedef vector<pair<componenttype, string>> resulttype;

float vecdistance(datatype v1, componenttype v2) {return 1.0;}

resulttype user995434(vector<datatype> data, vector<string> labels, vector<componenttype> components) {
    map<componenttype, possiblenames> maybenames;
    resulttype resultnames;
    
    //for each data d from data
    for(auto d=data.begin(); d!=data.end(); ++d) {
       //let c the nearest component from d according to distance.
       auto closest=components.begin();
       float closedistance = FLT_MAX;
       for(auto it=components.begin(); it!=components.end(); ++it) {
           float dist = vecdistance(*d, *it);
           if (dist < closedistance) {
               closedistance = dist; 
               closest = it;
           }
        }
        //associate the label of d to c.
        int offset = std::distance(data.begin(), d);
        maybenames[*closest][labels[offset]]++;
    }
    //for each component c
    for(auto c=components.begin(); c!=components.end(); ++c) {
        //let mostname be the name with the most matches.
        auto posnames = maybenames[*c];
        posnames[""]=0; //guarantee each component has _something_
        auto mostname = posnames.begin();
        for(auto it=posnames.begin(); it!=posnames.end(); ++it) {
            if (it->second > mostname->second)
                mostname = it;
        }
        //associate mostname with c
        resultnames.push_back(make_pair(*c, mostname->first));
    }
    return resultnames;
}

int main() {
    vector<componenttype> components(1, vector<float>(1, 1.5f));
    vector<datatype> data(1, vector<float>(1, 1.5f));
    vector<string> labels(1, string("APPLE"));
    resulttype names = user995434(data, labels, components);
    for(auto it=names.begin(); it!=names.end(); ++it) {
        std::cout << "{";
        for(auto it2=it->first.begin(); it2!=it->first.end(); ++it2)
            std::cout << *it2 << ",";
        std::cout << "} = " << it->second << '\n';
    }
    return 0;
}