个人杂记-----收录各种新奇方法

2020.05.08
STL库

nth_element
把第k+1小的数放到下标为k的位置上

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n,k;
long long a[5000010];
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	nth_element(a,a+k,a+n); //使第k+1小的数就位   
	printf("%d",a[k]); //下标为k的地方 
	return 0;
}

unique
数组去重

#include<iostream>
#include<algorithm>//unique in <algorithm>
using namespace std;
int main(){
	int ints[]={1,1,1,1,2,2,2,3,5,5,10};
    unique(ints,ints+11);
    for(int i=0;i<5;i++)cout<<ints[i]<<' ';
}
//  1 2 3 5 10

count 函数

a[5]={1,2,2,6,8};
vector<int> q;
q[0] = 2;
q[1] = 2;
q[2] = 1;
q[3] = 1;
q[4] = 2;
cout<<count(a,a+5,2); // 2
cout<<count(a.begin(),a.end(),2); //3

猜你喜欢

转载自blog.csdn.net/zhimeng_LQ/article/details/105999014
今日推荐