STL中的算法大致可以分为一下七类:
- 不变序列算法
- 变值算法
- 删除算法
- 变序算法
- 排序算法
- 有序区间算法
- 数值算法
大多重载的算法都是有两个版本的
用 "=="判断元素是否相等,或者用 "<“来比较大小,多出一个类型参数“Pred” 或者函数形参"Pred op”,通过表达式op( x , y ) 的返回值true/false判断是否等于y,或者是否小于y。
如下面两个版本的min_element
不变序列算法
find
count:
#include <iostream>
#include <algorithm>
using namespace std;
class A
{
public:
int n;
A( int i ):n( i )
{}
};
bool operator<( const A &a1 , const A &a2 )
{
cout << "< called" << endl;
if( a1.n == 3 && a2.n == 7 )
{
return true;
}
return false;
}
int main(int argc, char *argv[])
{
A aa[] = { 3 , 5 , 7 , 2 , 1 };
cout << min_element( aa , aa + 5 )->n << endl;
cout << max_element( aa , aa + 5 )->n << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
变值算法
举例:
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <list>
#include <iterator>
using namespace std;
class CLessThen9
{
public:
bool operator()( int n )
{
return n < 9;
}
};
void outputSquare( int value )
{
cout << value * value << " ";
}
int calculateCube( int value )
{
return value * value * value;
}
int main(int argc, char *argv[])
{
const int SIZE = 10;
int a1[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
int a2[] = { 100 , 2 , 8 , 1 , 50 , 3 , 8 , 9 , 10 , 2 };
vector<int> v( a1 , a1 + SIZE );
ostream_iterator<int> output( cout , " " );
random_shuffle( v.begin() , v.end() );
cout << endl;
copy( v.begin() , v.end() , output );
copy( a2 , a2 + SIZE , v.begin() );
cout << endl;
cout << count( v.begin() , v.end() , 8 ) << endl;
cout << endl;
cout << count_if( v.begin() , v.end() , CLessThen9() );
cout << endl;
cout << *( min_element( v.begin() , v.end() ) );
cout << endl;
cout << *( max_element( v.begin() , v.end() ));
cout << endl;
cout << accumulate( v.begin() , v.end() , 0 );
cout << endl;
for_each( v.begin() , v.end() , outputSquare );
vector<int> cubes( SIZE );
transform( a1 , a1 + SIZE , cubes.begin() , calculateCube );
cout << endl;
copy( cubes.begin() , cubes.end() , output );
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <list>
#include <iterator>
#include <fstream>
#include <string>
using namespace std;
template< class T >
class My_ostream_iterator:public iterator<output_iterator_tag , T >
{
public:
My_ostream_iterator( ostream &o , string s ):sep( s ):os( o )
{;}
void operator++(){; } // 只需要定义即可,不需要做什么
My_ostream_iterator &operator*(){ return *this ; }
My_ostream_iterator &operator=( const T &val )
{
os << val << sep;
return *this;
}
private:
string sep; // 分隔符
ostream &os;
int main(int argc, char *argv[])
{
int a[4] = { 1 , 2 , 3 , 4 };
My_ostream_iterator<int> oit( cout , "*" );
copy( a , a + 4 , oit ); // 输出 1 * 2 * 3 * 4
ofstream oFile( "text.txt" , ios::out );
My_ostream_iterator<int> oitf( oFile , "*" );
copy( a , a + 4 , oitf ); // 向 test.txt文件中写入 1 * 2 * 3 * 4
oFile.close();
};
system("PAUSE");
return EXIT_SUCCESS;
}
删除算法
== 删除算法的时间复杂度都是o(n) ==
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <list>
#include <iterator>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str = "231";
char szStr[] = "324";
while( next_permutation( str.begin() , str.end() ) )
{
cout << str << endl;
}
cout << "----" << endl;
while( next_permutation( szStr , szStr + 3 ) )
{
cout << szStr << endl;
}
sort( str.begin() , str.end() );
cout << "----" << endl;
while( next_permutation( str.begin() , str.end() ) )
{
cout << str << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
int a[] = { 8 , 7 , 10 };
list<int> ls( a , a + 3 );
while( next_permutation( ls.begin() , ls.end() ))
{
list<int>::iterator i ;
for( i = ls.begin() ; i != ls.end() ; i++ )
{
cout << *i << " ";
}
cout << endl;
}
//主函数代码部分,其余部分请自行补齐
结果图片:
排序算法
快速排序举例子
class MyLess
{
public:
bool operator()( int n1 , int n2 )
{
return ( n1 % 10 ) < ( n2 % 10 );
}
};
int a[] = { 14 , 2 , 9 , 111 , 78 };
sort( a , a + 5 , MyLess() );
int i;
for( i = 0 ; i < 5 ; i++ )
{
cout << a[i] << " " ;
}
cout << endl;
sort( a , a + 5 , greater<int>() );
for( i = 0 ; i < 5 ; i++)
{
cout << a[i] << " " ;
}
cout << endl;
有序区间算法
binary_search