C++primer the 17th Chapter

Source: Internet
Author: User
Tags bitset bitwise operators

//1. Tuple is useful when we want to combine some data into a single object, but do not want to bother defining a new data structure to represent it. It and its accompanying types and functions are defined in the header file tuple, declared in the namespace Std. TUPLE&LT;T1, T2,....., tn> t;//T is a member of a tuple of N, and the type of member I is ti. All members are initialized with a value. TUPLE&LT;T1, T2,....., tn> t (v1, v2,...., vn);//T is a tuple, the member type is T1 .... Tn, each member is initialized with the corresponding initial value VI. This constructor is explicit. MAKE_TUPLE&LT;V1, v2,...., vn>;//returns a tuple initialized with the given initial value. The type of a tuple is inferred from the type of the initial value. = = = <//for two tuple-directed relational operations, note: When two tuples have the same number of members, you can compare themGet<i> (t);//returns a reference to the I data membertuple_size<tupletype>::value;//a class template, which can be initialized with a tuple type, has a data member named value, type size_t, representing the number of members in a given tuple typeTuple_element<i, typletype>::type;//A class template that can be initialized with a constant and a tuple type, with a member of type representing the type of the specified member in a given tuple type//a common use of a tuple is to return multiple values from a function. //the 2.bitset type is defined in the header file Bitset. Declared in the namespace Std. Bitwise operators can be applied to bitset objects. //Bitset Constructor:Bitset<n> b;//b has n bits, each of which is 0Bitset<n> b (U);//B is a low n-bit copy of the unsigned long Long value, and if n is greater than unsigned long long, then the high of B will be set to 0Bitset<n> B (S, POS, M, Zero, one);//B is a copy of the M character of string s starting from position pos. S can only contain zero and one, if s contains other characters will throw an exception, pos default to 0,m default is String::npos (32-bit program is the largest integer 4 bytes, 64-bit program is 8 bytes maximum integer), zero defaults to ' 0 ', one defaults to ' 1 'Bitset<n> B (CP, POS, M, Zero, one);//similar to the previous constructor, but copies the characters from the character array pointed to by the CP. If M is not supplied, the CP must point to a C-style string. If M is supplied, at least M zero or one character starts at Pos. //Bitset The first bit is the lowest bit, the last one is the highest bitstrings ="101010"; Bitset<6> b (s);//B = [6] (0,1,0,1,0,1) Note: The subscript of string uses rules and Bitset insteadbitset<5> B1 (1);//B1 = [5] (1,0,0,0,0)//operation of Bitset:B.any ()//whether there is a bits in BB.all ()//has all the bits in B been put in place?B.none ()//there's no bits in B?B.count ()//number of positions in BB.size ()//returns the number of bits in BB.test (POS)//returns whether the POS position is placedB.Set(POS, V)//the position POS disposition bit bool value v,v defaults to True, and if the argument is not passed, all positions in BB.Set() B.reset (POS)//resets the bit at position POS and resets all bits in B if no parameters are passedB.reset () b.flip (POS)//change the bit state at POS, and change the bit state of all bits in B if no parameters are passedB.flip () B[pos]//returns a reference to the bit at POS in BB.to_ulong ()//returns a unsigned long or unsigned long long, overflow_over exception occurs if the size of the pending type cannot accept the size represented by BB.to_ullong () b.to_string (zero, one)//returns a String that represents the bit pattern in B. The default value for zero and one is ' 0 ', ' 1 ', which is used to denote 0,1 in B//3. Regular expression regex: Pending study//4. Random number: Defines a random number library in the header file defines a set of collaborative classes: The random number engine class and the random number distribution class. An engine class can generate a sequence of unsigned random numbers, a distribution class that uses an engine class to generate random numbers of a specified type, within a given range, and subject to a particular probability distribution. Default_random_engine e;//The standard library defines many random number engine classes, with differences in performance and random quality. Each compiler chooses one of these as the Defalut_random_engine type. This type generally has the most common features. Auto value = e ();//value = 3499211612//random number engine operation:Engine e;//default constructor: Use the default seed for the engine typeEngine e (s);//use shaped value s as a seedE.seed (s);//Use the seed s to reset the engine's stateE.min ();//The minimum and maximum values that can be generated by this engineE.max ();          Engine::result_type; //The unsigned shaping type generated by this engineE.discard (U);//push the engine U-step, the type of U is unsigned long long Discard[dis card | | d? ' Skɑ?d]n. Abandon//The random number distribution class defines a call operator that takes a random number engine as an argument, and the distribution object uses its engine parameters to generate a random number and map it to the specified distribution:Uniform_int_distribution<unsigned> U (0,9);d efault_random_engine E;auto value= U (e);//value = 8//The combination of a distribution object and an engine object is called a random number generator. Note: For a given generator, each run will return the same numeric sequence. By providing a seed (that is, a numeric value), the engine can use it to restart generating random numbers from a new position in the sequence. //operation of the random number distribution class:Dist D;//The default constructor, so that D is ready for use.D (e);//using the same E to call D consecutively, a sequence of random numbers is generated based on the distributed type of D. E is a random number engine. D.Min ();//returns the minimum and maximum values that can be generated by D (e)D.max ();                   D.reset (); //rebuilds the state of D so that subsequent use of D does not depend on the value that D has generated//each random number distribution class has a default template argument. The distribution type that generates the floating-point number generates a double value by default, and the distribution that generates the shaping value generates an int value by default. //common random number distribution classes:Bernoulli_distribution//non-template, always generates BOOL valueUniform_int_distribution<intt> u (m, n);//evenly distributed, Intt indicates that a reshape is required, but does not include bool and any char type. M is the minimum value returned, n is the maximum value returned, m defaults to 0, and N defaults to the maximum value that the type Intt object can represent. Uniform_int_distribution<realt> u (m, n);//evenly distributed, Realt represents one of the float, double, long double//The remaining random number distribution classes include: Bernoulli distribution, Poisson distribution, normal distribution, sample distribution. 

C++primer the 17th Chapter

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.