C ++ technology used by STL (1) -- member Template

Source: Internet
Author: User

STL is an important part of the C ++ standard library. It is not only a reusable component library, but also a software framework that contains algorithms and data structures, it is also a good example of C ++ generic programming. Many advanced C ++ technologies are used in STL. This topic describes how to use a member template. Mainly refer to "C ++ Primer" and "STL source code analysis".

Member template

Any class (template or non-template) can have members of its own class template or function template. Such members are called member function templates. Why does STL use this technology? Consider the assign member function of the vector container. It can take two iterators to assign values to the container, the two iterators can be list iterators, deque iterators, or even two native pointers. That is to say, the parameters of the member function assign are uncertain. The solution is to use the template parameters to represent the type of the iteration parameters. The following is a small program that uses the member template technology.

# Include <iostream> <br/> # include <list> <br/> using namespace std; </p> <p> // custom <br/> class MyAlloc {<br/>}; </p> <p> template <class T, class Alloc = MyAlloc> <br/> class MyVector <br/>{< br/> public: <br/> typedef T value_type; <br/> typedef value_type * iterator; <br/> // member template, accepting various iterators <br/> template <class I> <br/> void assign (I first, I last) <br/> {cout <"assign" <endl ;}< br/>}; </p> <p> int main () <br/>{< br/> MyVector <int> v; <br/> int a [] = {1, 2, 3 }; <br/> list <int> l (a, a + 3); <br/> v. assign (a, a + 3); // native pointer <br/> v. assign (l. begin (), l. end (); // linked list iterator <br/> return 0; <br/>}

Another template example in STL is to accept the container constructor of two iterators. Below is a small program.

# Include <iostream> <br/> # include <list> <br/> using namespace std; </p> <p> // custom <br/> class MyAlloc {<br/>}; </p> <p> template <class T, class Alloc = MyAlloc> <br/> class MyVector <br/>{< br/> public: <br/> typedef T value_type; <br/> typedef value_type * iterator; </p> <p> template <class I> <br/> MyVector (I first, I last) <br/> {cout <"MyVector" <endl ;} <br/>}; </p> <p> int main () <br/>{< br/> int a [] = {1, 2, 3 }; <br/> list <int> l (a, a + 3); <br/> MyVector <int> v (a, a + 3 ); // native pointer <br/> MyVector <int> v2 (l. begin (), l. end (); // linked list iterator <br/> return 0; <br/>}

In the above program, the internal definition member template of the class. If you want to define a member template outside, you must include two template parameters, class template parameters and your own template parameters. First, the class template parameter table, and then the template parameter table.

# Include <iostream> <br/> # include <list> <br/> using namespace std; </p> <p> // custom <br/> class MyAlloc {<br/>}; </p> <p> template <class T, class Alloc = MyAlloc> <br/> class MyVector <br/>{< br/> public: <br/> typedef T value_type; <br/> typedef value_type * iterator; <br/> template <class I> <br/> MyVector (I first, I last ); <br/> template <class I> <br/> void assign (I first, I last); <br/>}; <br/> template <class T, class Alloc> template <class I> <br/> MyVector <T, Alloc>: MyVector (I first, I last) <br/>{< br/> cout <"MyVector" <endl; <br/>}< br/> template <class T, class Alloc> template <class I> <br/> void MyVector <T, Alloc>: assign (I first, I last) <br/>{< br/> cout <"assign" <endl; <br/>}

Reprinted source http://blog.csdn.net/wuzhekai1985

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.