首页 > 代码库 > vector 指针结果 排序

vector 指针结果 排序

struct ET
{
ET(int i){
n=i;
};
ET(){
n=0;
};
int n;
};
bool cmp(const ET* lhs, const ET* rhs)
{//从大到小排列
return (lhs->n > rhs->n);
}
void __fastcall TForm18::Button2Click(TObject *Sender)
{
vector <ET*> v1;
vector <ET*>::iterator Iter1;
ET et[16];
for ( int i = 0 ; i <= 5 ; i++ )
{
et[i].n=2 * i;
v1.push_back( &et[i]);
}
int ii;
for ( ii = 0 ; ii <= 5 ; ii++ )
{
et[ii+5].n=2 * ii;
v1.push_back( &et[ii+5]);
// v1.push_back( ET(2 * ii + 1) );
}
cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << (*Iter1)->n << " ";
cout << ")" << endl;

sort( v1.begin( ), v1.end( ),cmp );
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << (*Iter1)->n << " ";
cout << ")" << endl;
}

vector 指针结果 排序