"Essential C + +"--(iii) Generic programming style

Source: Internet
Author: User

1.STL (Standard Template Library) is composed of 2 components: container--vector,list,set,map, generic algorithm--find (), sort (), replace (), merge (), etc.

2. When an array is passed to a function, or returned by a function, only the address of the first element is passed: int min (int array[24]) and int min (int *array) are the same.

3. Generic pointer iterator:vector<string>::iterator iter=svec.begin ().

4. Use the Sequential container: (1) Insert the element anywhere in the vector, or delete the element at any position, which is not the end and is very inefficient. But the index is fast. The vector is suitable for use as arrays.

(2) The list is easy to insert and delete, but the index is very slow.

(3) Deque is more efficient for inserting and deleting the most front-end elements.

5.5 ways to define sequential containers

#include <vector>

#include <list>

#include <deque>

(1) An empty container:list<string> IList;

(2) Producing a container of a specific size:list<string> IList (32);

(3) Produce a container of a specific size and specify the initial value:vector<int> Ivec (10,-1) for each element;

(4) Create a container by a pair of iterator: int ia[8]={1,1,2,3,5,8,13,21}; Vector<int> fib (ia,ia+8);

(5) The copy of the container:list<string> slist;

Fill Slist ....

List<string> Slist2 (slist);

6. Operation function of the container

Push_back ();p op_back (); Front (); Back ();

Push_front ();p op_front (); For List,deque only

Iterator Insert (iterator position,eletype value); Inserts a value before the specified position, returning the position of the element

"Essential C + +"--(iii) Generic programming style

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.