STL Simple Learning notes in the <<c++ standard library >>

Source: Internet
Author: User
Tags repetition

0. Content for personal study notes, for reference only, if there are errors and omissions, please correct me!

1. All components in the STL are made up of templates, so their elements can be of any type. The components are:

-Container: Manages a collection of objects of a class. Different containers have their own pros and cons.

-iterators: Used to traverse on an element of an object cluster (Collection of Objects). This cob can be part of a container/container. Each of these containers provides its own iterator.

-Algorithm (algorithm): used to process elements within a cluster (e.g., query, modify, sort, etc.).

-Adapter (Adapter)

-Functor (functors or function objects)

2. The basic concept of STL is to separate the data and operations, the data to the container for management, the operation (algorithm) to customize the algorithm, the use of iterators to provide a unified interface between the two.

-The concept of STL is somewhat contradictory to the original idea of OOP, however this small framework provides good container/algorithmic elasticity.

-STL is a great example of generic programming, and containers and algorithms have been generalized for arbitrary types and categories.

-STL provides adapters and functor that can be implemented on the algorithm: complements, constrains and customizes to meet different requirements.

3. Containers

-Sequence container (Sequence container): a sequential cluster. (3)----vector/deque/list (why Queue,stack and string are not?)

-You can also use strings and array as a sequential container (why?).

-Array is a type (type) that is supported by the C + + language core, not (class). has a static size and a dynamic size. But it is not an STL container either. Because there is no member function like size (), empty (). But STL's design allows it to invoke the STL algorithm. This is especially useful when we initialize a row with a static arrays.

-There is no need to write the dynamic array directly, because it can be implemented with vectors.

-Associative Containers (Association container): Ordered clusters (4). Sorting and element values are not inserted in the order. ----Set/multiset/map/multimap

-The elements are automatically sorted. By default, comparisons are made using operator <.

-The general associative container is implemented by a two-fork search tree, where the main difference is that the type of the element already handles the repeating element.

-Set: Sorts by the values of the inner elements and does not allow duplication.

-Multiset: Same as set, but allows repetition

-Map:key/values, the key is not allowed to repeat.

-Multimap: Same as map, but key allows repetition. Can be used as a dictionary.

4. Container Adapter

-Stack:filo

-Queue:fifo

-Priority_queue: The next outbound element is always the highest priority element (the default comparison is operator < so the smaller the number, the higher the priority?).

5. Iterators (Basic operations)

-Operator */++/==/!=/=

-Each container must provide its own inner class, in fact each container defines its adapter as an inner class.

-2 types of iterators for any container

-Container::iterator

-Container::const_iterator

6. Some applications of associative containers

- typedef SET<int> intset;  / typedef SET<int, greater<int> >IntSet; Greater<> is a pre-defined copy function.

-All associative containers have an insert () method, but there is no push_back (), Push_front () method for the sequence container, because you do not have the right to specify the position of the new element.

-map allows elements to be inserted using operator [] . Multimap does not allow the subscript operator (because key can be duplicated).

-When accessing elements of a multimap or map, you must use the first and second members of the pair structure to access the specific Key/value

1#include <iostream>2#include <map>3#include <string>4 using namespacestd;5 intMain () {6typedef map<string,float>Stringfloatmap;7 Stringfloatmap Coll;8coll["VAT"] =0.15;9coll["Pi"] =3.14;Tencoll["This is a string"] =3.4; Onecoll["NULL"] =0; AStringfloatmap::iterator it = Coll.begin ();//map is not manually ordered, but not without order -      for(; It!=coll.end (); ++it) {//use ++it efficiency higher than it++ -cout<<it->first<<" : "<<it->second<<Endl; the     } -     return 0; -}
View Code

7. Iterator Classification (2 types)

-Bidirectional iterator (bidirectional iterator): This class is provided by List/set/multiset/map/multiset.

-Random access iterator (randomness access iterator): This class is provided by Vector/deque/strings. Random access iterators can support operator <

8. Algorithms

-Not a member function of a container, but a global function that is used with an iterator.

- pos = min_element (Coll.begin (), Coll.end ()) / pos = max_element (Coll.begin (), Coll.end ())

- Sort (coll.begin (), Coll.end () [, comp]);

- pos = Find (Coll.begin (), Coll.end (), value);

-Reserve (Pos,coll.end ())

STL Simple Learning notes in the <<c++ standard library >>

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.