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; }