algorithm
/*algorithm头文件下的常用函数*/
/*
使用algorithm头文件,需要在头文件下加一行using namespace std;”
*/
//常用函数max(), min(), abs()
//swap()
//reverse()
//next_permutation()
//fill()
// sort()
//lower_bound和upper_bound()
/*max(), min(), abs()的使用*/
#include
#include
#include
using namespace std;
int main()
{
int x =1, y =-2;
cout <
cout << abs(x)<
return 0;
} */
/*
swap()的使用
swap(x,y)用来交换x和y的值,示例如下:
*/
#include
#include
using namespace std;
int main()
{
int x=1, y=2, z;
swap(x, y);
cout<< x << " "<< y<
swap(x, z);
cout<
swap(y, z);
cout<
return 0;
}
*/
/*reverse()的使用 */
/*reverse(it, it2)可以将数组指针在[it, it2)之间的元素或容器的迭代器在[it, it2)范围内的元素
进行反转。示例如下: */
/*对整形数组逆转*/
#include
#include
using namespace std;
int main()
{
int a[10]= {10, 11, 12, 13, 14, 15};
reverse(a, a+4);//a[0]~a[3]反转
for(int i=0; i<6; i++){
cout<
}
return 0;
}*/
/*对容器中的元素(例如string字符串)进行反转,结果也一样*/
#include
#include
#include
using namespace std;
int main()
{
string str = "abcdefghi";
reverse(str.begin()+2, str.begin()+6);//对a[2]~a[5]逆转*左闭右开*
for( int i=0; i < str.length(); i++){
cout<
}
return 0;
}
/*next_permutation的用法
/*@注意!!!* 只有在是个有小到大的序列!!!!!*/
#include
#include
#include
using namespace std;
int main()
{
int a[10] = {3, 2, 1};
do{
cout<