标签:std sort compare myvector const queue priority
function:
- sort
- 1,2,3,4,5
- cmp使用方法:std::greater<int>()
-
sort (myvector.begin()+4, myvector.end(), std::greater<int>());
- priority_queue
- 1,2,3,4,[5] (大顶堆)
- cmp使用方法:
std::greater<int>
-
priority_queue<int, std::vector<int>, std::greater<int> > third (myints,myints+4);
- set
- 1,2,3,4,5
- cmp使用方法:同上priority_queue
- map
- key: 1,2,3,4,5
- cmp使用方法:同上priority_queue
struct:
- greater
- >
- 5,4,3,2,1
- less(default)
- <
- 1,2,3,4,5
参考:
greater:
1 template <class T> struct greater { 2 bool operator() (const T& x, const T& y) const {return x>y;} 3 typedef T first_argument_type; 4 typedef T second_argument_type; 5 typedef bool result_type; 6 };
less:
1 template <class T> struct less { 2 bool operator() (const T& x, const T& y) const {return x<y;} 3 typedef T first_argument_type; 4 typedef T second_argument_type; 5 typedef bool result_type; 6 };
自定义cmp类:
// CLASS For: map, multimap struct classcomp_char { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;} }; // CLASS For: Type<int> : set, multiset, priority_queue // OBJ For Function: sort struct classcomp { bool operator() (const int& lhs, const int& rhs) const {return lhs<rhs;} } myobject; // Func For Function: sort bool myfunction (int i,int j) { return (i<j); } int main () { std::set<int,classcomp> set_test; // set std::multiset<int,classcomp> multiset_test; // multiset std::map<char,int,classcomp_char> map_test; // map std::multimap<char,int,classcomp_char> multimap_test; // multimap // using mycomparison: typedef std::priority_queue<int,std::vector<int>,classcomp> mypq_type; mypq_type pq_test; // priority_queue // using function as comp std::sort (myvector.begin()+4, myvector.end(), myfunction); // sort // using object as comp std::sort (myvector.begin(), myvector.end(), myobject); //sort return 0; }
标签:std,sort,compare,myvector,const,queue,priority 来源: https://www.cnblogs.com/habibah-chang/p/14852692.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。