• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
用STL对一组数组进行排序和去重
#include <iostream>
#include<cmath>
#include "algorithm"
#include "cstdio"
#include "stack"
using namespace std;
bool cmp(const int a, const int b)
{
    return a<b;
}

int main()
{
    int n;
    cin >> n;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
    }
    sort(a,a+n);
    int m = unique(a,a+n)-a;//地址的差值正好为数组中数据的个数
    cout << m <<endl;
    for(int i=0;i<m;i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;

    return 0;
}

第一:sort不加函数就是按从小到大的顺序进行快排。

第二:unique一般都是用于sort后的去重,实际上是把重复的元素丢到了最后面,返回的是没有重复元素数组的最后一个元素的地址。

posted on 2017-10-24 00:19  村雨sup  阅读(1138)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3