C++中的聚类示例
Clustering example in C++
我有一个像这样的增加输入向量{0, 1, 3, 5, 6, 7, 9}
并希望像这样聚类输入{{0, 1}, {3}, {5, 6, 7}, {9}}
即仅聚类作为邻居的整数。数据结构std::vector<std::vector<int>> solution(const std::vector<int>& input)
我通常主张不要放弃解决方案,但看起来你陷入了索引和临时向量的泥潭。相反,标准迭代器和算法使这项任务变得轻而易举:
std::vector<std::vector<int>> solution(std::vector<int> const &input) {
std::vector<std::vector<int>> clusters;
// Special-casing to avoid returning {{}} in case of an empty input
if(input.empty())
return clusters;
// Loop-and-a-half, no condition here
for(auto it = begin(input);;) {
// Find the last element of the current cluster
auto const last = std::adjacent_find(
it, end(input),
[](int a, int b) { return b - a > 1; }
);
if(last == end(input)) {
// We reached the end: register the last cluster and return
clusters.emplace_back(it, last);
return clusters;
}
// One past the end of the current cluster
auto const gap = next(last);
// Register the cluster
clusters.emplace_back(it, gap);
// One past the end of a cluster is the beginning of the next one
it = gap;
}
}
在 Coliru 上直播(免费提供蹩脚的输出格式(
相关文章:
- K 均值聚类 R 树提升
- C++中的聚类示例
- 两个数组之间的数据聚类和比较
- TSP 的聚类分析算法
- 如何使用opencv c++的聚类来根据面积和高度对连接的组件进行分类
- 用于聚集层次聚类的OpenCV机器学习库
- K 表示视频所有帧的直方图聚类
- OpenCV 聚类袋的单词 k 表示
- 用自适应MeanShift对特征空间-SURF描述符进行聚类
- 在std::list中合并(将两个项目融合在一起,用融合替换)项目的算法(即破坏性聚类)
- 使用OpenCV的Kmeans聚类中的mahalanobis距离
- (C++)K-Means聚类问题
- 使用Kmean找到具有最高数量元素的聚类
- 利用反演距离进行K-means聚类
- 从给定的点云数据中提取一组特征和聚类数据
- 逻辑错误:平均质心计算不正确,无限执行,'entries '函数 - K 表示一组点的聚类,以C++为单位
- c++: k均值聚类g矩阵
- 改进的k -均值聚类(Ward准则)速度提高
- 基于Flann openv的分层聚类
- 如何为聚类分析创建n维测试数据