algorithm头文件下常用函数

algorithm头文件下常用函数

1.max(),min(),abs()

应用:max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须是两个。
abs(x) 返回x的绝对值。x必须为整数,浮点型的绝对值要用math头文件下的fabs

2.count()

应用:计算某区间内某元素出现的次数

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[6] = {1, 2, 2, 3, 5, 5};
	int cnt = count(a, a+6, 2);
	cout << cnt << endl;	// 2
    return 0;
}
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	string str = "Hello world!";
	int cnt = count(str.begin(), str.end(), 'l');
	cout << cnt << endl;	// 3
    return 0;
}

3.find()

应用:返回第一次出现的指针或迭代器,通过做差可获得在数组或者向量中的下标

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[] = {1, 2, 2, 5, 4};
	int pos = find(a, a+5, 5) - a;
	cout << pos << endl;	//输出3,为该元素的下标
}
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	string str = "Hello world!";
	int pos = find(str.begin(), str.end(), 'l') - str.begin();
	cout << pos << endl;	//2
	return 0;
}

2.swap()

应用:swap(x,y)用来交换x和y的值

3.reverse()

应用:reverse(it,it2) 可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转。

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int a[10]={10,11,12,13,14,15};
    reverse(a,a+4);			//反转前四个元素
    for(int i=0;i<6;i++){   
        printf("%d ",a[i]); //13 12 11 10 14 15
    }
    return 0;
}

#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string str="abcdefghi";
    reverse(str.begin()+2,str.begin()+6); //反转第三个到第六个元素
    for(int i=0;i<str.length();i++){      
        printf("%c",str[i]);			  //abfedcghi
    }
    return 0;
}

4.next_permutation()

应用:next_permutation() 给出一个序列在全排列中的下一个序列

#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int a[10]={1,2,3};
    do{
        printf("%d %d %d\n",a[0],a[1],a[2]);
    }while(next_permutation(a,a+3));
    return 0;
}

5. fill()

应用:fill() 可以把数组或容器中的某一段区间赋为某个相同的值。和memset不同,这里的赋值可以使数组类型对应范围中的任意值。

#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int a[10]={1,2,3,4,5};
    fill(a,a+5,666);
    for(int i=0;i<5;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

6. sort()

应用:排序,默认为递增排序

  • 若要递减排序,需要增加比较函数
bool cmp(int a,int b){
  return a>b;
}
sort(a,a+n,cmp);
  • 结构体数组排序
struct node{
  int x,y;
};
bool cmp(node a,node b){
  return a.x>b.x; 
}
  • 容器排序,在STL标砖容器中,只有vector/string/deque可以sort
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
int main()
{
    vector<int> vi;
    vi.push_back(3);
    vi.push_back(1);
    vi.push_back(2);
    sort(vi.begin(),vi.end(),cmp);
    for(int i=0;i<3;i++){
        printf("%d ",vi[i]);
    }
    return 0;
}

7. lower_bound()和upper_bound()

应用:lower_bound 和 upper_bound()需要用在一个有序数组或容器中。
lower_bound(first,last,val) 用来寻找在数组或容器的[first,last)范围内第一个值大于等于
val元素的位置,如果是数组,返回该位置的指针;若果是容器,返回该位置的迭代器
upper_bound(first,last,val) 用来寻找在数组或容器的[first,last)范围内第一个值大于
val元素的位置,如果是数组,返回该位置的指针;若果是容器,返回该位置的迭代器

#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int a[10]={1,2,2,3,3,3,5,5,5,5};
    printf("%d,%d\n",lower_bound(a,a+10,3)-a,upper_bound(a,a+10,3)-a);
    return 0;
}

8.set_intersection()、set_union()和set_difference()

应用:求交集、并集、余集

#include<vector>
using namespace std;
int main(){
vector<int> a;
vector<int> b;
vector<int> c;
set_intersection(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
set_union(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
set_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
}
发布了54 篇原创文章 · 获赞 17 · 访问量 9177

猜你喜欢

转载自blog.csdn.net/qq_41979513/article/details/103104099