应用场景:
在内存中维持一个有序的vector:
1 // VectorSort.cpp : Defines the entry point for the console application. 2 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 7 //先自定义一个结构体 8 struct Test { 9 float member1; 10 std::string member2; 11 12 }; 13 bool SortByM1( const Test* v1, const Test* v2)//注意:本函数的参数的类型一定要与vector中元素的类型一致 14 { 15 return v1->member1 > v2->member1; //降序排列 16 //return v1->member1 > v2->member1; //升序排列 17 } 18 void PrintVector( std::vector<Test*> & vec) 19 { 20 /* 21 插一句, 22 vec.begin()对应的位置是向量的第一个位置, 23 vec.end()对应的是vector中的最后的一个元素位置的后面的一个位置(我认为,实际上是一个无效位置) 24 文档上的定义:Returns an iterator referring to the past-the-end element in the vector container. 25 */ 26 for(std::vector<Test*>::iterator it = vec.begin() ; it != vec.end() ; it++ ) 27 { 28 std::cout<<(*it)->member1<<'\t'<<(*it)->member2<<std::endl; 29 } 30 } 31 32 int main(int argc, char* argv[]) 33 { 34 std::vector<Test*> vecTest; 35 std::string stt; 36 for(int i=0; i<10;++i){ 37 stt = "sh" + stt; 38 Test* tmp = new Test(); 39 tmp->member1 = i; 40 tmp->member2 = stt; 41 vecTest.push_back(tmp); 42 } 43 44 //排序之前 45 std::cout<<"Before Sort:"<<std::endl; 46 PrintVector(vecTest); 47 48 std::cout<<"对向量中的所有元素按member1进行升序排列:"<<std::endl; 49 std::sort(vecTest.begin(),vecTest.end(), SortByM1); 50 PrintVector(vecTest); 51 52 //std::cout<<"对向量中的第2个到第5个元素按member1进行升序排列:"<<std::endl; 53 //std::sort(vecTest.begin()+1,vecTest.begin()+5,SortByM1);//vecTest.begin()+5为第6个位置 54 //PrintVector(vecTest); 55 56 for(std::vector<Test*>::iterator it = vecTest.begin(); 57 it != vecTest.end(); ++it){ 58 delete *it; #free掉内存 59 } 60 61 return 0; 62 } 63 64 root@u18:~/cp/test# g++ vector_sort.cpp -g -Wall 65 root@u18:~/cp/test# ./a.out 66 Before Sort: 67 0 sh 68 1 shsh 69 2 shshsh 70 3 shshshsh 71 4 shshshshsh 72 5 shshshshshsh 73 6 shshshshshshsh 74 7 shshshshshshshsh 75 8 shshshshshshshshsh 76 9 shshshshshshshshshsh 77 对向量中的所有元素按member1进行升序排列: 78 9 shshshshshshshshshsh 79 8 shshshshshshshshsh 80 7 shshshshshshshsh 81 6 shshshshshshsh 82 5 shshshshshsh 83 4 shshshshsh 84 3 shshshsh 85 2 shshsh 86 1 shsh 87 0 sh
root@u18:~/cp/test# valgrind --tool=memcheck --leak-check=yes ./a.out
==24247== Memcheck, a memory error detector
==24247== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==24247== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==24247== Command: ./a.out
==24247==
Before Sort:
0 sh
1 shsh
2 shshsh
3 shshshsh
4 shshshshsh
5 shshshshshsh
6 shshshshshshsh
7 shshshshshshshsh
8 shshshshshshshshsh
9 shshshshshshshshshsh
对向量中的所有元素按member1进行升序排列:
9 shshshshshshshshshsh
8 shshshshshshshshsh
7 shshshshshshshsh
6 shshshshshshsh
5 shshshshshsh
4 shshshshsh
3 shshshsh
2 shshsh
1 shsh
0 sh
==24247==
==24247== HEAP SUMMARY:
==24247== in use at exit: 0 bytes in 0 blocks
==24247== total heap usage: 25 allocs, 25 frees, 768 bytes allocated
==24247==
==24247== All heap blocks were freed -- no leaks are possible
==24247==
==24247== For counts of detected and suppressed errors, rerun with: -v
==24247== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)