18. 移除特定元素——remove
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 //remove 6 #include<algorithm> 7 #include<numeric> 8 using namespace std; 9 10 int main() 11 { 12 int all[11] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 13 int *a = remove(all, all + 11, 2);// 移除all中所有数值为2的元素 14 15 for(int i = 0 ; i < a - all ; i ++) 16 printf_s("%d ", all[i]); 17 18 return 0; 19 }
19. 交换——swap
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 //swap 6 #include<algorithm> 7 #include<numeric> 8 9 using namespace std; 10 11 int main() 12 { 13 int all[11] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 14 15 swap(all[0], all[1]);//交换两个位置元素 16 17 for(int i = 0 ; i < 11 ; i ++) 18 printf_s("%d ", all[i]); 19 20 return 0; 21 }
20. 移除重复元素——unique
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 #include<algorithm> 6 #include<numeric> 7 using namespace std; 8 9 int main() 10 { 11 int all[11] = {11, 10, 9, 9, 7, 7, 5, 4, 3, 1, 1}; 12 13 int *a = unique(all, all + 11);//移除所有重复元素 14 15 for(int i = 0 ; i < a - all; i ++) 16 printf_s("%d ", all[i]); 17 }