Set and bitset

  1. 关于set,必须说明的是set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。C++?STL中标准关联容器set,?multiset,?map,?multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB(Red-Black?Tree)RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。??
  2. 注意:??
  3. 1set中的元素都是排好序的??
  4. 2set集合中没有重复的元素??
  5. 来自?<https://blog.csdn.net/byn12345/article/details/79523516>???
  6. ????
  7. bitset??
  8. C++?bitset??bitset?头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间。??
  9. ????
  10. bitset<4>?bitset1;  //无参构造,长度为4,默认每一位为0??
  11. ????
  12. ????bitset<8>?bitset2(12);  //长度为8,二进制保存,前面用0补充??
  13. ????
  14. ????string?s?=?"100101";??
  15. ????bitset<10>?bitset3(s);  //长度为10,前面用0补充??
  16. ???????
  17. ????char?s2[]?=?"10101";??
  18. ????bitset<13>?bitset4(s2);  //长度为13,前面用0补充??
  19. ????
  20. ????cout?<<?bitset1?<<?endl;  //0000??
  21. ????cout?<<?bitset2?<<?endl;  //00001100??
  22. ????cout?<<?bitset3?<<?endl;  //0000100101??
  23. ????cout?<<?bitset4?<<?endl;  //0000000010101??
  24. ????
  25. 注意:??
  26. 用字符串构造时,字符串只能包含?‘0‘??‘1‘?,否则会抛出异常。??
  27. 构造时,需在<>中表明bitset?的大小(size)??
  28. 在进行有参构造时,若参数的二进制表示比bitsetsize小,则在前面用0补充(如上面的栗子);若比bitsize大,参数为整数时取后面部分,参数为字符串时取前面部分(如下面栗子)??
  29. ??????
  30. ???bitset<2>?bitset1(12);  //12的二进制为1100(长度为4),但bitset1size=2,只取后面部分,即00??
  31. ????
  32. ????string?s?=?"100101";  ??
  33. ????bitset<4>?bitset2(s);  //ssize=6,而bitsetsize=4,只取前面部分,即1001??
  34. ????
  35. ????char?s2[]?=?"11101";??
  36. ????bitset<4>?bitset3(s2);  //bitset2同理,只取前面部分,即1110??
  37. ????
  38. ????cout?<<?bitset1?<<?endl;  //00??
  39. ????cout?<<?bitset2?<<?endl;  //1001??
  40. ????cout?<<?bitset3?<<?endl;  //1110??
  41. ????
  42. 可用的操作符??
  43. bitset对于二进制有位操作符,具体如下??
  44. bitset<4>?foo?(string("1001"));??
  45. ????bitset<4>?bar?(string("0011"));??
  46. ????
  47. ????cout?<<?(foo^=bar)?<<?endl;???????//?1010?(foobar按位异或后赋值给foo)??
  48. ????cout?<<?(foo&=bar)?<<?endl;???????//?0010?(按位与后赋值给foo)??
  49. ????cout?<<?(foo|=bar)?<<?endl;???????//?0011?(按位或后赋值给foo)??
  50. ????
  51. ????cout?<<?(foo<<=2)?<<?endl;????????//?1100?(左移2位,低位补0,有自身赋值)??
  52. ????cout?<<?(foo>>=1)?<<?endl;????????//?0110?(右移1位,高位补0,有自身赋值)??
  53. ????
  54. ????cout?<<?(~bar)?<<?endl;???????????//?1100?(按位取反)??
  55. ????cout?<<?(bar<<1)?<<?endl;?????????//?0110?(左移,不赋值)??
  56. ????cout?<<?(bar>>1)?<<?endl;?????????//?0001?(右移,不赋值)??
  57. ????
  58. ????cout?<<?(foo==bar)?<<?endl;???????//?false?(0110==0011false)??
  59. ????cout?<<?(foo!=bar)?<<?endl;???????//?true??(0110!=0011true)??
  60. ????
  61. ????cout?<<?(foo&bar)?<<?endl;????????//?0010?(按位与,不赋值)??
  62. ????cout?<<?(foo|bar)?<<?endl;????????//?0111?(按位或,不赋值)??
  63. ????cout?<<?(foo^bar)?<<?endl;????????//?0101?(按位异或,不赋值)??
  64. ????
  65. 此外,可以通过?[?]?访问元素(类似数组),注意最低位下标为0,如下:??
  66. ????bitset<4>?foo?("1011");??
  67. ???????
  68. ????cout?<<?foo[0]?<<?endl;  //1??
  69. ????cout?<<?foo[1]?<<?endl;  //1??
  70. ????cout?<<?foo[2]?<<?endl;  //0??
  71. ????
  72. 可用函数??
  73. bitset还支持一些有意思的函数,比如:??
  74. bitset<8>?foo?("10011011");??
  75. ????
  76. ????cout?<<?foo.count()?<<?endl;  //5  (count函数用来求bitset1的位数,foo中共有5个1??
  77. ????cout?<<?foo.size()?<<?endl;  ?//8  (size函数用来求bitset的大小,一共有8位??
  78. ????
  79. ????cout?<<?foo.test(0)?<<?endl;  //true  (test函数用来查下标处的元素是0还是1,并返回falsetrue,此处foo[0]为1,返回true??
  80. ????cout?<<?foo.test(2)?<<?endl;  //false  (同理,foo[2]为0,返回false??
  81. ????
  82. ????cout?<<?foo.any()?<<?endl;  //true  (any函数检查bitset中是否有1??
  83. ????cout?<<?foo.none()?<<?endl;  //false  (none函数检查bitset中是否没有1??
  84. ????cout?<<?foo.all()?<<?endl;  //false  (all函数检查bitset中是全部为1??
  85. 补充说明一下:test函数会对下标越界作出检查,而通过?[?]?访问元素却不会经过下标检查,所以,在两种方式通用的情况下,选择test函数更安全一些??
  86. 另外,含有一些函数:??
  87. ????bitset<8>?foo?("10011011");??
  88. ????cout?<<?foo.flip(2)?<<?endl;  //10011111  (flip函数传参数时,用于将参数位取反,本行代码将foo下标2处"反转",即0变1,1变0??
  89. ????cout?<<?foo.flip()?<<?endl;  ?//01100000  (flip函数不指定参数时,将bitset每一位全部取反??
  90. ????
  91. ????cout?<<?foo.set()?<<?endl;    //11111111  (set函数不指定参数时,将bitset的每一位全部置为1??
  92. ????cout?<<?foo.set(3,0)?<<?endl;  //11110111  (set函数指定两位参数时,将第一参数位的元素置为第二参数的值,本行对foo的操作相当于foo[3]=0??
  93. ????cout?<<?foo.set(3)?<<?endl;  ??//11111111  (set函数只有一个参数时,将参数下标处置为1??
  94. ????
  95. ????cout?<<?foo.reset(4)?<<?endl;  //11101111  (reset函数传一个参数时将参数下标处置为0??
  96. ????cout?<<?foo.reset()?<<?endl;  ?//00000000  (reset函数不传参数时将bitset的每一位全部置为0??
  97. 同样,它们也都会检查下标是否越界,如果越界就会抛出异常??
  98. 最后,还有一些类型转换的函数,如下:??
  99. ????bitset<8>?foo?("10011011");??
  100. string?s?=?foo.to_string();  //bitset转换成string类型??
  101. unsigned?long?a?=?foo.to_ulong();  //bitset转换成unsigned?long类型??
  102. unsigned?long?long?b?=?foo.to_ullong();  //bitset转换成unsigned?long?long类型??
  103. cout?<<?s?<<?endl;  //10011011??
  104. cout?<<?a?<<?endl;  //155??
  105. cout?<<?b?<<?endl;  //155??
  106. ????
  107. ????
  108. ????
  109. ????
  110. ????
  111. ????
  112. 来自?<https://www.cnblogs.com/magisk/p/8809922.html>???

相关推荐