fork download
  1. #include <vector>
  2. #include <map>
  3. #include <string>
  4. #include <cfloat>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. typedef vector<float> componenttype;
  10. typedef vector<float> datatype;
  11. typedef map<string, int> possiblenames;
  12. typedef vector<pair<componenttype, string>> resulttype;
  13.  
  14. float vecdistance(datatype v1, componenttype v2) {return 1.0;}
  15.  
  16. resulttype user995434(vector<datatype> data, vector<string> labels, vector<componenttype> components) {
  17. map<componenttype, possiblenames> maybenames;
  18. resulttype resultnames;
  19.  
  20. //for each data d from data
  21. for(auto d=data.begin(); d!=data.end(); ++d) {
  22. //let c the nearest component from d according to distance.
  23. auto closest=components.begin();
  24. float closedistance = FLT_MAX;
  25. for(auto it=components.begin(); it!=components.end(); ++it) {
  26. float dist = vecdistance(*d, *it);
  27. if (dist < closedistance) {
  28. closedistance = dist;
  29. closest = it;
  30. }
  31. }
  32. //associate the label of d to c.
  33. int offset = std::distance(data.begin(), d);
  34. maybenames[*closest][labels[offset]]++;
  35. }
  36. //for each component c
  37. for(auto c=components.begin(); c!=components.end(); ++c) {
  38. //let mostname be the name with the most matches.
  39. auto posnames = maybenames[*c];
  40. posnames[""]=0; //guarantee each component has _something_
  41. auto mostname = posnames.begin();
  42. for(auto it=posnames.begin(); it!=posnames.end(); ++it) {
  43. if (it->second > mostname->second)
  44. mostname = it;
  45. }
  46. //associate mostname with c
  47. resultnames.push_back(make_pair(*c, mostname->first));
  48. }
  49. return resultnames;
  50. }
  51.  
  52. int main() {
  53. vector<componenttype> components(1, vector<float>(1, 1.5f));
  54. vector<datatype> data(1, vector<float>(1, 1.5f));
  55. vector<string> labels(1, string("APPLE"));
  56. resulttype names = user995434(data, labels, components);
  57. for(auto it=names.begin(); it!=names.end(); ++it) {
  58. std::cout << "{";
  59. for(auto it2=it->first.begin(); it2!=it->first.end(); ++it2)
  60. std::cout << *it2 << ",";
  61. std::cout << "} = " << it->second << '\n';
  62. }
  63. return 0;
  64. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
{1.5,} = APPLE