码迷,mamicode.com
首页 > 其他好文 > 详细

BitSet构造函数的两种特例

时间:2016-01-01 17:01:57      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

C++11之后,bitset的构造函数新加了两种形式:

bitset<bits>::bitset (const string& str,
                              string::size_type str_idx, string::size_type str_num,
                              string::charT zero)
bitset<bits>::bitset (const string& str,
                              string::size_type str_idx, string::size_type str_num,
                              string::charT zero, string::charT one)

在vs2015中作如下实现:

template<class _Elem,
        class _Tr,
        class _Alloc>
        explicit bitset(const basic_string<_Elem, _Tr, _Alloc>& _Str,
            _BITSET_SIZE_TYPE _Pos = 0,
            _BITSET_SIZE_TYPE _Count = basic_string<_Elem, _Tr, _Alloc>::npos,
            _Elem _E0 = (_Elem)0,
            _Elem _E1 = (_Elem)1)
        {    // construct from [_Pos, _Pos + _Count) elements in string
        _Construct(_Str, _Pos, _Count, _E0, _E1);
        }

其_Construct如下:

template<class _Elem,
        class _Tr,
        class _Alloc>
        void _Construct(
            const basic_string<_Elem, _Tr, _Alloc>& _Str,
            _BITSET_SIZE_TYPE _Pos,
            _BITSET_SIZE_TYPE _Count,
            _Elem _E0,
            _Elem _E1)
        {    // initialize from [_Pos, _Pos + _Count) elements in string
        if (_Str.size() < _Pos)
            _Xran();    // _Pos off end
        if (_Str.size() - _Pos < _Count)
            _Count = _Str.size() - _Pos;    // trim _Count to size

        typename basic_string<_Elem, _Tr, _Alloc>::size_type _Num;
        for (_Num = 0; _Num < _Count; ++_Num)
            if (!_Tr::eq(_Str[_Pos + _Num], _E0)
                && !_Tr::eq(_Str[_Pos + _Num], _E1))
                _Xinv();

        if (_Bits < _Count)
            _Count = _Bits;    // trim _Count to length of bitset
        _Tidy();

        for (_Pos += _Count, _Num = 0; _Num < _Count; ++_Num)
            if (_Tr::eq(_Str[--_Pos], _E1))
                set(_Num);
        }

在Nicolai M Josuttis的标准库参考书中有这么一句话:Throw invalid_argument if one of the characters is neither ’0’/zero nor ’1’/one.但在vs2015中却可以以’1’/zero和’0’/one的组合作为参数。程序会用0和1分别代替1和0应填充的位置,不足位仍用0填充。不会抛出异常。同时我没在标准文档中找到相关说明。只在草案n4567中找到这么一句:

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.

显然这并没太大关系。

BitSet构造函数的两种特例

标签:

原文地址:http://www.cnblogs.com/ptolemaeus/p/BitSet_Construct.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!