C ++ template library STL - commonly used functions in the algorithm

Followed by a finishing make book:
0, max (), min (), ABS ()
ABS () must be an integer; if the absolute value of the floating-point number, using FABS ()

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){
	int x = 1,y = -2;
	printf("%d %d\n",max(x,y),min(x,y));
	printf("%d %d\n",abs(x),abs(y));
	double z = -1.222;
	printf("%.3f\n",fabs(z));//小数的绝对值 
	return 0;
}

1, swap (~ easy)

#include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int x = 1,y = 2;
	swap(x,y);
	printf("%d %d\n",x,y);
	return 0;
}

2, reverse (the inverted data interval)

#include <stdio.h>
#include <algorithm>
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++){
		printf("%d ",a[i]);
	}
}

Inversion of the container element

#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;

int main(){
	string str = "abcdefghijklmn";
	reverse(str.begin()+2,str.begin()+6);
	for(int i = 0;i<str.length();i++){
		printf("%c",str[i]);
	}
	return 0;
}

3, next_permutation
given by a sequence alignment of the full sequence

#include <stdio.h>
#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;
}

4, fill ()
can be an array vessel or a forming section for a period of the same value (different from the memset, memset entire modified)

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

. 5, Sort
Sort Ascending default , this sort only record container

#include <stdio.h>
#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; 
}

Press string length from small to large:

#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(string str1,string str2){
	return str1.length()<str2.length();//按string的长度从小到大排序 
}

int main(){
	string str3[3] = {"bbbb","cc","aaa"};
	sort(str3,str3+3,cmp);
	for(int i = 0;i <3;i++){
		cout<<str3[i]<<endl;
	}
	return 0;
}

6, lower_bound () and upper_bound, ()
lower_bound (First, Last, val) Looking at the [first array or the container, the last) value is greater than a first range of positions equal to val element
upper_bound (first, last, val) to find in the [first array or container, a first value within a last) position of the element is greater than the range of val

#include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int a[10] = {1,2,2,3,3,3,5,5,5,5};
	//寻找-1 
	int *lowerPos = lower_bound(a,a+10,-1);
	int *upperPos = upper_bound(a,a+10,-1);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找1
	lowerPos = lower_bound(a,a+10,1);
	upperPos = upper_bound(a,a+10,1);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找3
	lowerPos = lower_bound(a,a+10,3);
	upperPos = upper_bound(a,a+10,3);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找4
	lowerPos = lower_bound(a,a+10,4);
	upperPos = upper_bound(a,a+10,4);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找6
	lowerPos = lower_bound(a,a+10,6);
	upperPos = upper_bound(a,a+10,6);
	printf("%d, %d\n",lowerPos - a,upperPos - a); 
}
Published 123 original articles · won praise 3 · Views 3220

Guess you like

Origin blog.csdn.net/weixin_42377217/article/details/104094379