sort(c++/stl)
今天遇到一个排序问题,本来想自己写一个排序的,翻阅了一下stl,发现stl已经提供了相应的方法,而且非常好用,速度也非常快。
#include <algorithm>
void sort( iterator start, iterator end );
void sort( iterator start, iterator end, StrictWeakOrdering cmp );
#include <algorithm>
void sort( iterator start, iterator end );
void sort( iterator start, iterator end, StrictWeakOrdering cmp );
vector<int> v; v.push_back( 23 );
v.push_back( -1 );
v.push_back( 9999 );
v.push_back( 0 );
v.push_back( 4 );
cout << "Before sorting: ";
for( i = 0; i < v.size(); i++ )
{ cout << v[i] << " "; }
cout << endl;
sort( v.begin(), v.end() );
cout << "After sorting: ";
for( i = 0; i < v.size(); i++ )
{ cout << v[i] << " "; }
cout << endl;
v.push_back( -1 );
v.push_back( 9999 );
v.push_back( 0 );
v.push_back( 4 );
cout << "Before sorting: ";
for( i = 0; i < v.size(); i++ )
{ cout << v[i] << " "; }
cout << endl;
sort( v.begin(), v.end() );
cout << "After sorting: ";
for( i = 0; i < v.size(); i++ )
{ cout << v[i] << " "; }
cout << endl;
bool cmp( int a, int b )
{ return a > b;
}
vector<int> v;
for( int i = 0; i < 10; i++ )
{
v.push_back(i);
}
cout << "Before: ";
for( int i = 0; i < 10; i++ )
{
cout << v[i] << " ";
}
cout << endl;
sort( v.begin(), v.end(), cmp );
cout << "After: ";
for( int i = 0; i < 10; i++ )
{
cout << v[i] << " ";
}
cout << endl;
{ return a > b;
}
vector<int> v;
for( int i = 0; i < 10; i++ )
{
v.push_back(i);
}
cout << "Before: ";
for( int i = 0; i < 10; i++ )
{
cout << v[i] << " ";
}
cout << endl;
sort( v.begin(), v.end(), cmp );
cout << "After: ";
for( int i = 0; i < 10; i++ )
{
cout << v[i] << " ";
}
cout << endl;
posted on 2007-11-26 16:55 john.huang 阅读(4678) 评论(0) 编辑 收藏 举报