【发布时间】:2020-06-21 17:26:08
【问题描述】:
如果有人可以帮助我,我将不胜感激! 我在 C++ 中实现 kmeans 算法。
-我必须分组的点是已知的。
-我想制作 3 个集群。
-但是集群必须在第一次随机实例化。
当我尝试时,出现以下消息,我无法解决。
menge.exe 中 0x00007FF92B484E20 (MengeCore.dll) 处的未处理异常:0xC0000005:访问冲突写入位置 0x0000000000000000。
当我尝试实例化集群时,问题出在队列中。
提前谢谢你!!!
#include <cassert>
#include <limits>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <list>
#include <ostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <stdio.h>
using namespace std;
class Point{
private:
double x;
double y;
public:
Point::Point(double x, double y) : x(x), y(y) {}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
Point::Point() {}
bool Point::operator==(const Point &rhs) const {
return x == rhs.x &&
y == rhs.y;
}
bool Point::operator!=(const Point &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os, const Point &point) {
os << "(" << point.x << "," << point.y << ")";
return os;
}
double getDistance(Point &p) {
return sqrt(pow(p.x - this->x, 2) + pow(p.y - this->y, 2));
}
};
class Cluster {
public:
Point centroid;
vector<int> points;
Cluster::Cluster(const Point ¢roid, const vector<int> &points) : centroid(centroid), points(points) {}
Cluster::Cluster() {
}
string getPoints() {
string s = "";
for (int p : points) {
s += to_string(p + 1) + " ";
}
return s;
}
Cluster::Cluster(const Point ¢roid) : centroid(centroid) {}
};
vector<Point> points{ { 9, 9 },
{ 1, 1 },
{ -1, -1 },
{ 3, 3 },
{ 10, 10 },
{ -2, -2 },
{ 7, 8 },
{ 0.2, 0 },
{-1, 0},
{ 6, 10 } };
vector<Cluster> clusters{};
int main() {
int K=2;
for (int i = 0; i < K; i++)
{
srand(time(NULL));
int RandIndex = rand() % 10; //generates a random number between 0 and 9
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//HERE IS THE PROBLEM
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
clusters[i].centroid = points[RandIndex];
}
return 0;
}
}
【问题讨论】:
-
你是在调试模式下编译吗?它应该告诉您超出范围的访问,而不仅仅是崩溃。
-
终于问题没有解决。是的,出现消息“Standard C++ Libraries out of range”,它显示了 include
的代码 -
有时它显示错误发生在 free.c ( ..\Microsoft Visual Studio 12.0\VC\crt\src\free )
void __cdecl _free_base (void * pBlock) { int retval = 0; if (pBlock == NULL) return; RTCCALLBACK(_RTC_Free_hook, (pBlock, 0)); retval = HeapFree(_crtheap, 0, pBlock); if (retval == 0) { errno = _get_errno_from_oserr(GetLastError()); } }
标签: c++ cluster-analysis k-means