vector struct a按照结构体中某一元素排序相同元素只保留最大值操作

首先根据需求定义了一个结构体

struct face_idx_score
{
    int idx = 0;
    float score = 0.0f;
};

然后使用容器vector定义了一个对象    vector<face_idx_score>     result_face_vec;

需求是相同的idx只保留score最大的一个,并写入.csv文件

使用sort()排序result_face_vec;需要重载操作符 ‘==’    ‘<’

struct face_idx_score
{
    int idx = 0;
    float score = 0.0f;

    bool operator == (const face_idx_score & obj) const //重载 == 操作符
    {
        return idx == obj.idx;
    }

    bool operator < (const face_idx_score & obj) const //重载 < 操作符
    {
//        return idx == obj.idx && score < obj.score;       //此时按照score升序排列
        return idx == obj.idx && obj.score < score;        //此时按照score降序排列
    }
};

函数实现

void write_result2csv( vector<face_idx_score> &fscore_vec, vector<string> &fname_vec )
{
    CSVWriter   csv;
    stringstream    stime;
    get_localtime(stime);
    string      csv_name = stime.str() + ".csv";

    csv.enableAutoNewRow(3);
    csv << "名字" << "ID号" <<"相似分值";

    vector<face_idx_score>::iterator    it;
    // detlete error result
    for( it = fscore_vec.begin(); it!= fscore_vec.end(); ){
        if( (*it).score < 0.5 )
            it=fscore_vec.erase(it);
        else
            it++;
    }

    sort(fscore_vec.begin(), fscore_vec.end()); //使用sort()进行排序,排序后的相同元素集中出现
    fscore_vec.erase(unique(fscore_vec.begin(), fscore_vec.end()), fscore_vec.end());
              //unique()函数将重复的元素放到vector的尾部 然后返回指向第一个重复元素的迭代器 再用erase函数擦除从这个元素到最后元素的所有的元素
    for( unsigned int i = 0; i < fscore_vec.size(); i++ ){
        string name = fname_vec.at(fscore_vec.at(i).idx).substr(0, fname_vec.at(fscore_vec.at(i).idx).length() - 4);
        csv << name << fscore_vec.at(i).idx << fscore_vec.at(i).score;
    }

    csv.writeToFile( csv_name, true );
    csv.ss.str( "" );
}

当然也可以使用函数的方式规定sort函数时升序还是降序

参考

sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级。本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能讲讲其用法:

1、sort入门:

使用sort需要包含algorithm头文件,完整代码如下
#include<iostream>
#include<vector>
#include<algorithm>//貌似可以不用,但最好加上。
using namespace std;
int main()
{
    vector<int>v;
    v.push_back(13);
    v.push_back(23);
    v.push_back(03);
    v.push_back(233);
    v.push_back(113);
    sort(v.begin(),v.end());
    int i=0;
    for(i=0;i<5;i++)
    {
        cout<<v[i]<<endl;
    }
    system("pause");
    return 0;
}
运行结果如下:

3
13
23
113
233
请按任意键继续. . .

可以看到结果是从小到大排序,但如果我需要从大到小排序呢?

2、改写comp从大到小排序。

加入comp函数后代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool comp(const int &a,const int &b)
{
    return a>b;
}
int main()
{
    vector<int>v;
    v.push_back(13);
    v.push_back(23);
    v.push_back(03);
    v.push_back(233);
    v.push_back(113);
    sort(v.begin(),v.end(),comp);
    int i=0;
    for(i=0;i<5;i++)
    {
        cout<<v[i]<<endl;
    }
    system("pause");
    return 0;
}
运行结果:

233
113
23
13
3
请按任意键继续. . .

为什么会这样呢?比较时sort函数根据comp函数进行判断输的大小,系统默认a<b时返回真,于是从小到大排,而我的comp函数设定为a>b时返回为真,那么最终得到的排序结果也相应的从小到大变成从大到小。简单吧~~

3、对结构体排序

有了comp函数我们就可以实现对任意结构体任意对象进行排序,只需要对应修改comp函数即可实现。代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct ss
{
    int a,b;
};
bool comp(const ss &a,const ss &b)
{
    return a.a<b.a;
}
int main()
{
    vector<ss>v;
    ss s1,s2,s3,s4,s5;
    s1.a=4;s1.b=23;
    s2.a=1;s2.b=213;
    s3.a=2;s3.b=231;
    s4.a=5;s4.b=123;
    s5.a=3;s5.b=223;
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);
    sort(v.begin(),v.end(),comp);
    int i=0;
    for(i=0;i<5;i++)
    {
        cout<<v[i].a<<" "<<v[i].b<<endl;
    }
    system("pause");
    return 0;
}
比如ss结构体中a代表的是索引号,b代表的是索引对应的值,那么我想按索引排序,通过改写comp函数即可实现。

结果:

1 213
2 231
3 223
4 23
5 123
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/cyf15238622067/article/details/82526497
今日推荐