C++ CArray類及子類,使用sort()排序

 

http://www.codeguru.com/forum/archive/index.php/t-215414.html

 

這篇貼子幫了大忙。有一個回貼這樣說到:

 

#include <algorithm>
...
// Note the *only* change is the template type!
CArray<unsigned char, unsigned char> MyCArray;
//...
std::sort( MyCArray.GetData(), MyCArray.GetData() + MyCArray.GetSize());

It doesn't matter whether it's a CArray of strings, ints, chars, doubles, widgets, or your own class. As long as the items are comparable using operator < (which is what std::sort uses internally), the std::sort will work.

 

當然,sort()裏如果用到自定義比較函數,自己寫了。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章