Answer the question
In order to leave comments, you need to log in
sort does not work, but does not find an obvious error in the code. How to fix?
I decided to write something similar to the k nearest neighbors algorithm, but I ran into a problem with the sort function.
Here is a brief algorithm of actions:
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
class Finder
{
public:
list<vector<double>> lines; //список, в котором информация
vector<double> input = {0, 0}; //массив, который вводит пользователь
string path; //путь к файлу
Finder(string path, string numbs) //конструктор открывает файл и создает входной массив
{
this->path = path;
int l = numbs.size();
vector<double> input;
for (int i = 0; i < l; i++)
{
string buffer = "";
if (numbs[i] != ' ')
{
buffer += numbs[i];
}
else
{
input.push_back(stod(buffer));
buffer = "";
}
}
this->input = input;
cout << "Initialization was successful" << endl;
}
void GetData() //да, загружать из файла в память обязательно
{
ifstream fin(path);
string line;
while (getline(fin, line))
{
int l = line.size();
string buffer = "";
vector<double> numbers;
for (int i = 0; i < l; i++)
{
if (line[i] != ' ')
{
buffer += line[i];
}
else
{
numbers.push_back(stod(buffer));
buffer = "";
}
}
this->lines.push_back(numbers);
}
fin.close();
cout << lines.size();
}
double GetDistance(vector<double> a) //расчет расстояним между точками
{
int l = this->input.size();
double sum = 0;
for (int i = 0; i < l; i++)
{
double el_i = this->input.at(i);
double el_a = a.at(i);
sum += pow((el_i - el_a), 2);
}
return sqrt(sum);
}
bool compareTo(vector<double> a, vector<double> b) //компаратор
{
return GetDistance(a) < GetDistance(b);
}
void ShowHeigbor() //для пользователя
{
this->lines.sort(this->compareTo);
}
};
int main()
{
string inp;
cout << "Input vector: ";
cin >> inp;
Finder f{"data.txt", inp};
f.GetData();
//f.ShowHeigbor();
}
Answer the question
In order to leave comments, you need to log in
You would at least provide an error message.
The problem is that your comparator is a class method. It would be strange to use it without an object. What is the compiler hinting at?
Here is the corrected version:
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <string>
#include <fstream>
#include <cmath>
#include <functional>
using namespace std;
class Finder
{
public:
list<vector<double>> lines; //список, в котором информация
vector<double> input = {0, 0}; //массив, который вводит пользователь
string path; //путь к файлу
Finder(string path, string numbs) //конструктор открывает файл и создает входной массив
{
this->path = path;
int l = numbs.size();
vector<double> input;
for (int i = 0; i < l; i++)
{
string buffer = "";
if (numbs[i] != ' ')
{
buffer += numbs[i];
}
else
{
input.push_back(stod(buffer));
buffer = "";
}
}
this->input = input;
cout << "Initialization was successful" << endl;
}
void GetData() //да, загружать из файла в память обязательно
{
ifstream fin(path);
string line;
while (getline(fin, line))
{
int l = line.size();
string buffer = "";
vector<double> numbers;
for (int i = 0; i < l; i++)
{
if (line[i] != ' ')
{
buffer += line[i];
}
else
{
numbers.push_back(stod(buffer));
buffer = "";
}
}
this->lines.push_back(numbers);
}
fin.close();
cout << lines.size();
}
double GetDistance(vector<double> a) //расчет расстояним между точками
{
int l = this->input.size();
double sum = 0;
for (int i = 0; i < l; i++)
{
double el_i = this->input.at(i);
double el_a = a.at(i);
sum += pow((el_i - el_a), 2);
}
return sqrt(sum);
}
bool compareTo(vector<double> a, vector<double> b) //компаратор
{
return GetDistance(a) < GetDistance(b);
}
void ShowHeigbor() //для пользователя
{
using namespace std::placeholders; // for _1, _2, _3...
this->lines.sort(std::bind(&Finder::compareTo, this, _1, _2));
}
};
int main()
{
string inp;
cout << "Input vector: ";
cin >> inp;
Finder f{"data.txt", inp};
f.GetData();
//f.ShowHeigbor();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question