(a): Learning objectives:
1: templates, including function templates and class templates
2: Container and its classification, to the data structure in the container
3: Specific use of container vectors and iterators
(ii): Learning
1: function template
/** * 学习模板函数 */int max(int a,intreturn a > b?a:b;}float max(float a,floatreturn a > b?a:b;}char max(char a,charreturn a > b?a : b;}
But the type is more, so each type has to write one such function, it must not be. So there is the template function.
Here is the template function:
/** * 上面函数的模板函数 */template<typename T> //或者是 template<class T>return a > b?a : b;}
T represents the type passed in
So how do I invoke a template function?
Here is the code that is called:
Note: Since Max is already defined in the STL, change the name here
The following is the definition and invocation of this procedure
/ * * test.cpp * * Created on:2015 April 15 * author:hongb_000 * *#include <iostream>using namespace std;///**//* Learn template functions// *///int Uip_max (int a,int b) {//return a > b?a:b;//}////float Uip_max (float a,float b) {//return a > b?a:b;//}////char Uip_max (char A,char b) {//return a > b?a:b;//}/** * Template function for the above function * /Template<typename t>//or a template<class t>T Uip_max (t a,t b) {returna > B?a:b;}intMainvoid){//Note that the <int> here cannot be omitted, this indicates the type of the input specified intm = uip_max<int> (Ten, -); cout << m << Endl;CharA = uip_max<Char> (' A ',' B '); cout << a << Endl;return 0;}
The template function replaces the appropriate type with the type that is passed in during the run. So this realizes the function of the template function.
2: template class
Now suppose we have two classes:
class IMax{publicIMax(int _a,int _b){ a = _a; int GetMax(){ return a>b?a:b; }privateintint b;};class FMax{publicFMax(float _a,float _b){ a = _a; int GetMax(){ return a>b?a:b; }privatefloatfloat b;};
The problem with this is the same as the problem with the function encountered above, which introduces the class template.
Now let's write the class template
TT>class CMax{public: CMax(T _a,T _b){ a = _a; T GetMax(){ returnTT b;};
Similar to the definition of a function template, all places where the type is encountered are replaced with T.
So how do you use this class template? Let's try it out here:
//这样这个地方的<int>也是不能省略的,为了指定类型 CMax<int> cMax(10,20intcout << p << endl;
A class template is the creation of a generic class.
Attention:
Class templates can define multiple types:
template<typename T1,typename T2>
3: Learning of containers
A container is a collection of data that is used to store and manage a set of elements.
The data structure of the container is shown in the following figure:
The container has a sequential container and an associative container.
Sequence of containers with vector,deque,list
The position of the associated container element depends on the criteria for the particular sort,
Associative containers have set,multiset and so on.
The study of 4:vector
1): Introduction to Vectors
Vectors are managed by placing elements in a dynamic container, which is equivalent to a dynamic array.
Vectors can access internal data at random.
The insertion and removal of elements in the tail of a vector is relatively straightforward, but in the middle it is more complex.
2): Pre-use preparation
#include <vector>usingnamespacestd;
3): Default construction
Vector VEC;
For example:
Vector VEC; A vector container that holds int types
Vector VEC; A vector that holds the float type
Class ca{};
Vector
vector<int> vec; vec.push_back(1); //在vector后面插入一个元素 vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5); //将vector的最后一个元素移除for0;i < vec.size();i++){ cout" "; }
The output is:
1 2 3 4
5): Access to vector data
Vectors get elements in two ways:
1:vec.at (i); But I cross the border, will run out Out_of_range a
2:vec[i]//i cross-border words without exception thrown, direct error
6): Modification of elements
Vectors can modify the elements of a position directly.
For example:
vce.at (2) = 18;
The following is an adjustment on the basis of the example just now:
The element of position 3 is now 3, we change her to 19.
vec.at(219fori0;ivec.size();i++){ cout << vec.at(i) << " "; } cout << endl;
The output is:
1 2 3 4
1 2 19 4
7): Access the first and last value of the vector
int mfront = Vec.front (); First value
int Mback-vec.back (); Last value
You can also use these two functions to modify the values in them
For example:
We want to change the first element to 88:
Vec.front () = 88; So the changes are successful.
5: Learning from iterators
An iterator is the equivalent of a pointer to an element in the vector.
Iterators are used to traverse the contents of a container, as if it were a pointer to a location in the container.
1): Category of Iterators
First type: input iterator, only one traversal is supported
The second type: output iterator, support only one pass input
Third: Forward iterators, which can be read and written multiple times
The fourth type: bidirectional iterator: You can read and write many times, and can read and write many times in the future
Fourth: Random-access iterators: You can read and enter content from anywhere except read and write
2): Operations supported by bidirectional iterators
It++,++it,it–,–it,*it,ita=itb,ita==itb,ita!=itb
Where List.set,multiset,map,multimap supports bidirectional iterators
3): Random access memory
Added random function based on the operation of the bidirectional iterator
It+=i;it = I;it+i;it[i],ita
vector<int>Veca;veca.push_back (1); Veca.push_back (3); Veca.push_back (5); Veca.push_back (7); Veca.push_back (9);//Note the wording of the iterator vector<int>:: Iterator it = Veca.begin ();cout<< *it << Endl;//The output should be the value of the first element 1++it;//But the front + + is more efficient than the Post + +, because the front + + returns a reference, and the Post + + returns a value cout<< *it << Endl;//The output is supposed to be 3.it + =3;cout<< *it << Endl;//The output should be 9.
When It==veca.end (), you cannot manipulate *it, because now is the next position of the last element
5): Use iterators for vector traversal
/** * 使用迭代器进行vector的遍历 */vector<int>for(;itA != vecA.end();itA++){ cout" "; }
6): Reverse iterator
/** * 反向迭代器,逆序输出 * * 其中的vec.rbegin()指向vector指向最后一个元素 * vec.rbegin()指向vector的第一个元素的上一个位置 */vector<int>for(;rit != vecA.rend();rit++){ cout" "; }
7): There are two more
First one: Vector::const_iterator
The second one: Vector::const_reverse_iterator
Are the two read-only forms that are mentioned earlier.
construction of 6:vector with parameters
1): Construction with iterator start and end
Example:
parameter is the start and end of the iterator
Vector VECB (array,array+5);
Vector VECC (Veca.begin (), Veca.end ());
2): Vector VecD (3,9); Vector has 3 elements, each element is 9
Assignment of 7:vector
1): Vector:assign (Beg,end); Copies the data of the (Beg,end) interval to itself. Left closed right Open
2): Vector:assign (N,ele); n Elements of Ele
3): Vector:swap (VEC); To swap the VEC with itself
Size of the 8:vector
1): Vector:size (); Returns the number of container elements, returning size_z
2): Vector:empty (); Returns whether the container is empty
3): Vector:resize (num); Reassign the container's length to num
4): Vector:resize (Num,ele); Re-specify the length of the container, more than the portion is filled with ele, and the less part is removed
For example:
vector<int> vecE; vecE.push_back(1); vecE.push_back(2); vecE.push_back(3//当前vecE有元素1,2,3 vecE.resize(5); //现在的元素应该为1,2,3,0,0 vecE.resize(8,3//现在的元素应该为1,2,3,0,0,3,3,3 vecE.resize(2//现在的元素应该为1,2
insertion of 8:vector
1): Vector:insert (Pos,ele); Inserts a copy of the Ele element at the POS location, returning the address of the new data
2): Vector:insert (Pos,n,ele); Insert n ele elements at POS location
3): Vector:insert (Pos,beg,end)://element in POS insertion [beg,end] Interval
Example:
/** * Insert Test * / vector<int>VECF; Vecf.push_back (1); Vecf.push_back (3); Vecf.push_back (5); Vecf.push_back (9); vector<int>VECG; Vecg.push_back (2); Vecg.push_back (4); Vecg.push_back (6); Vecg.push_back (8); Vecf.insert (Vecf.begin (), One);//Now the elements in the VECF should be 11,1,3,5,7,9Vecf.insert (Vecf.begin () +1,2, -);//Now the elements in the VECF should be 11,33,33,1,3,5,9 //The element after execution should be 2,4,6,8,11,33,33,1,3,5,9Vecf.insert (Vecf.begin (), Vecg.begin (), Vecg.end ());
element deletion of 9:vector
1): Vec.clear (); Remove all elements from a container
2): Vec.erase (Beg,end)://delete element of [Beg,end] Interval
3): Vec.erase (POS); Removes the element from the POS position, returning the location of the next data
Summarize:
Through the study of the section, basically understand the data structure in vector and its applicable methods, but still need more practice.
Study on STL Learning--vector