Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将用户定义的类型(结构)与容器类(如set)一起使用 我对C++非常陌生,我尝试使用一个用户定义的类型,一个结构,带有一个容器类,在这个例子中是一个集合。_C++ - Fatal编程技术网

将用户定义的类型(结构)与容器类(如set)一起使用 我对C++非常陌生,我尝试使用一个用户定义的类型,一个结构,带有一个容器类,在这个例子中是一个集合。

将用户定义的类型(结构)与容器类(如set)一起使用 我对C++非常陌生,我尝试使用一个用户定义的类型,一个结构,带有一个容器类,在这个例子中是一个集合。,c++,C++,我知道,要存储用户定义类型的元素(无法使用内置关系运算符进行比较),我需要编写一个比较回调函数并将其传递给Set构造函数。不过,我不知道这是什么语法 我在.cpp文件中定义了以下内容: Set<struct> included; 这就是我所需要做的,还是我在这里遗漏了一些重要的东西,因为代码似乎无法编译,因为类型无法识别 我在这个网站上找到了类似的答案,但我找不到任何具体和明确的情况。任何帮助都将不胜感激。首先,为班级选择一个名字;您可以用不同的方式调用它,pointT,point

我知道,要存储用户定义类型的元素(无法使用内置关系运算符进行比较),我需要编写一个比较回调函数并将其传递给Set构造函数。不过,我不知道这是什么语法

我在.cpp文件中定义了以下内容:

Set<struct> included;
这就是我所需要做的,还是我在这里遗漏了一些重要的东西,因为代码似乎无法编译,因为类型无法识别


我在这个网站上找到了类似的答案,但我找不到任何具体和明确的情况。任何帮助都将不胜感激。

首先,为班级选择一个名字;您可以用不同的方式调用它,
pointT
pointT
,以及
struct
(这甚至不是一个有效的名称)。我就叫它
point
,因为我不喜欢名字上奇怪的装饰

然后您需要决定成员名称:它们是
还是
x
y
?我会选择第一个


要将其存储在
std::set
(或者,通常,要将其用作标准关联容器中的键),您需要
操作符,您可以尝试以下操作:

#include <iostream>
#include <set>

namespace point {
    struct PointT {
        int x;
        int y;
    };

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

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

    bool operator<=(const PointT& p1, const PointT& p2) {
        return p1 < p2  ||  p1 == p2;
    }

    bool operator>(const PointT& p1, const PointT& p2) {
        return p2 < p1;
    }

    bool operator>=(const PointT& p1, const PointT& p2) {
        return p2 < p1  ||  p1 == p2;
    }
}


int main()
{
    using namespace point;
    std::set<PointT> s{ { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }, { 1, 2 } };
    for (const auto& e : s) std::cout << "(" << e.x << "," << e.y << ")" << std::endl;
    return 0;
}
#包括
#包括
名称空间点{
结构点{
int x;
int-y;
};
布尔运算符==(常数点T和p1,常数点T和p2){
返回p1.x用于(常数自动和e:s)std::cout
Set
首先可能应该是
Set
编译过程中会出现什么错误?
std::Set
需要更少的
运算符您的相等运算符是错误的:当它是一个成员函数时,它只需要一个参数,另一个对象就可以与此对象进行比较。此外,还存在区分大小写的错误(代码< > Potot> <代码> >代码> Pox< /Cuth>和<代码> y>代码> @ USE1894:如果类被称为“代码> PooToT”,则必须称之为“代码> PooToT,而不是<代码> PototT//> > C++。C++是一个敏感的例子。谢谢@ Mike Seymour,这非常有帮助。对于PooToT {1,2},B{1,1},你得到了一个AB但是!(B)!
struct point {
    int row;
    int col;

    bool operator<(point const & rhs) {
        return std::tie(row, col) < std::tie(rhs.row, rhs.col);
    }
};
bool operator<(point const & lhs, point const & rhs) {
    return std::tie(lhs.row, lhs.col) < std::tie(rhs.row, rhs.col);
}
bool operator<(point const & lhs, point const & rhs) {
    if (lhs.row < rhs.row) return true;
    if (rhs.row < lhs.row) return false;
    return lhs.col < rhs.col;
}
#include <iostream>
#include <set>

namespace point {
    struct PointT {
        int x;
        int y;
    };

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

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

    bool operator<=(const PointT& p1, const PointT& p2) {
        return p1 < p2  ||  p1 == p2;
    }

    bool operator>(const PointT& p1, const PointT& p2) {
        return p2 < p1;
    }

    bool operator>=(const PointT& p1, const PointT& p2) {
        return p2 < p1  ||  p1 == p2;
    }
}


int main()
{
    using namespace point;
    std::set<PointT> s{ { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }, { 1, 2 } };
    for (const auto& e : s) std::cout << "(" << e.x << "," << e.y << ")" << std::endl;
    return 0;
}