c++ - 对 vector 中的元素进行排序 C++

标签 c++

我有一个文本文件points.txt

由 Points/NotPoints , X , Y 格式化

Points, [3, 3]
NotPoints, [0, 0]
Points, [4, 4]
NotPoints, [0, 0]

我想做的是从文本文件中检索 X 值并将其放入 vector 中,然后 按 ASC 或 DSC 顺序对 X 值进行排序。

我找到了一个类似的帖子来回答我的问题

www.stackoverflow.com/questions/7102246/sorting-a-vector-of-objects-in-c#=

但问题是,当我尝试在 main.cpp 中执行此操作时,

sort(pointsVector.begin(), pointsVector.end(),sortOrder()) ;

它显示错误,在类型点的多个基类子对象中找到非静态成员 sortOrder。

有关我的实现,请参阅下面的代码。

我的任务的一些要求,

I must initialize int x as protected variable as there are other class inheriting it , 
I can't use struct therefore I am using get and set

代码

积分.h

#ifndef __Points__Points__
#define __Points__Points__
#include <iostream>

class Points {
    friend class main;

protected:
     int x;
     int y;

private:
     friend bool sortOrder (const Points & rhs, const Points & lhs);
public:
    Points() {
        x=0;
        y=0;
    };//default Constructor

    Points (int x , int y);
    int getX();
    int getY();

    void setX(int x);
    void setY(int y);

    bool sortOrder (const Point2D & rhs, const Point2D & lhs) {
        return lhs.x < rhs.x;
    }
};
#endif 

积分.cpp

#include "Points.h"

Points::Points(int x , int y) {
    setX(x);
    setY(y);

}

int Points::getX() {
    return x;
}

int Points::getY() {
    return y;
}

void Points::setX(int x)   {
    this->x = x;
}

void Points::setY(int y)   {
    this->y = y;
}

主要.h

 #ifndef Points_main_h
 #define Points_main_h
 #include <iostream>
 #include "Points.h"
 #include <vector>

class main : public Points  {

private:
    int input;
    std::string type,line;
    std::string pointX, pointY;
    std::vector<Points> pointsVector;
 public:
    void mainMenu();
    void getPoint2DRecordsFromFile();
};
#endif 

主要.cpp

 #include <iostream>
 #include "main.h"
 #include "Points.h"
 #include <fstream>
 #include <sstream>
 #include <string>
 #include <vector>
 #include <stdio.h>
 #include <algorithm>

class main outputMethod;

void main::getRecordsFromFile() {

    std::ifstream readFile;
    //put your text file name inside the ""
    readFile.open("points.txt");
    if (!readFile.is_open()) {
        std::cout <<" "<< std::endl;
        std::cout << "File Not Found!" << std::endl;
        std::cout <<" "<< std::endl;
    }
    else    {
        while (readFile.good()) {
            while(getline(readFile,line))   {
                std::stringstream iss(line);
                getline(iss, type,',');
                if(type == "Points")  {

                getline(iss, pointX,'[');
                getline(iss, pointX,',');

                getline(iss, pointY ,' ');
                getline(iss, pointY,']');

                Points pointsConsturctor(std::stoi(pointX),std::stoi(pointY));
                pointsVector.push_back(pointsConsturctor);
                }
            }
        }
    }
    readFile.close();

    /*error!
    Non static members sortOrder found in multiple base-class subobjects of type Points*/
    sort(pointsVector.begin(), pointsVector.end(),sortOrder());

    for (int i = 0; i<pointsVector.size(); i++) {
        std::cout << pointsVector[i].getX() << "," << pointsVector[i].getY() << std::endl;
    }  
}


void main::mainMenu() {

    std::cout << "1. Read data " << std::endl;
    std::cout << "2. sort " << std::endl;
    std::cin >> input;

    switch ( input ) {
        case 1:
            std::cout << "In Progress!" << std::endl;
            break;
        case 2:
            getRecordsFromFile();
            break;
        default:
            break;
        std::cin.get();
    }

}

int main()  {
    outputMethod.mainMenu();
}

最佳答案

请注意,在 Points 类中,您定义了两次 sortOrder(作为友元函数和方法):

friend bool sortOrder (const Points & rhs, const Points & lhs);

bool sortOrder (const Point2D & rhs, const Point2D & lhs) {
    return lhs.x < rhs.x;
}

关于c++ - 对 vector 中的元素进行排序 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19902862/

相关文章:

c++ - 在 C++ 类的 UML 类图中添加构造函数和析构函数

c++ - do-while循环多条件求值C++

c++ - 为什么这在 C++ 中抛出 "CryptoMaterial: this object contains invalid values",但在 python 中工作正常

c++ - 如果有 constexpr if 语句,为什么没有其他 constexpr 语句呢?

c++ - "separating module interface/implementation unit in a different source file"和使用 "private module fragment"之间的权衡是什么

c++ - 如何为类 unique_ptr 创建运算符->

c++ - `type` 和 `const type` 的类型特征

c++ - 用对初始化 vector

c++ - 如何在终端上以星号(*)的形式显示输入密码

c++ - 避免 set 创建 Comparator 对象的实际拷贝是否合法