algorithm header files commonly used functions (reproduced, there are original link)

@ [TOC] reproduced picture due to the direct label all misplaced, can not be corrected, thus re-typeset copy. There are text links, hope understanding.

(1) max () function: returns the maximum of two numbers.
1. Usage: max (x, y); Note: there are only two parameters (which may be floating point)
to return to this maximum value of x, y, z of the three, can be written as max (x, max ( Y, Z);
#include
#include
the using namespace STD;
int main () {
int X = 10, Y = 15, Z = 20 is;
int max = a (X, Y); // find the maximum of two numbers
int b = max (x, max (x, z)); // find the maximum number of three
COUT a << << endl;
COUT B << << endl;
return 0;
}
(2) min () function: returns the minimum of two numbers
1. usage: min (x, y); Note: there are only two parameters (which may be floating point)
to return to this,, z three maximum x y, can be written as min (X, min (Y, Z));
#include
#include
the using namespace STD;
int main () {
int X = 10, Y = 15, Z = 20 is;
int A = min (X, Y);
B min = int (X, min (X, Z));
COUT A << << endl;
COUT B << << endl;
0 return;
}
(. 3) ABS () function: the absolute value of a number of
1. Usage: abs (x) Note: x must be an integer
, if the absolute value of a floating-point math is used in the header file fabs ().

#include
#include<math.h>
#include
using namespace std;
int main() {
int x,y;
x=-10;
y=-1.2;
int a=abs(x);
int b=fabs(y);
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
(4) swap(x,y) :交换x,y的值

#include
#include
the using namespace STD;
int main () {
int A =. 1, B = 2;
the swap (A, B);
COUT A << << endl;
COUT B << << endl;
return 0;
}
( . 5) reverse ()
reverse (IT, IT2) may be reversed in the pointer array element or container element iterator between [it, it2) in [it, it2) range.

1. The array reversed

#include
#include
the using namespace STD;
int main () {
int A [10] = {10,11,12,13,14,15};
Reverse (A, A +. 4); // a a [0] - a [3] is inverted
for (int I = 0; I <. 6; I ++)
{
COUT << A [I] << "";
}
return 0;
}
2. container (string or strings) anti turn

#include
#include
#include
the using namespace STD;
int main () {
String A = "ABCDEFGHIJ";
Reverse (a.begin () + 2, a.begin () +. 6); // a a [2] -a [5] is inverted
COUT << a;
return 0;
}
(. 6) next_permutation ()
is given in a sequence in a sequence alignment of full-

#include
#include
using namespace std;

main int () {
int A [. 3] = {l, 2,3};
do
{
COUT << A [0] << "" << A [. 1] << "" << A [2] << endl;
} the while (next_permutation (a, a +. 3));
return 0;
}
(. 7) Fill () array or an interval can be assigned the same value as the container

#include
#include
the using namespace STD;
int main () {
int A [. 5] {1,2,3,4,5};
Fill (A, A + 5,233); // [A, A +. 5) is the interval 233 is a value
for (int I = 0; I <. 5; I ++)
{
COUT << A [I] << "";
}
COUT << endl;
return 0;
}
(. 8) Sort () is used to sort

For integer, floating point, character sort (c ++ template used in writing)
Note: The default sort is ascending
#include
#include
a using namespace std;
Template
void Print (T arr []) // output function
{
for (int I = 0; I <. 5; I ++)
{
COUT << ARR [I] << "";
}
}
int main () {
int ARR [. 5] = {9,8,5,3, }. 6;
char CH [. 5] = { 'H', 'A', 'm', 'n-', 'I'};
Double D [. 5] = {} 1.1,1.6,1.5,1.8,2.1;
Sort (ARR, ARR +. 5);
Print (ARR);
COUT << endl;
Sort (CH, CH +. 5);
Print (CH);
COUT << endl;
Sort (D, D +. 5);
Print (D) ;
COUT << endl;
return 0;
}
2. descending integer of

#include
#include
using namespace std;

bool cmp (int a, int b ) // need to increase the size of the big
{
return A> B;
}
int main () {
int ARR [. 5] = {8,9,5,4,3};
Sort (ARR , + ARR. 5, CMP);
for (int I = 0; I <. 5; I ++)
{
COUT << ARR [I] << "";
}
COUT << endl;
return 0;
}
3. for the character descending

#include
#include
using namespace std;

bool cmp(char a,char b)
{
return a>b;
}
int main() {
char ch[5]={‘h’,‘a’,‘m’,‘n’,‘i’};
sort(ch,ch+5,cmp);
for(int i=0;i<5;i++)
{
cout<<ch[i]<<" ";
}
cout<<endl;
return 0;
}
4.对浮点型从大到小

#include
#include
using namespace std;

bool cmp(double a,double b)
{
return a>b;
}
int main() {
double d[5]={1.1,1.6,1.5,1.8,2.1};
sort(d,d+5,cmp);
for(int i=0;i<5;i++)
{
cout<<d[i]<<" ";
}
cout<<endl;
return 0;
}
5.对结构体进行排序

#include
#include
using namespace std;
struct Node
{
int x;
char y;
}arr[3];
bool cmp(Node a,Node b)
{
if(a.x!=b.x)
return a.x>b.x;
else
return a.y>b.y;
}
int main() {
arr[0].x=5;
arr[0].y=‘H’;
arr[1].x=5;
arr[1].y=‘A’;
arr[2].x=3;
arr[2].y=‘B’;
sort(arr,arr+3,cmp);
for(int i=0;i<3;i++)
{
cout<<arr[i].x<<" "<<arr[i].y<<endl;
}
cout<<endl;
return 0;
}
6.对容器的排序

#include
#include
#include
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main() {
vector s;
s.push_back(3);
s.push_back(9);
s.push_back(11);
sort(s.begin(),s.end(),cmp);
for(int i=0;i<3;i++)
{
cout<<s[i]<<" ";
}
cout<<endl;
return 0;
}
7.对string字符串排序

#include
#include
#include
the using namespace STD;
int main () {
String STR [. 3] = { "BCD", "ABC", "EFG"};
Sort (STR, STR +. 3); // string ascending large order
for (int I = 0; I <. 3; I ++)
{
COUT << STR [I] << "";
}
COUT << endl;
return 0;
}
(. 8) lower_bound () requires an ordered upper_bound, vessel or array;
1.lower_bound (first, Last, val): is used to find the array or the container [first, within the last) is greater than a first range of positions equal to val element, if an array, the position of the return pointer, if a container, the iterator returns the position

2.upper_bound (first, last, val): is used to find the array or the container [first, a first position is greater than the element of val last) range, if the array is, the position of the pointer is returned, if the container iterator returns the position

#include
#include
the using namespace STD;
int main () {
int A [10] = {1,3,3,3,5,7,9,10,15,15};
// Find position 3, the subscript from 0
COUT << lower_bound (A, A + 10,3) -a << endl;
COUT << upper_bound, (A, A + 10,3) -a << endl;
return 0;
}


Author: LEEWLD
Source: CSDN
Original: https://blog.csdn.net/wait_13/article/details/86254025
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/m0_45128748/article/details/90761356