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;
}
呀 寫些入門級的 充充數
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;
}