C++之结构体数组和std::vector容器结合使用的排序/均值/方差

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: C++之结构体数组和std::vector容器结合使用的排序/均值/方差

1、排序


最近有个项目需要使用C++ STL的vector容器,然后做一个排序操作。STL很强大,有它自己的排序方法。


std::sort(数组起始指针,数组尾指针,排序规则);


举例如下:

//参考文献,sort对结构体排序
//排序方法,sort(数组起始指针,数组尾指针,排序规则);
//数组起始指针,数组尾指针是左闭右开;
//排序规则可以省略,也可以自己写;
//https://blog.csdn.net/qq_40828914/article/details/80670151
#include <QCoreApplication>
#include <algorithm>
#include <iostream>
#include <string>
//结构体排序方法1
//按姓名从小到大排序,姓名一样,按年龄从小到大排序
struct student1
{
    std::string name; //姓名
    int age;          //年龄
};
bool compare(const student1 &s1, const student1 &s2)
{
    //自己定义的排序规则
    if (s1.name == s2.name)
    {
        return s1.age < s2.age;
    }
    else
    {
        return s1.name < s2.name;
    }
}
//结构体排序方法2
//按姓名从小到大排序,姓名一样,按年龄从小到大排序
struct student2
{
    std::string name; //姓名
    int age;          //年龄
    bool operator<(const student2 &s2) const
    {
        //符号重载
        if (name == s2.name)
        {
            return age < s2.age;
        }
        else
        {
            return name < s2.name;
        }
    }
};
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    //普通数组排序
    int a[] = {9, 2, 4, 5, 10, 7, 30};
    std::sort(a, a + 7);                   //省略掉排序规则的形式,默认从小到大
    std::sort(a, a + 7, std::less<int>()); //用c++自身的排序规则,从小到大
    for (int i = 0; i < 7; i++)
    {
        std::cout << a[i] << " ";
    }
    std::cout << std::endl;
    std::sort(a, a + 7, std::greater<int>()); //用c++自身的排序规则,从大到小
    for (int i = 0; i < 7; i++)
    {
        std::cout << a[i] << " ";
    }
    std::cout << std::endl;
    //结构体数组排序方法1
    student1 s1[100];
    s1[0].name = "zhangsan";
    s1[0].age = 18;
    s1[1].name = "zhangsan";
    s1[1].age = 19;
    s1[2].name = "lisi";
    s1[2].age = 20;
    std::sort(s1, s1 + 3, compare); //左闭右开,所以是对s[0]到s[2]排序
    for (int i = 0; i < 3; i++)
    {
        std::cout << s1[i].name << " " << s1[i].age << std::endl;
    }
    //结构体数组排序方法2:符合重载
    student2 s2[100];
    s2[0].name = "zhangsan";
    s2[0].age = 18;
    s2[1].name = "zhangsan";
    s2[1].age = 19;
    s2[2].name = "lisi";
    s2[2].age = 20;
    std::sort(s2, s2 + 3); //左闭右开,所以是对s[0]到s[2]排序
    for (int i = 0; i < 3; i++)
    {
        std::cout << s2[i].name << " " << s2[i].age << std::endl;
    }
    //针对std::vector容器排序
    std::vector<student1> vtDemo;
    student1 s3;
    s3.name = "hello";
    s3.age = 12;
    vtDemo.emplace_back(s3);
    s3.name = "world";
    s3.age = 7;
    vtDemo.emplace_back(s3);
    s3.name = "hello";
    s3.age = 11;
    vtDemo.emplace_back(s3);
    std::sort(vtDemo.begin(), vtDemo.end(), compare);
    for (size_t i = 0; i < vtDemo.size(); i++)
    {
        std::cout << vtDemo[i].name << " " << vtDemo[i].age << std::endl;
    }
    return app.exec();
}


2、均值与方差


先看基本数据结构

#include <numeric>
double sum = std::accumulate(std::begin(resultSet), std::end(resultSet), 0.0);  
double mean =  sum / resultSet.size(); //均值  
double accum  = 0.0;  
std::for_each (std::begin(resultSet), std::end(resultSet), [&](const double d) {  
    accum  += (d-mean)*(d-mean);  
});  
double stdev = sqrt(accum/(resultSet.size()-1)); //方差


再看自定义数据结构


struct Grade
{
    string name;
    int grade;
};
std::vector<Grade> subject = {
    {"English", 80},
    {"Biology", 70},
    {"History", 90}};
int test()
{
    //方法1,accumulate
    int sum1 = std::accumulate(subject.begin(), subject.end(), 0,
                               [](int a, Grade b) -> int {
                                   return a + b.grade;
                               });
    cout << "sum1 = " << sum1 << endl;
    //方法2,for_each
    int sum2 = 0;
    for_each(subject.begin(), subject.end(), [&sum2](Grade b) -> void {
        sum2 += b.grade;
    }); // sum2作为引用形参传递
    cout << "sum2 = " << sum2 << endl;
    system("pause");
    return 0;
}


相关文章
|
14天前
|
设计模式 存储 Android开发
c++的学习之路:18、容器适配器与反向迭代器
c++的学习之路:18、容器适配器与反向迭代器
19 0
|
27天前
|
人工智能 算法 测试技术
【数学】【排序】【C++算法】3027人员站位的方案数
【数学】【排序】【C++算法】3027人员站位的方案数
|
1月前
|
C++
C++11 std::lock_guard 互斥锁
C++11 std::lock_guard 互斥锁
10 0
|
16小时前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
6 1
|
6天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
12天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
13天前
|
C++
【C++】std::string 转换成非const类型 char* 的三种方法记录
【C++】std::string 转换成非const类型 char* 的三种方法记录
6 0
|
20天前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
13 0
|
26天前
|
算法 测试技术 C#
【模拟】【C++算法】2826. 将三个组排序
【模拟】【C++算法】2826. 将三个组排序
|
28天前
|
容器
C++map/multimap容器
C++map/multimap容器