转载自:C++ Primer, Fifth Edition
The indexing conventions of strings and bitsets are inversely related:The character in the string with the highest subscript (the rightmostcharacter) is used to initialize the low-order bit in the bitset (the bit withsubscript 0). When you initialize a bitset from a string, it is essential to remember this difference.
For example:
string str("1111111000000011001101");
bitset<32> bitvec5(str, 5, 4); // four bits starting at str[5], 1100
bitset<32> bitvec6(str, str.size()-4); // use last four characters
We can view these initializations as
Here bitvec5 is initialized by the substring in str starting at str[5] and continuing for four positions. As usual, the right-most character of the substring represents the lowest-order bit. Thus, bitvec5 is initialized with bit positions 3 through 0 set to 1100 and the remaining bits set to 0. The initializer for bitvec6 passes a string and a starting point, so bitvec6 is initialized from the characters in str starting four from the end of str. The remainder of the bits in bitvec6 are initialized to zero.