c++ - 使用 STL 查找容器是否包含重复值的有效方法?
问题描述
我很惊讶 STL 的算法部分没有 std::is_unique 。
我已经用 std::unique_copy 解决了这个值,但我发现它很差,因为当我真的只需要知道是否有重复值时它会执行内存分配。
我知道替代解决方案包括在插入 std::set 时进行迭代以查找重复但相同的故事。
是否有一种高效、优雅(即:一个只使用标准算法的班轮)解决方案?
std::vector<std::string> elements = { "foo", "bar", "foo"} ;
std::vector<std::string> uniquified;
std::unique_copy(cbegin(elements),cend(elements), std::back_inserter(uniquified));
if (uniquified.size() != elements.size())
return "Elements are not unique";
解决方案
推荐阅读
- node.js - 在 NodeJS 中运行 R 脚本时产生 Rscript ENOENT 错误
- node.js - vue-cli 项目登录不优雅?
- r - 如何使用 expand.grid 值在 R 中运行 Ranger 的各种模型超参数组合
- c# - ASP.Netcore MVC - Razor 使用 efcore 按月/年行显示类别的费用总额和每月的总费用
- xamarin - 我如何检查复选框的值
- typescript - 我想使用材料表中的选择,但它不起作用 - 材料表选择
- sql - Postgres 可序列化事务未按预期工作
- reactjs - Ant 设计类型脚本输入字段验证不起作用
- reactjs - 你如何将 React-bootstrap 与 Typescript 一起使用?
- android - 此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 6.0 不兼容。- 反应原生