Files
python/C++专题课程/STL基础知识/数组排序去重.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

45 lines
670 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
//数组长度
const int n=10;
/*
功能:打印一维数组
作者:黄海
时间:2019-10-29
*/
void printArray(int a[n]) {
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout<<endl;
}
int main() {
// 测试数据
int a[n] = { 0, 7, 7, 6, 1, 1, 5, 5, 8, 9 };
//数组长度
int length= (sizeof(a) / sizeof(a[0]));
//去重前打印输出
printArray(a);
//先排序再去重(由小到大:less,由大到小:greater)
sort(a, a + n, less<int>() );
//开始去重
int n = unique(a, a + length) - a;
//输出去重后的数组
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
return 0;
}