优化 - 返回过滤的元素作为std:vector< class>或std :: vector< Intrusive_ptr< class>>什么更好,为什么?

发布于 2025-02-04 18:14:34 字数 8 浏览 0 评论 0原文

continue

I have some doubts about optimization regarding storing elements in vectors and getting information from them. Lets see the example

struct Entity
{
public:
    Entity() = default;
    bool isActive = false;
    std::vector<SomeOtherClass> classesThatUseEntity;
}

class Manager
{
public:
Manager() = default;

const std::vector<Entity>& GetOnlyActiveEntity()
{
    std::vector<Entity> value;
    for (const auto& entity : differentSettings)
    {
        if (entity.isActive)
        {
            value.emplace_back();
        }
    }

    return value;
}
private:
std::vector<Entity> differentSettings;

}


I was thinking of maybe using a vector of pointers to return values:
std::vector< boost::intrusive_ptr<Entity>> GetOnlyActiveEntity()
with this, I got some comments from colleagues that the vector of pointers, in this case, is not the smart option because of heap fragmentation, but I am not sure that this comment is legit. What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文