5th Chapter C++STL Generalization Technology analysis

Source: Internet
Author: User

/*5th Chapter C++STL Generalization Technical Analysis 5.1 algorithm and iterator 5.2 memory allocator and Container 5.3 Concept 5.4 Chapter Summary 5th Chapter C++STL Generalization Technical Analysis 5.1 algorithm and iterator 5.1.1 algorithm 5.1.2 iterator 5.1.3 Function Object 5.1.4 Adapter 5.2 memory allocator and container 5.2.1 Memory Allocator 5.2.2 Container 5.3 concept 5.3.1 basic concept 5.3.2 container concept 5.3.3 iterator concept 5.3.4 Function Object Concept 5.4 Chapter summary*///5th Chapter C++stl Generalization Technology Analysis//5.1 Algorithms and iterators--------------------------------------------------------------------------------------------- // A#include <algorithm>#include<iostream>intMainvoid){  using namespacestd; Doublea[8] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0};//array of 8 elements  Doubleval =3.0; Double*result = Find (A, A +8, Val);//equivalent to find (&a[0],&a[7]+1,val)  if(Result = = A +8) cout<<"the array does not have an element value equal to"<< Val <<Endl; Elsecout<<"array has an element value equal to"<< Val <<Endl; return 0;}// the#include <iostream>#include<algorithm>using namespacestd;structprint{void operator()(intx) {cout<< x <<' '; }};intMain () {intA[] = { the,1, -,6,3, to,6,5, -}; Const intLength =sizeof(a)/sizeof(int); //print for each array elementFor_each (A, A +length, print ()); cout<<Endl; return 0;}//70, different from the code in the book, changed. #include <iterator>#include<iostream>#include<vector>intMainvoid){  using namespacestd; Vector<int>v; V.push_back (3); V.push_back (6); V.push_back (9); Vector<int>:: Reverse_iterator rfirst (V.end ()); Vector<int>:: Reverse_iterator rend (V.begin ());  while(Rfirst! =rend) {cout<< *rfirst <<' '; ++Rfirst; }  return 0;}// the/*The front story is not the wrong code, may be wrong. Bind can bind the first parameter, or bind a second parameter to interpret bind. bool Greater (T&X, t&y) {return x>y;} bind1st, which binds a constant value to the first parameter of greater, then the second argument is *it, the value in the referenced container. The following program, the constant 7, bound to greater the first parameter x, then return is:7> (*it) then the container less than 7 of the number, will be first found. Binding a constant to the first argument is not easy for the program to read. So bind2nd is much more common than bind1st. */#include<vector>#include<iostream>#include<algorithm>//find_if Algorithm#include <functional>intMainvoid){  using namespacestd; Vector<int>v; V.push_back ( -); V.push_back ( -); V.push_back (6); V.push_back (3); V.push_back ( in); Vector<int>:: Iterator less7_iter; Less7_iter= Find_if (V.begin (), V.end (), bind1st (Greater <int> (),7)); cout<< *less7_iter << Endl;//will print the number 6  return 0;}//My Test/*directly with bind2nd, find less than 7 of the number, to be more intuitive. Binding is the first, or second, parameter that binds a constant value to a two-tuple function object such as greater. If not special, bind to the second argument. */#include<vector>#include<iostream>#include<algorithm>//find_if Algorithm#include <functional>intMainvoid){  using namespacestd; Vector<int>v; V.push_back ( -); V.push_back ( -); V.push_back (6); V.push_back (3); V.push_back ( in); Vector<int>:: Iterator less7_iter; Less7_iter= Find_if (V.begin (), V.end (), bind2nd (less<int> (),7)); cout<< *less7_iter << Endl;//will print the number 6  return 0;}//74. Of course, it is also possible to use a function directly without a function object. #include <vector>#include<iostream>#include<algorithm>//find_if AlgorithmBOOLLESS7 (intx) {  returnX <7;}intMainvoid){  using namespacestd; Vector<int>v; V.push_back ( -); V.push_back ( -); V.push_back (6); V.push_back (3); V.push_back ( in); Vector<int>:: Iterator less7_iter =find_if (V.begin (), V.end (), LESS7); cout<< *less7_iter << Endl;//will print the number 6  return 0;}//5.2 Memory allocator and container--------------------------------------------------------------------------------------------- //5.3 Concept---------------------------------------------------------------------------------------------//5.4 Summary of this chapter---------------------------------------------------------------------------------------------//This chapter is a bit of interpretation of the flavor of the source, do not read carefully. 

TOP

5th Chapter C++STL Generalization Technology analysis

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.