Bitset of STL containers

Source: Internet
Author: User

1. STL container
A container is an object used to save other objects. The saved object is called the container element. Generally, containers are implemented as template classes.

The container allocates storage space for elements and provides element access methods.

Stack, queue, and priority_queue are implemented as "container adapters ". The container adapter is not an independent container, but provides specific interfaces, which are implemented by other containers internally.

STL provides the following container template classes

1.1. Ordered container
Vector: vector
Deque: bidirectional queue
List: Linked list
1.2. Container Adapter
Stack: stack, advanced and later (LIFO)
Queue: queue, FIFO)
Priority_queue: priority queue
1.3. combined container
Set: set
Multiset: Multi-key set
Map: ing
Multimap: Multi-key ing
Bitset: bit set
2. bitset
We have introduced some knowledge about STL containers in the previous section. The following section describes bitset in detail.

2.1 header file, namespace, and bitset Declaration
[Cpp]
# Include <bitset>
 
Using std: bitset;
Bitset declaration:
[Cpp]
Template <size_t N> class bitset;
2.2 Constructor
[Cpp] view plaincopyprint?
Bitset ();
 
Bitset (unsigned long val );
 
Template <class charT, class traits, class Allocator>
Explicit bitset (const basic_string <charT, traits, Allocator> & str,
Typename basic_string <charT, traits, Allocator >:: size_type pos = 0,
Typename basic_string <charT, traits, Allocator >:: size_type n =
Basic_string <charT, traits, Allocator>: npos );
Bitset <N>: bitset () is used to instantiate a bitset with N bits. Each bitset is set to 0.
Bitset <N>: bitset (unsigned long val) instantiate a bitset with N bits. The bitset uses the unsigned long integer val to initialize the bitset. Val's low position corresponds to the low position of the bit set. If N is greater than the number of digits occupied by val, use 0 for initialization of the extra high level and val for initialization of the lower level. If N is less than the number of digits occupied by val, use val's low N-bit initialization bit set.
The third constructor uses the binary string str to initialize the bitset. Start with str [pos] and use the n-Bit String. If n is not specified, the end of the string is used. It is worth noting that the low position of the string's high initialization bit set. If the length of the string substring used for initialization is greater than the number of digits in the bit set N, the string substring is initialized using the low N bytes. If the length of the character string substring used for initialization is less than the number of digits N of the bit set, the low position of the character string substring is initialized. If the length exceeds the number of digits, the value 0 is used for initialization.
Sample Code:

[Cpp]
// 32-bit machine, sizeof (unsigned long) = 4, that is, 32-bit
// Use an unsigned integer to initialize a bitset
Unsigned long n = 0xf0f0ff00;
 
Bitset <32> bits1 (n); // bit set from high to low: 11110000111100001111111100000000
Bitset <16> bits2 (n); // bit set from high to low: 1111111100000000
Bitset <40> bits3 (n); // bitset from high to low: 0000000011110000111100001111111100000000
 
// Use a string to initialize a bit set
String str = "1111111100000000 ";
 
Bitset <16> strbits1 (str); // bitset from high to low: 1111111100000000
Bitset <10> strbits2 (str); // bitset from high to low: 1111111100
Bitset <20> strbits3 (str); // bitset from high to low: 00001111111100000000
 
// What happens if non-binary string Initialization is used?
Bitset <32> bits (-1); // converts a negative number to a positive number for initialization. 0xffffffff is used for initialization.
 
// What if negative integer is used for initialization?
Bitset <32> strbits (string ("01abc10"); // an error is reported during running

 

2.3 Operator
[Cpp]
Bitset <N> & operator & = (const bitset <N> & rhs );
Bitset <N> & operator | = (const bitset <N> & rhs );
Bitset <N> & operator ^ = (const bitset <N> & rhs );
Bitset <N> & operator <= (size_t pos );
Bitset <N> & operator >=( size_t pos );
Bitset <N> operator ~ () Const;
Bitset <N> operator <(size_t pos) const;
Bitset <N> operator> (size_t pos) const;
Bool operator = (const bitset <N> & rhs) const;
Bool operator! = (Const bitset <N> & rhs) const;
 
// *** Global functions :***
Template <size_t N>
Bitset <N> operator & (const bitset <N> & lhs, const bitset <N> & rhs );
Template <size_t N>
Bitset <N> operator | (const bitset <N> & lhs, const bitset <N> & rhs );
Template <size_t N>
Bitset <N> operator ^ (const bitset <N> & lhs, const bitset <N> & rhs );
 
// *** Iostream global functions (extraction/insertion ):***
Template <class charT, class traits, size_t N>
Basic_istream <charT, traits> &
Operator> (basic_istream <charT, traits> & is, bitset <N> & rhs );
Template <class charT, class traits, size_t N>
Basic_ostream <charT, traits> &
Operator <(basic_ostream <charT, traits> & OS, bitset <N> & rhs );
 
// *** Bit access ***
<Pre name = "code" class = "cpp"> <pre> bool operator [] (size_t pos) const;
Reference operator [] (size_t pos );

2.4 bitwise operation functions
Function Description
Set () bit set to full 1
Set (n) the nth position is 1
Set the reset () bit set to 0
The position n of reset (n) is 0.
Flip () Bit Set flip
2.5 bitset operation functions
Function Description
To_string bit set to string
To_ulong bitset is converted to an unsigned integer. If the bit set size is greater than the number of digits of ulong, the height of the bit set is discarded.
Count returns the number of values in the bitset 1.
Size returns the capacity of the bitset.
Any returns true if the bitwise set has 1.
None if the bitset does not have 1, true is returned.
Test (n) returns true if the nth bit is 1.

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.