• BitSet构造函数的两种特例


    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);
            }

    它可以用来转换非01字符串,向下面这样:

    cout << bitset<4>(string("xax"), 0, 3, 'x','a')<< endl;

    输出:

    0010
  • 相关阅读:
    c++静态变量和静态函数
    c++抽象类和纯虚函数
    二维矩阵的算法
    JS操作CSS样式
    DOM
    JavaScript学习
    CSS样式表介绍
    HTML 学习整理
    ADO.Net知识总结
    数据库表查询高级 触发器游标等
  • 原文地址:https://www.cnblogs.com/ptolemaeus/p/BitSet_Construct.html
Copyright © 2020-2023  润新知