-7

我不断收到实例化错误。尝试修复它,无论如何,我似乎无法实现 SET。尝试使用向量,但我不知道如何正确实现相等运算符并且也失败了。

起初我使用向量,但在尝试保持 x 和 y 值唯一时很难使用。意思是,我会阅读一个数据文本文件。 Point1, [3, 10] Point2, [10, 10]

因此点 1 将与点 2 转到不同的向量,然后分别存储数据。但不知何故,在文本文件中有 Point1, [3, 10] 的重复值。我不想插入另一个相同的记录并希望它们是独一无二的。所以我尝试使用 set 但我无法真正插入数据。

错误是:

在成员函数 'bool std::les<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]': STL_TREE.h:980: 从 'std::pair::iterator 实例化, bool> std::_Rb_tree<_Key,.... STL_SET.h:307: 实例化自 'std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_interator, bool> std::设置..... main.cpp:141:从这里实例化stl_function.h:227:错误:'_ x < _y'中的'operator<'不匹配

点.h

#ifndef POINT_H
#define POINT_H 

#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
#include <set>
#include <string.h>
#include <string>
#include <stdio.h>

using namespace std;

class Point
{
    friend ostream& operator<<(ostream&, const Point&);

    protected:
        int x, y;

    public:
        //c'tor
        Point(int xx, int yy);

        //setters
        void setX(int x1);
        void setY(int y1);

        //getters
        int getX() const {return x;};
        int getY() const {return y;};
};

Point::Point(int xx, int yy) : x(xx), y(yy)
{

}

void Point::setX(int x1)
{
    x=x1;
}

void Point::setY(int y1)
{
    y=y1;
}

#endif

主文件

#include "Point.h"
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <set>
#include <math.h>
#include <set>
#include <string.h>
#include <string>
#include <stdio.h>
#include <map>
#include <algorithm>
#include <map>

using namespace std;

class Main
{
    public: 
        void mainMenu();
        void stringToUpper(string &s);
};

void stringToUpper(string &s)
{
    for(unsigned int l=0; l < s.length(); l++)
    {
        s[l] = toupper(s[l]);    
    }
}

void Main::mainMenu()
{
    cout<<"1)   type filename and read data"<<endl;
    cout<<"2)   View data"<<endl;    
    cout<<"Q)   Enter 'Q' to quit"<<endl<<endl;
}

int main()
{
    Main main;
    char menuChoice;
    bool quit=false;
    int count = 0;

    set<Point> p;

    while ( !quit)
    {
        main.mainMenu();
        cout << "Please enter your choice : ";
        cin>>menuChoice;
        menuChoice = toupper(menuChoice);

        switch(menuChoice)
        {
            case '1':
                {
                    string line, name;
                    char filename[50];
                    int x,y;

                    cout << "Please enter filename : ";
                    cin >> filename;

                    ifstream myfile(filename);
                    if (myfile.is_open())
                    {
                        while ( myfile.good() )
                        {
                            getline(myfile, line);
                            {      
                                for (unsigned int i = 0; i < line.size(); ++i) 
                                {
                                    if (line[i] == '[' || line[i] == ']' || line[i] == ',') 
                                    {
                                        line[i] = ' ';
                                    }
                                    istringstream in(line);
                                    in >> name >> x >> y;                                  
                                }
                                if(name == "Point")
                                {
                                p.insert(Point(x,y))    }                        
                            }                                
                            count++;
                        }    
                        myfile.close();
                        cout << count;
                    }
                    else cout<< "Unable to open file";
                }
                break;                             
            case '2':
                {
                cout<<"view data"<<endl;
                }                   
            case 'Q':
                cout<<"You have chosen to quit!"<<endl;
                quit=true;
                exit(0);
            default:
                cout<<"Invalid entry"<<endl<<endl;
                break;
        }
    }
}
4

2 回答 2

2

如果您想将 Point 放入一个集合中,那么 set 需要一种将一个 Point 与另一个进行比较的方法,以便将它们按顺序排列。你必须声明这个函数

bool operator<(const Point& p1, const Point& p2)
{
    ...
}

这样做,您将能够实例化std::set<Point>

通常的实现是

bool operator<(const Point& p1, const Point& p2)
{
    return p1.x < p2.x || p1.x == p2.x && p1.y < p2.y;
}

operator<只要您遵守严格的弱排序规则,顺序是什么并不重要(我假设否则您可能已经意识到您需要更早) 。

于 2012-11-13T13:21:38.427 回答
1

std::set需要一个比较函数或函子来比较两个Points并应用严格的弱排序。Point您可以通过提供小于比较函数来实现这一点。

您可以使用以下方法轻松实现std::tie

#include <tuple> // for std::tie

class Point
{
    // rest as before
    friend bool operator<(const Point& lhs, const Point& rhs)
    {
      return std::tie<lhs.x, lhs.y> < std::tie<rhs.x, rhs.y>;
    }
};
于 2012-11-13T13:23:10.237 回答