0

For example I have a std::vector<char *> filled with image filenames. Another std::vector filled with float values that correspond by index to the vector with filenames. I am sorting the float values and returning the smallest 5 floats. I need to know which images those 5 floats are.

Below is the code I have so far. It is a sum square difference fxn for comparing images.

// sum squared difference
int ssd(std::vector<float> &target_data, std::vector<char *> dir_filenames, std::vector<std::vector<float>> &dir_fvec, char *num_matches) {
    
    // initialize variables
    double target_sum;
    float dir_sum;
    float diff;
    float ssd = 0;
    std::vector<float> ssd_values;
    std::vector<float> dir_sum_values;
    std::vector<float> diff_values;
    
    

    // compute sum of target image features
    target_sum = std::accumulate(target_data.begin(), target_data.end(), 0);


    // ssd computation
    for( auto& n : dir_fvec ) { 
        // compute sum of each dir image's features
        dir_sum = std::accumulate(n.begin(), n.end(), 0);
        dir_sum_values.push_back(dir_sum);
            
        // difference between dir image features and target image features
        diff = dir_sum - target_sum;
        diff_values.push_back(diff);

        // sum square difference 
        ssd = diff * diff;
        
        ssd_values.push_back(ssd);
        
        
    }

    // sort ssd values from min to max
    sort(ssd_values.begin(), ssd_values.end());
    
    // return N matches
    int N = atoi(num_matches); // convert argv[6] (N number of matches) from char to integer
    ssd_values.resize(N);

    for( auto& n : ssd_values) {
        std::cout << std::fixed << n << std::endl;
    }
    

    return 0;
}
  • I realize this is a school assignment so you're bound to a certain API, but I find it hilarious that you're allowed to use `std::vector`, but you can't use `std::string`, having instead a vector of C-strings. – JohnFilleau Feb 22 '22 at 00:23
  • What specific problem are you having with this? Please read and heed [example] and [ask]. That will help you improve your question so I don't have to ask 20 questions to get the info out of you. – JohnFilleau Feb 22 '22 at 00:24
  • Now may be a good time to get acquainted with structs. A struct with just two fields: `filename`, and `value` (float). Put them in a vector and sort by the order of the `value` field, then pick the first 5, which will also give you the first 5 files – smac89 Feb 22 '22 at 00:24
  • You generally have two options: 1) reorganize your data to store pairs (_e.g._ `std::pair`); 2) create a vector of indices and create a comparison function that uses your float vector for sorting -- after that you'll have a list of indices sorted by the float values, without modifying either of your other two vectors – paddy Feb 22 '22 at 00:24

0 Answers0