std::set compare

 

#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;
        //大小都要比较,不然就会有二义性
        //从小到大排序
        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);//用于取初始样本
    int* cur_init_sub_set_indexs = NULL;	//初始选择的样本


    //当前模型的点集索引标志,currentVotes[i] 符合当前模型置1,否则0
    short* currentVotes = new short[numSamples];

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



    while (iterations < k)
    {
        //从数据集中随机选择n个点
        cur_init_sub_set_indexs = new int[minimalFitNum];

        //重置		
        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;
        }

        //查看是否已经用过这个子集
        std::pair< std::set<int*, SubSetIndexComparator >::iterator, bool > res = chosen_sub_sets.insert(cur_init_sub_set_indexs);



        if (res.second)//true,表示插入成功,第一次用到这个子集
        {
            std::cout << "TRUE" << std::endl;
        }
        std::cout << "=========" << std::endl;
        iterations++;
    }

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



    //遍历数据,用迭代器遍历数据
    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; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值