C + + Learning Note 34: Generics programming Development 3

Source: Internet
Author: User

Input stream iterator

#include <iostream>#include<iterator>#include<algorithm>#include<vector>using namespacestd;intmain () {vector<int> V (4); Vector<int>::iterator it =V.begin (); cout<<"enter four ints separated by Space & a char:\n"; Istream_iterator <int> Head (CIN), tail;    Copy (head, tail, it);    Cin.clear (); cout<<"vector=";  for(it = V.begin (); It! = V.end (); it++) {cout<< *it <<" "; } cout<<Endl; return 0;}

Table: A doubly linked list in the Standard Template Library

Use of tables

    • Defines the container that contains the Point object:list<point> pts (8);
    • Insert: Pts.insert (Pts.begin (), point (n));
    • Table Header insert: Pts.push_front (point);
    • Insert: Pts.insert (Pts.end (), point (n));
    • Footer insertion: Pts.push_back (point);
    • Defines a container that contains a point pointer: list<point*>ppts (8);
    • Insert: Ppts.insert (Ppts.begin (), new Point);
    • Insert: Ppts.insert (Ppts.end (), new Point);
    • Delete: Delete Ppts.front ();pp Ts.remove (Ppts.front ());
    • Determine if the table is empty: if (Pts.empty ()) cout << "empty!";

Tables and iterators

Iterators can work together with tables in the same way as vectors

List<int> A (8);

List<int>::iterator it;

for (it = A.begin (); It! = A.end (); it++)

*it = Generaterandomnumber (10,99);

Table sort

    • Directly using the table's member functions: A.sort ();//Default Ascending
    • Descending: Reverse ()
    • Descending: (incoming functor) greater_equal<int> ()
    • A.sort (greater_equal<int> ());
    • For custom objects, you need to overload operator < for comparison

Standard algorithms

Functions that invoke the standard Template Library

Standard Sub-function

Arithmetic functor

Plus<t>

Minus<t>

Multiplies<t>

Divides<t>

Modulus<t>

Negate<t>

Relationship functor

Equal_to<t>

Not_equal_to<t>

Greator<t>

Greator_equal<t>

Less<t>

   Less_equal<t>

Logical functor

Logical_and<t>

Logical_or<t>

Logical_not<t>

C + + Learning Note 34: Generics programming Development 3

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.