B
B
BitNeBolt2019-08-07 17:15:34
C++ / C#
BitNeBolt, 2019-08-07 17:15:34

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:

  1. User enters data
  2. The already existing array with information is sorted by increasing distance to the point
  3. User can choose how many neighbors to show

But when I run the program, the compiler throws an error in the list.tcc file (not mine) on line 385. It seems to me that something is wrong with the comparator, but I can't figure out what.
Why does this problem occur and how to fix it?
Here is the code:
#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

1 answer(s)
L
lorc, 2019-08-07
@BitNeBolt

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 question

Ask a Question

731 491 924 answers to any question