C++之去重
2024-09-08 19:18:13
note
今天刷题,忘了去重复库函数,于是手写了一个。 前提: 必须保证数组是有序的。
源码
template <typename T>
void unique_arr(T arr[], int len, T* parr, int& out_len)
{
if (1 > len || nullptr == parr || NULL == parr)
return;
int cur_index = 0;
int pre_index = 0;
int real_len = 1;
parr[0] = arr[0];
while (cur_index < len)
{
if (arr[pre_index] != arr[++cur_index])
{
pre_index = cur_index;
parr[real_len++] = arr[cur_index];
}
}
out_len = real_len;
}
调用
int arr2[10] = { 0 };
int real_len = 0;
int arr[] = { 5, 1, 3, 2, 3, 4, 4, 2, 1, 6 };
// 排序
std::sort(std::begin(arr), std::end(arr));
// 去重复
unique_arr(arr, 10, arr2, real_len);
// 输出
for (int i = 0; i < real_len - 1; ++i)
std::cout << " " << arr2[i];
最新文章
- Oracle学习笔记2
- userdebug版本开机串口log打开
- python - 简明 性能测试
- HDOJ/HDU 1073 Online Judge(字符串处理~)
- ASE中的主要数据库
- Oracle Tablespace Transportation
- Python学习笔记:概要
- java设计原则---开闭原则
- jdbc 链接数据库步骤 7步
- Windows苹果安卓手机远程桌面客户端推荐
- SpringMVC 与 REST.
- SpringMVC学习 十三 拦截器栈
- centos7下配置saltstack
- java JDBC (八) 连接池 DBCP
- sublime配置 sublimecondeintel 分号后不要提示
- Python类的构成元素
- quartz ? * 区别
- [转]cron表达式详解
- N76E003之IIC
- MYSQL三大范式
热门文章
- cat的生产应用
- C语言 自定义函数按行读入文件
- account, accomplish, accumulate
- Linux基础命令---ab测试apache性能
- Linux基础命令---vmstat显示虚拟内存状态
- Mysql多字段模糊查询
- 二、SpringBoot实现上传文件到fastDFS文件服务器
- Maven的聚合工程(多模块工程)
- 【VSCode】检测到 #include 错误。请更新 includePath。已为此翻译单元(C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\i686-
- 匿名内部类与lamda表达式