The fifth chapter in-depth understanding of Template 5.1 template parameters
About Bitset
The Bitset is the container that can store the binary.
The main operations for Bitset are:
(constructor)
Construct Bitset (public member function)// construction bitset: format bitset< length > name
Applicable operators
Bitset operators (functions)// You can perform binary operations directly on the Bitset container, such as ^,|,~,<<,>& gt; wait
Operator[]
Access bit (public member function)// can be assigned in the form of an array. bitset<4> B; B[0]=1;
Set
Set Bits (public member function)// default assigns all values in the container to 1, or assign specific locations to specific values
as bitset<4> B; B.set (); 1111. B.set (2,0)//1011.
Reset
Reset bits (public member function)// default assigns all values in the container to 0, or assigns specific values to specific locations
Flip
Flip bits (public member function)// default number of containers in reverse,1 to 0,0 Change to 1, you can also take a specific position of the anti- bitset<4>b (String ("0001")); B.file (2); 0101; B.file (); 1010
To_ulong
Convert to unsigned LongInteger (public member function)// Converts the value of a container to a number of ten binary
To_string
Convert to String (public member function)// convert container tired value to string
Count
Count Bits set (public member function)// number of 1 in the statistics container
Size
Return size (public member function)// container
Test
Return bit value (public member function)// returns the number at each location
Any
the value of the Test if any bit was set (public member function)// container >0 returns True, and vice versa.
None
The Test if no bit is the set (public member function)// and any inverse. The value of the container ==0 returns True. Conversely
Table 3-7 Bitset Operations
|
B.any () |
Is there a bits in B that is set to 1? |
|
B.none () |
There is no bits in B that is set to 1? |
|
B.count () |
Number of bits with 1 in b |
|
B.size () |
Number of bits in B |
|
B[pos] |
Access the bits at POS in B |
|
B.test (POS) |
B is the bits at POS at 1? |
|
B.set () |
Place all bits in B to 1 |
|
B.set (POS) |
Put the binary position in B at POS at 1 |
|
B.reset () |
Place all bits in B to 0 |
|
B.reset (POS) |
Put the binary position in B at POS at 0 |
|
B.flip () |
Take all the bits in B counter- |
|
B.flip (POS) |
Reverse the bits in the POS at the point B |
|
B.to_ulong () |
Returns a unsigned long value with the same bits in B |
|
OS << b |
Output the bit set in B to the OS stream |
Urand.h
/*** book: "thinkinginc++" * function: About template bitset* time: October 16, 2014 16:28:59* Author: cutter_point*/#ifndef urand_h_included#define Urand_h_included#include <bitset> #include <cstddef> #include <cstdlib> #include <ctime>using Std::size_t;using std::bitset;template<size_t upperbound> class urand{ bitset<upperbound> used; Public: Urand () {Srand (time (0));} Generate random number size_t operator () ();}; template<size_t upperbound>inline size_t Urand<upperbound>::operator () () { if (used.count () = = Upperbound)//b.count () b is set to 1 of the number of bits used.reset (); The description is full, the number of the inside is 1 full, all 1, then this let him all reset to 0 size_t newval; while (Used[newval = rand ()%upperbound]) ; Find the only value, that is, to jump out of the used[newval]=true when False ; return newval;} #endif//urand_h_included
UrandTest.cpp
/*** book: "thinkinginc++" * function: about how to use urand.h* time: October 16, 2014 16:29:21* Author: cutter_point*/#include <iostream># Include "Urand.h" using namespace Std;int Main () { urand<10> u; for (int i=0; i <; ++i) cout<<u () << "; System ("pause"); return 0;}
5.1.3 template parameters for template types
TempTemp.cpp
/*** book: "Thinkinginc++" * Features: Template parameters about template types * Time: October 16, 2014 16:29:58* Author: cutter_point*/#include <cstddef> #include <iostream>using namespace Std;template<class t>class array{enum {init=10}; American drama type, this is to avoid the use of macro t* data; size_t capacity; size_t Count;public:array () {count=0; Data=new t[capacity = INIT]; Create an array of type T} ~array () {delete [] data;} void push_back (const t& T) {if (count = = capacity)//If the quantity and total number are the same, the memory is insufficient {size_t newcap= 2*capacity; Increase the memory space by a factor of t* newdata=new T[newcap]; Move the original data to the new array for (int i=0; i<count; ++i) newdata[i]=data[i]; Reclaim the old space Delete []data; Update data member Data=newdata; Capacity=newcap; }//If there is free space data[count++]=t; Assign T to the last free place} void Pop_back () {if (Count > 0)--count; Remove the last one} t* begin () {RetuRN data; } t* End () {return data+count;}}; Template<class T, template<class> class seq>//This can be written Template<class T, Template<class U> class Se Q>class container{seq<t> seq;//Refer to the above template, where Seq is equivalent to tpublic:void append (const t& t) {seq.push_back (t); } t* begin () {return seq.begin ();} T* End () {return seq.end ();}}; int main () {container<int, array> Container; Template<class t> class array here seq is equivalent to array container.append (1); Container.append (2); int* P=container.begin (); while (P! = container.end ()) cout<<*p++<<endl; return 0;}
A discussion on the TypeName
TypeName
" typename " is a keyword in a C + + programming language. When used for generic programming, is a synonym for another term " class ". [1] This keyword is used to indicate that a non-independent name (dependent names) in a template declaration (or definition) is a type name, not a variable name. The following is an explanation of the typename two very different usages of generic programming.
classSynonyms for keywords
This is a function of generic programming (or "template programming") for a C + + programming language, which is typename used to introduce a template parameter, for example:
Defines a general function that returns the larger of a parameter
Template <typename t>
Const t& MAX (const t& x, const t& y)
{
if (y < x) {
return x;
}
return y;
}
In this case, typename another equivalent keyword can be used class instead, as shown in the following code snippet:
Defines a general function that returns the larger of a parameter
Template <class t>
Const t& MAX (const t& x, const t& y)
{
if (y < x) {
return x;
}
return y;
}
There is no functional difference between the two pieces of code.
Type name Indicator
Consider the following error code:
Template <typename t>
void foo (const t& T)
{
Declares a pointer to an object of type T::bar
T::bar * p;
}
struct Structwithbarastype {
typedef int BAR;
};
int main () {
Structwithbarastype x;
Foo (x);
}
This code appears to compile, but in fact the code is not correct. Because the compiler doesn't know T::bar whether it's a type name or a name for a variable. The root cause of this ambiguity is that the compiler does not understand whether the T::bar "template parameter of the non-independent name", referred to as "non-independent name." [2] Note that any bar class T containing an item named "" Can be passed into a function as a template parameter foo() , including typedef type, enum type, or variable.
In order to eliminate ambiguity, the C + + language standard stipulates:
A name used in a template declaration or definition andthat are dependent on a template-parameter are assumed not to name a Type Unlessthe applicable name lookup finds a type name or the name is qualified by Thekeyword TypeName.
When this ambiguity occurs, the compiler will automatically default bar to a variable name instead of a type name. So the code in the example above T::bar * p is interpreted as a multiplication instead of declaring p as a pointer to an object of type T::bar.
If there is another named StructWithBarAsValue type, as follows:
struct Structwithbarasvalue {
int bar;
};
Then the compiler will interpret it in a completely different way
T::bar * p
The meaning.
The final solution to the problem is to explicitly tell the compiler that T::bar it is a type name. This has to be done with typename keywords, such as:
Template <typename t>
void foo (const t& T)
{
Declares a pointer to an object of type T::bar
TypeName T::bar * p;
}
In this way, the compiler determines that it T::bar is a type name, and P is naturally interpreted as a T::bar pointer to an object of type.
5.4 Name Lookup problems
FriendScope2.cpp
/*** book: "thinkinginc++" * Function: Friend function about template * Time: October 16, 2014 16:30:49* Author: cutter_point*/#include <iostream>using namespace std;//forward declaration Template<class t> class Friendly;template<class t> void f (const friendly<t>& ); Template<class t>class friendly{ t t;public: Friendly (const t& TheT): T (TheT) {} friend void f& lt;> (const friendly<t>&); Note here that the addition of a <> is to indicate that this is a template void G () {f (*this);}}; void H () { F (friendly<int> (1));} Template<class t>void F (const friendly<t>& fo) {cout<<fo.t<<endl;} int main () { h (); Friendly<int> (2). g (); return 0;}
Here friendly the angle brackets in the declaration of F. This is the one who has to tell the compiler that F is a template. Otherwise, the compiler will look for a normal function called F without finding him.
Box1.cpp
Defining action symbols with templates
/*** book: "thinkinginc++" * Function: Define the operator of the template * Time: October 16, 2014 16:31:13* Author: cutter_point*/#include <iostream>using namespace std;//forward declaration Template<class t> Class Box;template<class t>box<t> operator+ (const Box<T >&, const box<t>&); Template<class t>ostream& operator<< (ostream&, const Box< t>&);//define Template<class T>class box{t T;public:box (const t& TheT): T (TheT) {} friend Box oper ator+<> (const box<t>&, const box<t>&); Indicates that this is a template friend ostream& operator<< <> (ostream&, const box<t>&); Ibid.};template<class t>box<t> operator+ (const box<t>& B1, const box<t>& b2) {return Box <T> (b1.t+b2.t);} Template<class t>ostream& operator<< (ostream& os, const box<t>& b) {return os<< ' [' <<b.t<< '] ';} int main () {box<int> B1 (1), B2 (2); cout<<b1+b2<<endl; REturn 0;}
One drawback of this program is that implicit conversions cannot be performed because the templates do not provide these conversions.
"Thinkinginc++" 73, in-depth understanding of the template