stl提供了sort排序,针对vector可以很方便地进行指定的排序。本文给出两个例子,一个是针对结构体(类)某个成员的排序;另一个针对分辨率排序。
一个复杂的结构体(类)存在着大量的成员,使用vector存储,假如要依据某一成员进行排序,则可以重载“<”、“>”、“==”,然后使用sort排序。下面的代码根据foobar类中的token关键字排序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include <vector> #include <string> #include <algorithm> #include <stdio.h> class foobar { public: std::string token; std::string item; int number; // 重载操作符 bool operator<(const foobar& rhs) { return (*this).token < rhs.token;}; bool operator>(const foobar& rhs) { return (*this).token > rhs.token;}; bool operator==(const foobar& rhs) { return (*this).token == rhs.token;} }; // 结构体(类)成员排序 bool sort_token(const foobar& s1, const foobar& s2) { return s1.token.compare(s2.token) < 0; } int struct_element_sort(int argc, char* argv[]) { std::vector<foobar> vFoo; foobar tmp; tmp.token = "osd_1"; tmp.item = "OSD11111"; tmp.number = 1; vFoo.push_back(tmp); tmp.token = "osd_0"; tmp.item = "OSD111111"; tmp.number = 0; vFoo.push_back(tmp); tmp.token = "osd_2"; tmp.item = "OSD22222"; tmp.number = 2; vFoo.push_back(tmp); tmp.token = "osd_3"; tmp.item = "OS3330"; tmp.number = 3; vFoo.push_back(tmp); tmp.token = "osd_4"; tmp.item = "OS444444444444444"; tmp.number = 4; vFoo.push_back(tmp); tmp.token = "osd_1"; tmp.item = "OS5555555554444"; tmp.number = 5; vFoo.push_back(tmp); printf("before sort: \n"); for (unsigned int i = 0; i < vFoo.size(); i++) { printf("token: %s font: %d item: %s\n", vFoo[i].token.c_str(), vFoo[i].number, vFoo[i].item.c_str()); } std::sort(vFoo.begin(),vFoo.end(),sort_token); printf("after sort: \n"); for (unsigned int i = 0; i < vFoo.size(); i++) { printf("token: %s font: %d item: %s\n", vFoo[i].token.c_str(), vFoo[i].number, vFoo[i].item.c_str()); } std::vector<foobar>::iterator unque_it = std::unique(vFoo.begin(), vFoo.end()); vFoo.erase(unque_it, vFoo.end()); printf("after unque: \n"); for (unsigned int i = 0; i < vFoo.size(); i++) { printf("token: %s font: %d item: %s\n", vFoo[i].token.c_str(), vFoo[i].number, vFoo[i].item.c_str()); } return 0; }
|
另外一个常见的场合是对分辨率进行排序。下面的代码将分辨率存储于string中,然后分别对比宽、高,分辨率大的在前面。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| //////////////////////////////////// // 普通字符串 // 分辨率 降序,即大的分辨率在前面 bool sort_res(const std::string& s1, const std::string& s2) { //return s1.compare(s2) < 0; int width1 = 0; int height1 = 0; int width2 = 0; int height2 = 0; sscanf(s1.c_str(), "%dx%d", &width1, &height1); sscanf(s2.c_str(), "%dx%d", &width2, &height2); // 宽相等,则比较高 if (width1 == width2) { return height1 > height2; } else { return width1 >width2; } return true; }
int sort_string() { std::vector<std::string> foo; foo.push_back("1920x1080"); foo.push_back("1280x720"); foo.push_back("1920x1000"); foo.push_back("720x576"); foo.push_back("720x1080"); std::sort(foo.begin(), foo.end(), sort_res); printf("after sort: \n"); for (unsigned int i = 0; i < foo.size(); i++) { printf("resolution: %s\n", foo[i].c_str()); } return 0; }
int main(int argc, char* argv[]) { //struct_element_sort(argc, argv); sort_string(); return 0; }
|
李迟 2016.6.21 周二 夜 部门吃饭归来 记