level 11
string str("110111111100000000000111111000001111111100000011110000110101");
bitset<16> bitvec(str);
bitvec输出为:1101111111000000
这个怎么不是从str的末尾开始初始化bitvec
c++ primer 4th原文:
The numbering conventions of strings and bitsets are inversely related: The
rightmost character in the stringthe one with the highest subscriptis used to
initialize the low-order bit in the bitsetthe bit with subscript 0.
2012年05月08日 12点05分
1
level 12
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main()
{
string str("1111111100000000");
bitset<16> bs(str);
cout<<str<<endl;
cout<<bs<<endl;
return 0;
}
//按照书上开说应该反过来才对
2012年05月08日 12点05分
5
level 11
按照编译器给出的结果推断出它的处理过程:
首先判断str的字符个数是否和位集的的位数相等,如果小于,则在str的左端补0凑够相等,然后把str表示的01序列正常序初始化位集(效果等效于反向初始化),如果等于,则直接按str的01正常序初始化位集,如果大于则从str的左端按正常序初始化位集,也就是表现为我举得这种情况.
2012年05月08日 13点05分
6
level 15
ISO C++03
23.3.5.1
template <class charT, class traits, class Allocator>
explicit
bitset(const basic_string<charT, traits, Allocator>& str,
typename basic_string<charT, traits, Allocator>::size_type pos = 0,
typename basic_string<charT, traits, Allocator>::size_type n =
basic_string<charT, traits, Allocator>::npos);
3 Requires: pos <= str.size().
4 Throws: out_of_range if pos > str.size().
5 Effects: Determines the effective length rlen of the initializing string as the smaller of n and str.size() - pos. The function then throws invalid_argument if any of the rlen characters in str beginning at position pos is other than 0 or 1.
Otherwise, the function constructs an object of class bitset<N>, initializing the first M bit positions to values determined from the corresponding characters in the string str. M is the smaller of N and rlen.
ISO C++11
20.5.1
template <class charT, class traits, class Allocator>
explicit
bitset(const basic_string<charT, traits, Allocator>& str,
typename basic_string<charT, traits, Allocator>::size_type pos = 0,
typename basic_string<charT, traits, Allocator>::size_type n =
basic_string<charT, traits, Allocator>::npos,
charT zero = charT(’0’), charT one = charT(’1’));
3 Requires: pos <= str.size().
4 Throws: out_of_range if pos > str.size().
5 Effects: Determines the effective length rlen of the initializing string as the smaller of n and str.size() - pos.
The function then throws invalid_argument if any of the rlen characters in str beginning at position pos is other than zero or one. The function uses traits::eq() to compare the character values.
Otherwise, the function constructs an object of class bitset<N>, initializing the first M bit positions to values determined from the corresponding characters in the string str. M is the smaller of N and rlen.
2012年05月08日 13点05分
7