1, sorting,
A recent project needed to use a C++ STL vector and do a sort operation. The STL is powerful and has its own sorting method.
STD ::sort(array start pointer, array end pointer, sort);
Examples are as follows:
// references,sort sort on structure // sort method,sort(array start pointer, array end pointer,sort rule); // Array start pointer, array end pointer is left closed right open; // The collation can be omitted or written; //https://blog.csdn.net/qq_40828914/article/details/80670151 #include <QCoreApplication> #include <algorithm> #include <iostream> #include <string> struct student1 {STD ::string name; // name int age; / / age}; Bool compare(const student1 &s1, const student1 &s2) {// Self-defined sorting rule if (s1.name == s2.name) {return s1.age < s2.age; } else { return s1.name < s2.name; Struct student2 {STD ::string name;}} struct student2 {STD ::string name; // name int age; // Age bool operator<(const student2&s2) const {// Symbol overload 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>()); For (int I = 0; i < 7; i++) { std::cout << a[i] << " "; } std::cout << std::endl; std::sort(a, a + 7, std::greater<int>()); For (int I = 0; i < 7; i++) { std::cout << a[i] << " "; } std::cout << std::endl; // Array array sort method 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); For (int I = 0; int I = 0; i < 3; i++) { std::cout << s1[i].name << " " << s1[i].age << std::endl; } // Array array sort method 2: comply with overload 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); For (int I = 0; int I = 0; i < 3; i++) { std::cout << s2[i].name << " " << s2[i].age << std::endl; 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(); }Copy the code
2. Mean and variance
Let’s start with the basic data structure
#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)); / / varianceCopy the code
Look at custom data structures
struct Grade { string name; int grade; }; std::vector<Grade> subject = { {"English", 80}, {"Biology", 70}, {"History", 90}}; Sum1 = accumulate(subject.begin(), subject.end(), 0, []) Grade b) -> int { return a + b.grade; }); cout << "sum1 = " << sum1 << endl; // method 2,for_each int sum2 = 0; for_each(subject.begin(), subject.end(), [&sum2](Grade b) -> void { sum2 += b.grade; }); Sum2 is passed as a reference parameter cout << "sum2 = "<< sum2 << endl; system("pause"); return 0; }Copy the code