当前位置:网站首页>std::set compare

std::set compare

2022-06-12 16:21:00 seventeen billion five hundred and six million three hundred an

 

#include <iostream>
#include <string>
#include <set>
#include <cmath>
#include <vector>

class SubSetIndexComparator
{
private:
    int m_length;
public:
    SubSetIndexComparator(int arrayLength) : m_length(arrayLength) {}
    bool operator()(const int* arr1, const int* arr2) const
    {
        std::cout << "AAAA" << std::endl;
        // All sizes should be compared , Otherwise, there will be ambiguity 
        // Sort from small to large 
        for (int i = 0; i < m_length; i++)
        {
            std::cout << "i=" << i << "  " << arr1[i] << " " << arr2[i] << std::endl;

            if (arr1[i] < arr2[i])
            {
                return true;
            }
            if (arr1[i] > arr2[i])
            {
                return false;
            }
        }
        return false;
    }
};


int main()
{
    int iterations = 0;
    int maxIterations = 10;
    double k = maxIterations;

    int minimalFitNum = 3;
    int numSamples = 4;

    std::vector<int> shuffled_indices(numSamples);// For taking initial samples 
    int* cur_init_sub_set_indexs = NULL;	// Initially selected samples 


    // The point set index flag of the current model ,currentVotes[i]  Conform to current model settings 1, otherwise 0
    short* currentVotes = new short[numSamples];

    SubSetIndexComparator subsetIdxComparator(minimalFitNum);
    std::set<int*, SubSetIndexComparator > chosen_sub_sets(subsetIdxComparator);



    while (iterations < k)
    {
        // Randomly select... From the dataset n A little bit 
        cur_init_sub_set_indexs = new int[minimalFitNum];

        // Reset 		
        for (int i = 0; i < (int)shuffled_indices.size(); i++)
        {
            shuffled_indices[i] = i;
        }

        for (int i = 0; i < minimalFitNum; i++)
        {
            std::swap(shuffled_indices[i], shuffled_indices[i + rand() % (numSamples - i)]);
        }



        memset(currentVotes, 0, numSamples * sizeof(short));

        for (int i = 0; i < minimalFitNum; i++)
        {
            cur_init_sub_set_indexs[i] = shuffled_indices[i];
            currentVotes[shuffled_indices[i]] = 1;
        }

#if 0
        for (int i = 0; i < (int)shuffled_indices.size(); i++)
        {
            std::cout << shuffled_indices[i] << " " << currentVotes[i]  << std::endl;
        }
#endif 

        std::cout << std::endl;
        for (int i = 0; i < minimalFitNum; i++)
        {
            std::cout << cur_init_sub_set_indexs[i] << std::endl;
        }

        // See if you have used this subset 
        std::pair< std::set<int*, SubSetIndexComparator >::iterator, bool > res = chosen_sub_sets.insert(cur_init_sub_set_indexs);



        if (res.second)//true, Indicates successful insertion , This subset is used for the first time 
        {
            std::cout << "TRUE" << std::endl;
        }
        std::cout << "=========" << std::endl;
        iterations++;
    }

    std::cout << "" << std::endl;



    // Traversal data , Iterate through the data with an iterator 
    for (std::set<int*, SubSetIndexComparator>::iterator it = chosen_sub_sets.begin(); it != chosen_sub_sets.end(); ++it)
    {
        for (int i = 0; i < minimalFitNum; i++)
        {
            std::cout << (*it)[i] << " ";
        }
        std::cout << std::endl;
    }



    /*
    // custom comparison
    std::set<Point, PointCmp> z = { {1, 0}, {1, 1}, {3, 4},  {2, 5} };
    z.insert({ 2, -1 }); // this fails because the magnitude of 1,-1 equals 1,1
    for (auto& p : z) std::cout << '(' << p.x << ',' << p.y << ") ";
    std::cout << '\n';
    */
    return 0; 
}

原网站

版权声明
本文为[seventeen billion five hundred and six million three hundred an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121618137933.html