Template <size_t n> class Bitset;
Bitset
A Bitset Stores bits (elements with only possible values:0 or 1, true or false, ...).
[Bitset Storage bit (element can only be two possible values, i.e. 0 or 1,true or FALSE, ...)]
The class emulates an array of bool elements, but optimized for space allocation:generally, each element occupies E bit (which, on most systems, are eight times less than the smallest elemental Type:char).
[Bitset is similar to storing an array of type bool elements, but optimizes the spatial configuration. In general, each element in the Bitset occupies only one bit (in most systems, each element in the Bitset is a minimum data type of 1/8 of the number of bits in char)]
Each bit position can is accessed individually:for example, for a given bitset named Foo, the expression Foo[3] accesses Its fourth bit, just like a regular array accesses its elements. But because no elemental type was a single bit in most C + + environments, the individual elements is accessed as special re Ferences type (see Bitset::reference).
[Each bit in the Bitset can be read separately, for example, there is a bitset named Foo, and the expression foo[3] is like a normal array gets its elements, which means to get the 4th bit of Foo]
Bitsets has the feature of being able to being constructed from and converted to both integers values and binary strings (see Its constructor and members To_ulong and to_string). They can also is directly inserted and extracted from streams in binary format (see applicable operators).
[Bitset can be constructed from integer values or binary strings, or can be converted to integer values or binary strings (see Bitset constructors and their member functions To_ulong and to_string). Bitset can also be inserted directly into or read from a stream from a binary format (see applicable operators)]
The size of a bitset is fixed at compile-time (determined by its template parameter). For a class this also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of Vecto R (vector<bool>).
[The length of the Bitset is determined during compilation (as determined by its template parameters), if you want to use a class that can be used to determine the length and also optimize the spatial configuration during the run, see class VECOTR<BOOL> specifically designed for BOOL;]
/*Bitset (); Bitset (unsigned long val); Template<class CharT, class traits, class Alloc>explicit Bitset (const BASIC_ST ring<chart,traits,alloc>& str, typename Basic_string<chart,traits,alloc>::size_type pos = 0, TypeName Basic_string<chart,traits,alloc>::size_type n = basic_string<chart,traits,alloc>::npos); Construct bitsetconstructs a bitset container object:[constructs a Bitset container object] (1) Default Constructorthe object is initialized wit H Zeros. [Default constructor: Object initialized to 0] (2) initialization from integer valueinitializes the object with the bit values of val:[initialized with integer value] (3) initialization from St Ringuses the sequence of zeros and/or ones in Str to initialize the first n bit positions of the constructed Bitset object . [Constructs a Bitset object with the first n 0 or one of STR] Note that Bitset objects has a fixed size (determined by their class template argument) No matter the constructor used:t Hose bit positions not explicitly set by the constructor is initialized with a value of zero. [Note that the length of the Bitset object is determined at compile time, ifWhen constructing bitset, the value of the corresponding bit is not specified, the corresponding bit is initialized to 0]valintegral value whose bits is copied to the Bitset positions.-If the value Represe Ntation of Val is greater than the bitset size, only the least significant bits of Val was taken into consideration. [If Val is longer than Bitset, consider only the least significant bit of Val (that is, the top is truncated)]-If the value representation of Val is less than the bitset size, the Remainin G bit positions is initialized to zero. [If the length of Val is smaller than the length of the Bitset, then the remaining bits are initialized to 0]stra basic_string whose contents is used to initialize the Bitset:the constructor parse s the string reading at most n characters beginning at Pos, interpreting the character values ' 0 ' and ' 1 ' as zero and one, respectively. [Constructor reads n (up to N) characters starting from Pos in a string to initialize Bitset] If This sequence is shorter than the bitset size, the remaining bit positions be initialized to zero. [If the length of the string is smaller than the length of the Bitset, the remaining number of bits is initialized to 0]*/#include<iostream>#include<string>#include<bitset>intMain () {Std::bitset<8>foo; Std::bitset<8> Bar (0XFA2); Std::bitset<8> Baz (std::string("0101111001")); Std::cout<<"foo:"<<foo<<'\ n';//00000000std::cout<<"Bar:"<<bar<<'\ n';//10100010, the least significant bits of Val is taken into considerationstd::cout<<"Baz:"<<baz<<'\ n';//01011110, reading at the most n characters beginning at PosSystem ("Pause"); return 0;}
/*Std::bitset operators//member functionsbitset& operator&= (const bitset& RHS);bitset& operator|= ( Const bitset& RHS);bitset& operator^= (const bitset& RHS);bitset& operator<<= (size_t pos); bitset & Operator>>= (size_t POS), bool operator== (const bitset& RHS) Const;bool operator!= (const bitset& RHS) Const;bitset operator~ () const;bitset operator<< (size_t pos) const;bitset operator>> (size_t POS) const;// Non-member functionstemplate<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 Inserters/extractotemplate<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, const bitset<N>& RHS); Bitset operatorsperforms The proper bitwise operation using the contents of the Bitset. [Bitwise Operation Bitset]*/#include<iostream>#include<string>#include<bitset>intMain () {Std::bitset<4> foo (std::string("1001")); Std::bitset<4> Bar (std::string("0011")); Std::cout<< (Foo^=bar) <<'\ n';//1010 (XOR, assign)std::cout<< (Foo&=bar) <<'\ n';//0010 (and, assign)std::cout<< (Foo|=bar) <<'\ n';//0011 (OR, assign)Std::cout<< (foo<<=2) <<'\ n';//1100 (SHL, assign)std::cout<< (foo>>=1) <<'\ n';//0110 (SHR, assign)Std::cout<< (~bar) <<'\ n';//1100 (not)std::cout<< (bar<<1) <<'\ n';//0110 (SHL)std::cout<< (bar>>1) <<'\ n';//0001 (SHR)Std::cout<<std::boolalpha<< (Foo==bar) <<'\ n';//False (0110==0011)std::cout<< (Foo!=bar) <<'\ n';//true (0110!=0011)Std::cout<< (Foo&bar) <<'\ n';//0010std::cout<< (Foo^bar) <<'\ n';//0101std::cout<< (Foo|bar) <<'\ n';//0111System ("Pause"); return 0;}
/*bool None () const; Checks whether the bits is all 0 and returns the true bool any () const; Check if BITS has 1, then return True size_t count () const; Calculates the number of bits in Bitset of 1 size_t size () const; Returns the number of bits of bitset bool Test (size_t POS) const; Returns the value on the POS bit in Bitset reference operator[] (size_t POS)//Returns a reference to the value on the POS bit in Bitset bitset& flip ();bitset& Flip (size_t POS); Bitset all bits in the reverse, if POS is specified, then only bits on the POS are reversed bitset& reset () bitset& Reset (size_t POS); Reset Bits (all set to 0), if POS is specified, only bits on the POS are reset bitset& set ();bitset& set (size_t pos, bool val = true); Set bits (all set to 1), if POS is specified, then only bits on the POS are set to vto_string (); Returns the string form of the Bitset To_ulong (); Returns the unsigned long form of Bitset*/#include<iostream>#include<string>#include<bitset>intMain () {Std::bitset<4>mybits; Mybits.Set(); STD::stringMyString =mybits.to_string (); Std::cout<<"mystring:"<<mystring<<'\ n';//1111unsignedLongMyulong =Mybits.to_ulong (); Std::cout<<"Myulong:"<<myulong<<'\ n';// theSystem ("Pause"); return 0;}
Bitset of standard non-STL