std list/vector sort 排序就这么简单
2014-10-09 10:11
666 查看
网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多。关于排序,我还真没研究过,看了江湖郎中和蒙古大夫的帖子,搞了半天不行,所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index。但是要注意对象和对象指针的排序区别。
容器中是对象时,用<排序。
容器中是对象指针时,用()和比较函数排序都可以。
list用成员方法sort
vector用sort函数
[cpp]
view plaincopy
class TestIndex{
public:
int index;
TestIndex(){
}
TestIndex(int _index):index(_index){
}
bool operator()(const TestIndex* t1,const TestIndex* t2){
printf("Operator():%d,%d/n",t1->index,t2->index);
return t1->index < t2->index;
}
bool operator < (const TestIndex& ti) const {
printf("Operator<:%d/n",ti.index);
return index < ti.index;
}
};
bool compare_index(const TestIndex* t1,const TestIndex* t2){
printf("CompareIndex:%d,%d/n",t1->index,t2->index);
return t1->index < t2->index;
}
int main(int argc, char** argv) {
list<TestIndex*> tiList1;
list<TestIndex> tiList2;
vector<TestIndex*> tiVec1;
vector<TestIndex> tiVec2;
TestIndex* t1 = new TestIndex(2);
TestIndex* t2 = new TestIndex(1);
TestIndex* t3 = new TestIndex(3);
tiList1.push_back(t1);
tiList1.push_back(t2);
tiList1.push_back(t3);
tiList2.push_back(*t1);
tiList2.push_back(*t2);
tiList2.push_back(*t3);
tiVec1.push_back(t1);
tiVec1.push_back(t2);
tiVec1.push_back(t3);
tiVec2.push_back(*t1);
tiVec2.push_back(*t2);
tiVec2.push_back(*t3);
printf("tiList1.sort()/n");
tiList1.sort();//无法正确排序
printf("tiList2.sort()/n");
tiList2.sort();//用<比较
printf("tiList1.sort(TestIndex())/n");
tiList1.sort(TestIndex());//用()比较
printf("sort(tiVec1.begin(),tiVec1.end())/n");
sort(tiVec1.begin(),tiVec1.end());//无法正确排序
printf("sort(tiVec2.begin(),tiVec2.end())/n");
sort(tiVec2.begin(),tiVec2.end());//用<比较
printf("sort(tiVec1.begin(),tiVec1.end(),TestIndex())/n");
sort(tiVec1.begin(),tiVec1.end(),TestIndex());//用()比较
printf("sort(tiVec1.begin(),tiVec1.end(),compare_index)/n");
sort(tiVec1.begin(),tiVec1.end(),compare_index);//用compare_index比较
return 0;
}
容器中是对象时,用<排序。
容器中是对象指针时,用()和比较函数排序都可以。
list用成员方法sort
vector用sort函数
[cpp]
view plaincopy
class TestIndex{
public:
int index;
TestIndex(){
}
TestIndex(int _index):index(_index){
}
bool operator()(const TestIndex* t1,const TestIndex* t2){
printf("Operator():%d,%d/n",t1->index,t2->index);
return t1->index < t2->index;
}
bool operator < (const TestIndex& ti) const {
printf("Operator<:%d/n",ti.index);
return index < ti.index;
}
};
bool compare_index(const TestIndex* t1,const TestIndex* t2){
printf("CompareIndex:%d,%d/n",t1->index,t2->index);
return t1->index < t2->index;
}
int main(int argc, char** argv) {
list<TestIndex*> tiList1;
list<TestIndex> tiList2;
vector<TestIndex*> tiVec1;
vector<TestIndex> tiVec2;
TestIndex* t1 = new TestIndex(2);
TestIndex* t2 = new TestIndex(1);
TestIndex* t3 = new TestIndex(3);
tiList1.push_back(t1);
tiList1.push_back(t2);
tiList1.push_back(t3);
tiList2.push_back(*t1);
tiList2.push_back(*t2);
tiList2.push_back(*t3);
tiVec1.push_back(t1);
tiVec1.push_back(t2);
tiVec1.push_back(t3);
tiVec2.push_back(*t1);
tiVec2.push_back(*t2);
tiVec2.push_back(*t3);
printf("tiList1.sort()/n");
tiList1.sort();//无法正确排序
printf("tiList2.sort()/n");
tiList2.sort();//用<比较
printf("tiList1.sort(TestIndex())/n");
tiList1.sort(TestIndex());//用()比较
printf("sort(tiVec1.begin(),tiVec1.end())/n");
sort(tiVec1.begin(),tiVec1.end());//无法正确排序
printf("sort(tiVec2.begin(),tiVec2.end())/n");
sort(tiVec2.begin(),tiVec2.end());//用<比较
printf("sort(tiVec1.begin(),tiVec1.end(),TestIndex())/n");
sort(tiVec1.begin(),tiVec1.end(),TestIndex());//用()比较
printf("sort(tiVec1.begin(),tiVec1.end(),compare_index)/n");
sort(tiVec1.begin(),tiVec1.end(),compare_index);//用compare_index比较
return 0;
}
相关文章推荐
- std list/vector sort 自定义类的排序就是这么简单
- std list/vector sort 排序就这么简单
- std list/vector sort 自定义类的排序就是这么简单
- std list/vector sort 排序就这么简单
- std list/vector sort 排序就这么简单
- std list/vector sort 排序就这么简单
- 【转】 std list/vector sort 排序
- std list/vector sort 排序
- std list/vector sort 自定义类的排序
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- List<T>集合的Sort自定义排序用法简单解析
- 实现一个简单的c++ list容器(含sort排序 链表归并算法实现)
- 对std::vector/list的iterator简单包装尝试
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- std::list<>::sort()排序分析
- LeetCode 147. Insertion Sort List插入排序链表的高效简单解法
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法