Fuck VC::STL

本文介绍了在VC6中使用STL遇到的问题,特别是针对list容器中按成员变量排序时的困难。提供了两种解决方案:一是自定义一个test_ptr类作为wrapper进行比较;二是创建一个排序接口类test_ptr_comp。然而,由于VC6的限制,第二种方案无法编译通过。作者最终考虑使用map或者更换库来解决排序问题。
摘要由CSDN通过智能技术生成

说明: Fuck vc6 自带的 stl 库。
问题:
struct test
{
    test(int _nIndex) : nIndex(_nIndex) {}
    int nIndex;
};

list<test*> aList;
按照 test::nIndex 从小到大对 aList 中的元素进行排序。

方案:
方案一, 来源于辣子鸡丁. 制作一个 test* 的 wapper.
#include <list>
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

struct test
{
 test(int _nIndex = 0) { nIndex =  _nIndex;}
 int nIndex;
};

class test_ptr
{
public:
    test_ptr() : ptr_(0) {}
    test_ptr(test* p) : ptr_(p) {}
public:
    bool operator<(const test_ptr& x) const
    {
          return ptr_->nIndex < x.ptr_->nIndex;   
    }
     void print()
     {
          cout  << ptr_->nIndex  << endl;
     }
private:
    test * ptr_;   
};

list<test_ptr> aList;

void print()
{
 list<test_ptr>::iterator pos;
 for(pos = aList.begin(); pos != aList.end(); ++pos)
 {
      (*pos).print();
 }
}

int main()
{
    aList.push_back(new test(15));
    aList.push_back(new test(6));
    aList.push_back(new test(32));

 print();
    aList.sort();
 print();
 return 0;
}


方案二, 做一个排序接口。 一开始我用这种方法。 但他在 vc 下不能通过编译。 
// 在 Dev c++ 通过
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <list>
using namespace std;

struct test
{
 test(int _nIndex = 0) { nIndex = _nIndex; }
 int nIndex;
};

class test_ptr_comp
{
public:
    bool operator()(const test* const & x, const test* const &y) const
    {
          return x->nIndex < y->nIndex;   
    }
};

list<test*> aList;

void print()
{
 list<test*>::iterator pos;
 for(pos = aList.begin(); pos != aList.end(); ++pos)
 {
  cout << (*pos)->nIndex << endl;
 }
}

int main()
{
    aList.push_back(new test(15));
    aList.push_back(new test(6));
    aList.push_back(new test(18));

 print();
    aList.sort(test_ptr_comp());
 print();

 system("pause");
 return 0;
}

在 VC 下的 sort 函数只能支持 greater 的比较接口。fucking~~
第一种方法感觉很不友好, 而且每次都通过 wrapper 取东西。 打算 sort 用个 map 来串行数据了。

准备去打 sp6, 换库.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值