[C + +] STL Extraction Learning

Source: Internet
Author: User

STL Extraction Learning

First, the Extraction template class implementation

Iterator class: A collection of common properties for templates, and other classes only need to integrate that class. This example includes only the Value_type attribute for learning.

Iterator_traits Class: Attribute extraction class.

/*********************************** author:szyu** date:2017.3.1***************************************/#ifndef _ _szyu_iterator__#define __szyu_iterator__template <typename t>class iterator_traits{public:typedef TypeName T: : Value_type Value_type;}; Template <typename t>class iterator{public:typedef T value_type;}; #endif
/******************************                                                              ** author : szyu** date :  2017.3.1*************************************/#ifndef  __szyu_head__#define __szyu_head__# include  "Iterator.h" Template <typename t>class test : public iterator <t>{};template <typename t>typename iterator_traits<t>::value_typefunction (  int num1, int num2 ) {    std::cout <<  "Traits  function int. "  << std::endl;    return num1 + num2;} Template <typEname t>typename iterator_traits<t>::value_typefunction ( double num1, double  num2 ) {    std::cout <<  "traits function double."  << std::endl;    return num1 + num2;} #endif

two, test case

/*************************                                                                   ** author :  szyu** date : 2017.3.1***********************************/#include  <iostream># include  "Head.h" Voidtest1 () {    int v1 = function<test<int>  > (1, 2);     std::cout <<  "v1 = "  < < v1 << std::endl;    double v2 = function<test <double> > ( 1.1, 2.2 );    std::cout <<  "v2  =  "&NBSP;&LT;&LT; v2 << std::endl;} Intmain ( int argc, char *argv[] ) {    test1 ();     return 0;}

Third, test results

Traits function int.v1 = 3Traits function Double.v2 = 3.3



[C + +] STL Extraction Learning

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.