boost bimap 无效迭代器

问题描述 投票:0回答:1

我正在尝试使用以下类型的类的成员:

boost::bimaps::bimap<std::filesystem::path, int> mRowIndexes = {};

使用以下代码时

PB::printDebug("mRowIndexes %d\n", mRowIndexes.size());
if (mRowIndexes.empty())
{
  PB::printDebug("Bimap empty\n");
}
if (mRowIndexes.left.begin() != mRowIndexes.left.end())
{
  PB::printDebug("F1\n");
}
if (mRowIndexes.begin() != mRowIndexes.end())
{
  PB::printDebug("F2\n");
}

我得到以下输出:

[Debug] mRowIndexes 0
[Debug] Bimap empty
[Debug] F1
[Debug] F2

知道迭代器有什么问题吗?为什么迭代器与 .end() 不同? 谢谢!

c++ boost
1个回答
0
投票

显然你还做错了其他事情。这不能从显示的代码中重现:

住在Coliru

#include <boost/bimap.hpp>
#include <filesystem>
#include <iostream>
namespace bm = boost::bimaps;
namespace fs = std::filesystem;

namespace PB {
    inline void printDebug(char const* msg) { std::printf("%s", msg); }
    inline void printDebug(char const* fmt, auto const&... args) { std::printf(fmt, args...); }
} // namespace PB

void probe(auto const& m) {
    PB::printDebug("mRowIndexes %d\n", m.size());
    bool empty = m.empty();
    bool F1    = m.left.begin() != m.left.end();
    bool F2    = m.begin() != m.end();
    PB::printDebug("Bimap: size %d, %s%s%s\n",    //
                   m.size(),                      //
                   empty ? "empty" : "not empty", //
                   F1 ? ", F1" : "",              //
                   F2 ? ", F2" : "");
}

int main() {
    bm::bimap<fs::path, int> mRowIndexes = {};

    probe(mRowIndexes);

    for (int i = 0; auto& e : fs::directory_iterator(".", fs::directory_options::skip_permission_denied))
        mRowIndexes.insert({e.path(), i++});

    probe(mRowIndexes);
}

打印例如

mRowIndexes 0
Bimap: size 0, empty
mRowIndexes 2
Bimap: size 2, not empty, F1, F2

如果没有看到您的其他代码,我们无法提供帮助,但请注意未定义的行为,例如使用

-fsanitize=address,undefined
或类似
valgrind

的工具
© www.soinside.com 2019 - 2024. All rights reserved.