仕上げメイク本続い:
0、MAX()、分()、ABS()
ABS()は整数でなければならず、もし浮動小数点数の絶対値が、Fabを使用して()
#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、スワップ(〜簡単)
#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に示すように、逆(反転データインターバル)
#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]);
}
}
コンテナ要素の反転
#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
完全配列の配列アラインメントによって与えられます
#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、フィル()
配列の容器またはとすることができる同じ値の期間の形成部(memsetを異なる、改変のmemsetの全体)
#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、ソート
ソートデフォルト昇順、この種の唯一のレコードコンテナ
#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;
}
小規模から大規模まで押して文字列の長さ:
#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()とUPPER_BOUND、()
LOWER_BOUND(最初、最後、ヴァル)[最初の配列または容器を見ると、最後)の値は、位置の第1の範囲は、ヴァル要素に等しいより大きい
見つけるためUPPER_BOUND(最初、最後、ヴァル) 【最初の配列または容器内に、要素の最後の)位置内の最初の値はヴァルの範囲よりも大きいです
#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);
}