C + + Learning Note 43:stl

Source: Internet
Author: User

STL Introduction (Standard Template Library) basic components of STL: Containers (Container), iterators (iterator), function objects (functions object) algorithm (algorithms) Iterators is a bridge between the algorithm and the container that uses iterators as parameters to access the container rather than the container directly as the parameter of the algorithm, using the function object as a parameter of the algorithm rather than as part of the algorithm's execution of the function , using the STL or the custom iterator and function object, with the STL algorithm, can combine a variety of functions; Container Concept: An object that holds and contains a set of elements Basic Container class Template: Sequential containers: Array (array), vector (vector), deque (double-ended queue), Forward_list (single-linked list), list ) (ordered) associative container: Set (set), Multiset (multiset), map (map), multiple (multi-mapping)    unordered associative container:     Unordered_set, Unordered_multiset,unodered_map,unordered_multimap Container AdapterStack (stack) queue (queue) Priority_queue (priority queue) Note: Add the corresponding header file basic components of STL---iterators (iterator)
    1. Provides a way to sequentially access each element in a container
    2. You can use the + + operator to get an iterator that points to the next element
    3. You can use the * operator to access the element that the iterator points to, and if the element type is a class or struct, you can directly access a member of the element by using the-operator;
    4. Some iterators also support--operators get iterators that point to the previous element
    5. Iterators are generalized pointers and pointers have the same characteristics, so the pointer itself is an iterator
    6. Using an STL-independent iterator, you need to include the header file <iterator>

Basic components of STL---function objects (Functions object)

    1. An object that behaves like a function, which invokes a function as if it were called
    2. function objects are generalized functions: any ordinary function and any object that overloads the class of the "()" operator can be used as a function object
    3. Use STL's function object, need to include header file <functional>

STL's BASIC Component---algorithm (algorithm)

    1. Can be used extensively with different objects and built-in data types
    2. STL includes more than 70 algorithms (sorting, elimination algorithms, counting algorithms, comparison algorithms, transformation algorithms, permutation algorithms, container management, etc.)
    3. Using the STL algorithm, you need to include the header file <algorithm>
#include <iostream> #include <vector>#include<iterator>#include<algorithm>#include<functional>using namespacestd;intMain () {Const intN =5; Vector<int> S (N);//Container     for(inti =0; i < N; i++) {cin>>S[i]; }        /*algorithm: The inverse number of output;ostream_iterator<int> (cout, ""): Output stream iterator negate<int> () take the inverse number of the function, a transform algorithm implementation: t The Ransform algorithm iterates through the elements pointed to by first and last two iterators, takes each element's value as a Function object op's argument, and outputs the OP's return value through the iterator result order; The result iterator points to the next bit of the last element of the output after the traversal is complete. Transform will return the iterator, template <class Inputiterator, class Outputiterator, class unaryfunction> Outputiterator Transform (inputiterator First, Inputiterator last, Outputitertor result,unaryfunction op) {for (; First!=last;++first    , ++result) *result = OP (*first);    return result; }    */transform (S.begin (), S.end (), Ostream_iterator<int> (cout," "), negate<int>()); cout<<Endl; return 0;}

C + + Learning Note 43:stl

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.