簡體   English   中英

從std :: vector獲取字節<bool>

[英]Get bytes from a std::vector<bool>

我有類似下面的內容,並在用任意數量的位填充后,我需要將字節寫入文件。 我沒有看到這樣做的方法,它看起來很有用,所以我必須遺漏一些東西。 有任何想法嗎?

std::vector<bool> a;

a.push_back(true);
a.push_back(false);
a.push_back(false);
a.push_back(true);

a.push_back(false);
a.push_back(true);
a.push_back(true);
a.push_back(false);

std::vector <bool>實際上不包含bools(iebytes),它包含位! 這主要是一個missfeature,建議你使用std::deque <bool> ,而不是這個“功能”。

如果您希望存儲是連續的,請使用std::vector <char>

嘗試這個

void WriteOut(fstream& stream, const vector<bool>& data) {
  for (vector<bool>::const_iterator it = data.begin(); it != data.end(); it++) {
    stream << *it;
  }
}

bool通常是一個字節 - 您可以使用vector :: iterator簡單地迭代向量,並以這種方式訪問​​每個值。

std::vector<bool> a;

a.push_back(true);
a.push_back(false);

for(std::vector<bool>::iterator iter = a.begin(); iter != a.end(); ++iter)
{
    std::cout << *iter << std::endl;
}

將遍歷每個bool,並將其打印到命令行。 打印到文件相對簡單。

做這樣的事情

std::vector<bool> a;
a.push_back(true);
a.push_back(false);
//...
for (auto it = a.begin(); it != a.end();) // see 0x for meaning of auto
{
    unsigned b = 0;
    for (int i = 0; i < 8*sizeof(b); ++i)
    {
        b |= (*it & 1) << (8*sizeof(b) - 1 - i);
        ++it;
    }
    // flush 'b'
}

所以,你最終要做的是將大塊的位組合在一起,在這里我選擇將位分組為本機整數(這對於目標平台來說是最佳的)。 我不在這里查看索引,但這是你必須要做的事情。 我要做的是,我會檢查我可以先提取多少個完整的塊,然后執行該操作,然后處理剩余的剩余部分。

另外,請注意我從左到右填充位(假設目標體系結構是little-endian),這意味着首先填充msb。

如果您正在進行位操作和類似的操作,請為您找出一個包裝方案並讓它成為您的數據結構。 std :: bit_vector,std :: vector或:: dequeue並不重要。 巧妙地將您的位打包到目標平台的本機整數類型中,這將提供最佳性能。

為什么不使用STL bitset呢? 它具有將bitset值轉換為等效long值或字符串表示的特定方法:

http://www.cppreference.com/wiki/stl/bitset/start

實際上你可以這樣做:

copy(yourvector.begin(), yourvector.end(), std::ostreambuf_iterator<char>(outputstream));

我不記得是否需要打包std::vector<bool> ,很可能不是。 如果是,您可以訪問其:: data()成員以訪問原始字節。

在查看上面建議的解決方案之后,我最終只編寫了一個完全正常工作的函數。

  // Count number of bytes needed to contain the bits
  // and then copy 8 bit block as bytes.

  void writeAsBytes(const vector<bool> & inBits, vector<uint8_t> & outBytes) {
    int bitOffset = 0;
    const int maxBitOffset = (int) inBits.size();

    const bool emitMSB = true;

    int numBytes = (int)inBits.size() / 8;
    if ((inBits.size() % 8) != 0) {
      numBytes += 1;
    }

    for (int bytei = 0; bytei < numBytes; bytei++) {
      // Consume next 8 bits

      uint8_t byteVal = 0;

      for (int biti = 0; biti < 8; biti++ ) {
        if (bitOffset >= maxBitOffset) {
          break;
        }

        bool bit = inBits[bitOffset++];

        // Flush 8 bits to backing array of bytes.
        // Note that bits can be written as either
        // LSB first (reversed) or MSB first (not reversed).

        if (emitMSB) {
          byteVal |= (bit << (7 - biti));
        } else {
          byteVal |= (bit << biti);
        }
      }

      outBytes.push_back(byteVal);
    }
  }

首先,您想使用bit_vector而不是vector。

其次,使用bit_vector或vector無法完全滿足您的需求。 它們被設計為集合,它們的基礎格式對您是隱藏的(因此它可能決定將每個bool存儲為單個字節而不是每個字節打包為8位)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM