小编Jol*_*luk的帖子

std::unique 不能按预期与结构一起工作

我目前正在开发一款 2D 游戏,其中关卡由边缘定义:

struct Edge
{
    vec2int start;
    vec2int end;
}
Run Code Online (Sandbox Code Playgroud)

该结构vec2int是一个具有 x、y 坐标的向量,并且重载了所有需要的运算符(在本例中operator==)。由于存储网格内部边缘的数据结构,网格内部的不同单元格中可能存在重复的边缘。当将它们重新组合成一个时,std::vector<Edge>我试图像这样摆脱它们:

auto it = std::unique(
    edges.begin(),
    edges.end(),
    [&](const Edge& e1, const Edge& e2)
    {
        return e1.start == e2.start && e1.end == e2.end;
    });

edges.resize(std::distance(edges.begin(), it));
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,这只会删除一些(或没有)重复边缘。我不知道为什么。我有什么遗漏的吗std::unique

代码:

#include <algorithm>
#include <iostream>
#include <vector>

template<class T>
struct v2d_generic
{
    T x = 0;
    T y = 0;
    
    bool operator==(const v2d_generic& rhs) const
    {
        return (this->x == rhs.x && this->y == …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

1
推荐指数
1
解决办法
122
查看次数

继承:如果默认为 private,为什么这两个示例的工作方式不同?

我正在处理 C++ 中的继承问题。据我所知,如果您不指定,B 将始终从 A 继承 private。

那么为什么这段代码可以工作:

struct A {};
struct B : A {};

int main(void)
{
    A b = B();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但这会产生“A 是 B 的不可访问的基点”错误:

struct A {};
struct B : private A {};

int main(void)
{
    A b = B();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望它们是一样的?

c++ inheritance

0
推荐指数
2
解决办法
76
查看次数

标签 统计

c++ ×2

c++17 ×1

inheritance ×1