• 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
  • 相关阅读:
    Linux免密登录
    HDFS shell 常用命令
    zabbix4.4图表中文显示乱码解决办法
    安装zabbix-agent
    安装配置zabbix4.4
    elasticsearch插件sql安装
    dedecms调用头部文件 dede:include时页面出现一行空白的解决方案
    DedeCMS <=5.7 SP2 file_class.php 任意文件上传漏洞
    DedeCMS后台文件任意上传漏洞media_add.php的修改方法
    织梦DEDECMS任意文件上传漏洞与注入漏洞修复方法
  • 原文地址:https://www.cnblogs.com/ptolemaeus/p/BitSet_Construct.html
Copyright © 2020-2023  润新知