C + + standard library type Bitset and its application

Source: Internet
Author: User

---old text finishing---

The Bitset object is stored with each bit bit 0 or 1, which can be used to mark the presence or absence, the location subscript and the stored content to form a key-value pair. The advantage of using bit notation is that memory space can be saved, especially for mass data processing.

For example, the following questions: (1) A file is known to contain some telephone numbers, each number is 8 digits, the number of different numbers are counted, (2) 250 million integers to find out the number of distinct integers, memory space is not enough to accommodate the 250 million integers; (3) Someone has 8 stamps 5, 1 Stamps 4 , 1.8 Yuan of the Stamps 6, how many different kinds of postage can be obtained with one or several of these stamps?

The common denominator of the above 3 questions boils down to finding the number of non-duplicates in a pile of numbers- to repeat the problem . For the problem (3), in the 6*5*7 integer to repeat, we can use a char array or an array of int access, memory savings is not so obvious, but for (1) and (2), it is necessary to adopt the Bieset class library. (1) Medium: 8 bits maximum is 99 999 999, if each bit indicates whether a number exists, if each number on average 1 times, theoretically only need 100M bit (12.5MB) of memory.

1#include <iostream>2#include <bitset>//Bitset Class Library declaration3 using namespacestd; 4   5 intMain ()6 {  7     ConstUnsignedintA =189;//maximum postage 188 pt8Bitset<a> Bitvec;//Generating Bitset Objects9   Ten     intx =0, y =0, z =0;  One      for(x =0; X <=5; ++x) {   A          for(y =0; Y <=4; ++y) {   -              for(z =0; Z <=6; ++z) {   -bitvec[8* x +Ten* y + -* Z] =1;  the             }   -         }   -     }   -    +     intCount =0;  -      for(x =0; x<a; ++x) +     if(Bitvec[x]) Acount++;  atcout <<"Total:"<<Count -<<"types of postage combinations"<<Endl;  -    -     return 0;  -}

Reference Links:

http://blog.csdn.net/yushuai007008/article/details/7466945

http://dongxicheng.org/structure/bitmap/

http://www.cplusplus.com/reference/bitset/bitset/

C + + standard library type Bitset and its application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.