A probe into C + + STL

Source: Internet
Author: User

People who have studied C + + will certainly be familiar with STL standard template libraries, andSTL is actually encapsulating a series of interfaces for us to invoke . the implementation of many functions or algorithms does not require us to write from scratch, greatly improving our programming efficiency . This blog in a simple introduction to the STL case, will be detailed to introduce the use of vectors.

Official documentation Links

There are six main components of STL:

A: Container (container): is a data structure, such as List,vector,deque,queue, which is provided by the method of the template class, in order to access the data in the container, you can use the iterator provided by the container class.

Second, iterator (Iterator): Provides a way to access objects in the container.

Algorithm (algorithm): a template function that is used to manipulate the data in a container.

Functor (function object).

Five, iterative adapter (Adapter).

Six, the control adapter (allocator).

We'll talk about vectors first in this blog post. the data structure of the vector is an array of dynamic arrays, which means that we do not have to care about the amount of capacity that the array defines beforehand, and its size will grow dynamically . Similar to arrays, we can add and delete elements at the end, as well as random access and modification of element values.

First of all to introduce the header file #include <vector>. The specific use is as follows:

(1) Creation of vectors:

vector<int//  Create an empty vector  vector<int//  Create a vector vec2 and use VEC1 to initialize  vec2vector<int> vec3 (//  Create a vector  containing n data vector<int> Vec4 (ten,0//

Constructors are overloaded, and vectors can be created in a variety of ways.

(2) push_back (), Pop_back () add Delete element

//adding elements to the tail of a vectorVec1.push_back (4); Vec1.push_back (6); Vec1.push_back (8); Vec1.push_back (1); Vec1.push_back (2);    Printvector (VEC1); //Delete an element at the tail of a vectorVec1.pop_back ();    Printvector (VEC1); //adding elements to the head of a vector cannot be done because the data structure of the vector is an array of elements that cannot be inserted in the head, otherwise the entire array must be moved forward;//deleting elements in the head of a vector cannot be done for the same reason. 

You can use Push_back () to continuously add elements to the tail of the vector, using pop_back to delete the trailing elements. The operation is very convenient, more convenient than we directly use the array structure.

(3) [],at (), take the element value of a position

    1. Take the element value of a position in a vector
    2. cout << "element values at 1 position:" << vec1.at (1) << Endl;
    3. cout << "element values at 1 position:" << vec1[1] << Endl;

Because the data structure of a vector is an array, random access is possible.

(4) Begin (), end (), pointer to head element, tail element

void Printvector (vector<int> ve)  {      "thedata in the vector is:";      Vector<int>:: Iterator veiterator;        for (Veiterator = Ve.begin (); Veiterator < Ve.end (); veiterator++)      {          "";      }       << Endl;  }  

(5) Back (), front (), access to the head element and tail element

// returns a reference  to the trailing data " the value of the trailing data is: " << vec1.back () << Endl;   // returns a reference  to the header data " the value of the header data is: " << vec1.front () << Endl;  

(6) Max_size (), maximum capacity of the vector; size (): The current number of elements of the vector.

" the maximum capacity in a vector is: " << vec1.max_size () << Endl;   " the number of elements in the vector is: " << vec1.size () << Endl;  

(7) empty () to determine if the vector is empty

" whether the vector is empty: " << vec1.empty () << Endl;  

If empty, returns 1. Otherwise, 0 is returned.

(8) Swap (): Swaps the values in two vectors.

(9) Sort (): Sort vectors in ascending order; reverse (): Sort vectors in descending order.

(ten) Erase (): Delete an element

// Delete an element  of an array // Why use iterator for positioning, because an array that deletes an element or inserts an element causes other elements to move, so it cannot be deleted  directly vector<int>::iterator Vitera = vec1.begin ();   2 ;  Vec1.erase (Vitera);  Printvector (VEC1);  

(one) Clear (): Clear all elements

// Clear All data      vec1.clear ();     Printvector (VEC1);      " whether the vector is empty: "

Reference article:

http://www.cplusplus.com/reference/queue/queue/

Http://mropengate.blogspot.com/2015/07/cc-vector-stl.html

http://blog.csdn.net/chenyufeng1991/article/details/52269300

http://blog.csdn.net/wallwind/article/details/6858634

A probe into C + + 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.