C++stl bit flags, smart pointers and exception handling

Source: Internet
Author: User
Tags bitset bitwise class operator

Refer to the 25th section of 21-day learning and C + + to introduce the STL bit mark. is when you need a data representation that is not a whole number of bytes, such as a char int long, but instead uses the bits representation, you usually use the bit flags that are mentioned here. From the perspective of the C + + Standard Template Library, two forms of bit flags are implemented: The Bitset class and the vector<bool> template are more convenient to implement a variety of bit storage and bit operations.

first, the use of STL bit Mark

Bits are an efficient way to store settings and flags. The Standard Template Library provides classes that help you organize and manipulate bit information.

1. Bitset class

Std::bitset is not an STL container class because it cannot adjust the length, which is a utility class that is optimized for the bit sequences that are known to process the length in the compilation phase. Must include # include <bitset>.

2. Instantiating Bitset

Bitset <4> fourbits;

Bitset <5> fivebits ("10101");

Bitset <8> eightbitscopy (eightbits);

You can use integers to initialize bits.

But remember: Bitset is not like a vector, its bits are specified in the compile phase, not dynamically adjusted, so you cannot insert more bits after you specify it.

3. Bitset class operator and member functions

Bitset provides a number of useful operators, such as:

<< inserts the text of a bit sequence into the input stream;

>> inserts a character into the Bitset object;

& execution of bitwise and;

| Perform a bitwise OR;

^ Perform bitwise XOR;

~ Perform bitwise inversion;

>>= performs a bitwise right SHIFT operation;

<<= performs a bitwise left shift operation;

operator [N] Provides reference access to similar array subscripts;

A bit is one or all of the bits in a bitset that can be stored in two states, bitset content operations, using the following member functions:

Set sets all the bits in the sequence into a bitwise operation;

Set (n,val=1) sets the n+1 bit to the value specified in Val, which defaults to 1;

Reset resets all bits in the sequence to the reset operation;

Reset (N) resets the n+1 bit to the reset operation ;

Flip takes all the bits in the bit sequence back;

Size returns the number of bits in the sequence;

Count returns the number of digits in the sequence that have a value of 1;

4. Vector <bool>

Vector <bool> is a partial materialization of vectors that store boolean data and can be used to dynamically adjust the length so programmers do not need to know the number of Boolean flags to store during the compilation phase.

In addition to the function flip provided for vector<bool>, which is used to reverse the Boolean values in the sequence, other operations much the same as the vector. Don't dwell on it.

5. Summary

Bitset is the most effective tool for processing bit sequences and bit markers, and dynamic bool can be provided by vector<bool>.

************************

2015-8-9


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C++stl bit flags, smart pointers and exception handling

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.