zoukankan      html  css  js  c++  java
  • C++标准库中bitset类的用法

    bitset是C++标准库中的一个类,用来处理位操作及运算。可以通过下标访问。bitset在内存中以倒序存储,如存入的数据为1000,用for(int i=0;i

    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <bitset>
    using namespace std;
    int main()
    {
        bitset<8>bint(1);//16位的二进制数据
        for (int i = 0; i < bint.size(); i++)
            cout << bint[i];
        cout << endl;
        cout << "输入初始状态:" << endl;
        cin >> bint;
        cout<<"大小:"<<bint.size()<<endl;
        cout <<"all:" <<bint.all()<<endl;//全为1返回1,否则返回0
        cout << "any:" << bint.any()<<endl;//全为0返回0,否则返回1
        cout << "count:"<<bint.count() << endl;//返回1的个数
        //cout << "flip:" << bint.flip() << endl;//全部取反
        for (int i = 0; i < bint.size(); i++)
            cout << bint[i];
        cout << endl;
        cout << "flip(n=1),将bint[1]取反:" << bint.flip(1) << endl;
        //返回它转换为unsigned long的结果,如果超出范围则报错
        cout << "to_ulong:" << bint.to_ulong() << endl;
        //返回它转换为unsigned long long的结果,如果超出范围则报错
        cout << "to_ullong:" << bint.to_ulong() << endl;
        //转换为string类型
        cout << "to_string:" << bint.to_string() << endl;
    
        cout << "set:" << bint.set() << endl;//全部置为1
    
        cout << "set(n):" << bint.set(2) << endl;//把bint[2]位置为1
    
        cout << "reset():" << bint.reset() << endl;//全部置为0
    
        cout << "none():"<<bint.none() << endl;//不存在1则返回1
        cout << "hash:" << bint.hash() << endl;
        /*for (int i=0;i<bint.size();i++)
        {
            cout << bint[i];
        }*/
    
        system("pause");
        return 0;
    }

    这里写图片描述

  • 相关阅读:
    十八、分页查询
    十七、oracle的权限和角色
    十六、同义词(synonyms)
    十五、序列
    十四、禁用与启动约束
    十三、修改表时添加约束
    十二、约束
    十一、修改表格
    十、事务(Transaction)
    Windows 远程连接后,自动断开,所有程序都自动关闭(待验证,待更新)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302384.html
Copyright © 2011-2022 走看看