Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
B.Eckel - Thinking in C++, Vol.2, 2nd edition.pdf
Скачиваний:
50
Добавлен:
08.05.2013
Размер:
2.09 Mб
Скачать

implementations. If it’s between 32 and 64, it requires two longs, greater than 64 requires 3 longs, etc. Thus you make the best use of space if you use a bit quantity that fits in an integral number of longs. However, notice there’s no extra overhead for the object – it’s as if you were hand-coding to use a long.

Another clue that bitset is optimized for longs is that there is a to_ulong( ) member function that produces the value of the bitset as an unsigned long. There are no other numerical conversions from bitset, but there is a to_string( ) conversion that produces a string containing ones and zeros, and this can be as long as the actual bitset. However, using bitset<32> may make your life simpler because of to_ulong( ).

There’s still no primitive format for binary values, but the next best thing is supported by bitset: a string of ones and zeros with the least-significant bit (lsb) on the right. The three constructors demonstrated show taking the entire string (the char array is automatically converted to a string), the string starting at character 2, and the string from character 2 through 11. You can write to an ostream from a bitset using operator<< and it comes out as ones and zeros. You can also read from an istream using operator>> (not shown here).

You’ll notice that bitset only has three non-member operators: and (&), or (|) and exclusiveor (^). Each of these create a new bitset as their return value. All of the member operators opt for the more efficient &=, |=, etc. form where a temporary is not created. However, these forms actually change their lvalue (which is a in most of the tests in the above example). To prevent this, I created a temporary to be used as the lvalue by invoking the copy-constructor on a; this is why you see the form BS(a). The result of each test is printed out, and occasionally a is reprinted so you can easily look at it for reference.

The rest of the example should be self-explanatory when you run it; if not you can find the details in your compiler’s documentation or the other documentation mentioned earlier in this chapter.

vector<bool>

vector<bool> is a specialization of the vector template. A normal bool variable requires at least one byte, but since a bool only has two states the ideal implementation of vector<bool> is such that each bool value only requires one bit. This means the iterator must be speciallydefined, and cannot be a bool*.

The bit-manipulation functions for vector<bool> are much more limited than those of bitset. The only member function that was added to those already in vector is flip( ), to invert all the bits; there is no set( ) or reset( ) as in bitset. When you use operator[ ], you get back an object of type vector<bool>::reference, which also has a flip( ) to invert that individual bit.

//: C04:VectorOfBool.cpp

// Demonstrate the vector<bool> specialization #include <iostream>

#include <sstream> #include <vector>

Chapter 15: Multiple Inheritance

230

#include <bitset> #include <iterator> using namespace std;

int main() {

vector<bool> vb(10, true); vector<bool>::iterator it;

for(it = vb.begin(); it != vb.end(); it++) cout << *it;

cout << endl; vb.push_back(false);

ostream_iterator<bool> out(cout, ""); copy(vb.begin(), vb.end(), out);

cout << endl;

bool ab[] = { true, false, false, true, true, true, true, false, false, true };

//There's a similar constructor: vb.assign(ab, ab + sizeof(ab)/sizeof(bool)); copy(vb.begin(), vb.end(), out);

cout << endl;

vb.flip(); // Flip all bits copy(vb.begin(), vb.end(), out); cout << endl;

for(int i = 0; i < vb.size(); i++) vb[i] = 0; // (Equivalent to "false")

vb[4] = true; vb[5] = 1;

vb[7].flip(); // Invert one bit copy(vb.begin(), vb.end(), out); cout << endl;

//Convert to a bitset:

ostringstream os; copy(vb.begin(), vb.end(),

ostream_iterator<bool>(os, "")); bitset<10> bs(os.str());

cout << "Bitset:\n" << bs << endl; } ///:~

The last part of this example takes a vector<bool> and converts it to a bitset by first turning it into a string of ones and zeros. Of course, you must know the size of the bitset at compiletime. You can see that this conversion is not the kind of operation you’ll want to do on a regular basis.

Chapter 15: Multiple Inheritance

231

Соседние файлы в предмете Численные методы
  • #
    08.05.20133.99 Mб22A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20135.91 Mб24B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136.09 Mб17D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20133.85 Mб15DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20131.84 Mб12E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu