如何将 std::vector<bool> 转储到二进制文件中?

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

我编写工具来转储和加载二进制文件中的常见对象。在第一个快速实现中,我为

std::vector<bool>
编写了以下代码。它可以工作,但显然没有在内存中进行优化。

template <>
void binary_write(std::ofstream& fout, const std::vector<bool>& x)
{
    std::size_t n = x.size();
    fout.write((const char*)&n, sizeof(std::size_t));
    for(std::size_t i = 0; i < n; ++i)
    {
        bool xati = x.at(i);
        binary_write(fout, xati);
    }
}

template <>
void binary_read(std::ifstream& fin, std::vector<bool>& x)
{
    std::size_t n;
    fin.read((char*)&n, sizeof(std::size_t));
    x.resize(n);
    for(std::size_t i = 0; i < n; ++i)
    {
        bool xati;
        binary_read(fin, xati);
        x.at(i) = xati;
    }
}

如何复制流中

std::vector<bool>
的内存?

注意: 我不想用其他东西替换

std::vector<bool>

c++ boolean stdvector
3个回答
3
投票

回答我自己的问题,目前被验证为最佳答案,但如果有人提供更好的东西,它可以改变。

执行此操作的方法如下。它需要访问每个值,但它有效。

template <>
void binary_write(std::ofstream& fout, const std::vector<bool>& x)
{
    std::vector<bool>::size_type n = x.size();
    fout.write((const char*)&n, sizeof(std::vector<bool>::size_type));
    for(std::vector<bool>::size_type i = 0; i < n;)
    {
        unsigned char aggr = 0;
        for(unsigned char mask = 1; mask > 0 && i < n; ++i, mask <<= 1)
            if(x.at(i))
                aggr |= mask;
        fout.write((const char*)&aggr, sizeof(unsigned char));
    }
}

template <>
void binary_read(std::ifstream& fin, std::vector<bool>& x)
{
    std::vector<bool>::size_type n;
    fin.read((char*)&n, sizeof(std::vector<bool>::size_type));
    x.resize(n);
    for(std::vector<bool>::size_type i = 0; i < n;)
    {
        unsigned char aggr;
        fin.read((char*)&aggr, sizeof(unsigned char));
        for(unsigned char mask = 1; mask > 0 && i < n; ++i, mask <<= 1)
            x.at(i) = aggr & mask;
    }
}

1
投票

抱歉,但答案是你无法便携地执行此操作。

要以不可移植的方式执行此操作,您可以为

vector<bool>
编写一个特定于标准库实现的迭代器的函数。

如果幸运的话,相关字段将在迭代器内公开,因此您不必将私有更改为公共。


0
投票

只需使用我的比特流:
https://github.com/redmms/FineStream
它将把向量写成位:

#include "finestream/finestream.h" // or "import finestream;"
fsm::ofinestream stream("output.txt");
vector <bool> v{true, false, true, false};
stream << v;

然后,如果您需要重读:

#include "finestream/finestream.h" // or "import finestream;"
fsm::ifinestream stream("output.txt");
vector <bool> v(4);
stream >> v;

附注如果您有兴趣:这里文件中的最后 4 位将为零,要知道这些位的数量,请使用stream.ExtraZerosN()

© www.soinside.com 2019 - 2024. All rights reserved.