3D FPS

1234567890

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  57 Posts :: 2 Stories :: 57 Comments :: 0 Trackbacks

常用链接

留言簿(10)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

C++ 想写些高深的 不过我不会
呀 写些入门级的 充充数
stl 里面有个算法的 sort

大概是用那个partion sort 算法吧 其实我不太清楚 根据OO 思想 不应该去知道 对这个思想有保流

使用上方便 放入起始的位置迭代器 然后再放入自己的函数对象就可以了

写个示例:

#include <stdio.h>
#include <tchar.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <stdlib.h>
#include <vector>

template<int iType>
struct mygreater
{     
    bool operator()(const std::pair<int,float>  & _Left, const std::pair<int,float>  & _Right) const
    {   
        if (iType == 1)
        {
            return (_Left.first > _Right.first);
        }
        else if (iType == 2)
        {
            return (_Left.second > _Right.second);
        }
        else
        {
             //error
        }
       
    }
};

void myprint(const std::pair<int,float>  & i)
{
    std::cout<< i.first << " " << i.second << "\n";
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::pair<int,float> > gVecpair;
    for (int i = 0; i <= 10; i++ )
        gVecpair.push_back( std::make_pair( rand(),(float)rand()));
   
    std::for_each(gVecpair.begin(),gVecpair.end(),myprint);
    std::sort(gVecpair.begin(),gVecpair.end(),mygreater<1>( ));
    std::for_each(gVecpair.begin(),gVecpair.end(),myprint);

    std::cout<<"\n";

    std::sort(gVecpair.begin(),gVecpair.end(),mygreater<2>( ));
    std::for_each(gVecpair.begin(),gVecpair.end(),myprint);

    getchar();
    return 0;
}


posted on 2009-01-12 00:12 DK_jims 阅读(385) 评论(1)  编辑 收藏 引用 所属分类: C++

Feedback

# re: stl::sort 2009-01-12 00:20 jimsmorong
呀 毙 好似有bug  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理