Summary of vector containers in STL

Source: Internet
Author: User

1. Introduction to Vector

As one of the standard containers provided by STL, vector is frequently used and plays an important role. It is also very easy to use. A vector is also called a vector. A vector can be visually described as an array whose length can be changed dynamically. Its functions are similar to those of arrays. In fact, the more professional description is: vector is a multi-function, able to operate a variety of data structures andAlgorithmVector is considered a container because it can store various types of objects like a container. Simply put, vector is a dynamic array that can store any type, data can be added and compressed. (Note: STL containers can be called class templates from the implementation perspective ).)

So what are the main differences between vector and array ?? This is helpful for understanding vector ~~~~

Array:Static space is allocated. Generally, it cannot be changed if it is allocated. Just as we are familiar with defining an array, the length of the array cannot be changed, and we cannot access it out of bounds, but the compiler does not check for out-of-bounds. This should be especially noted during programming (many of them may get bored with such errors !!). Generally, the length of the requested array cannot meet our requirements. We need to apply for a larger array and copy the data in the original array.

Vector:Dynamic space is allocated, that is, we find that the container size can be not specified when the vector container is declared. The vector is automatically expanded with the addition of elements. However, we must be responsible for confirming that the space allocated by the vector is continuous, that is, supporting Random Access to subscripts in the array. In fact, the implementation mechanism of the vector is to reserve some space, in addition, the reserved space size increases at a certain rate. If the space is not enough, to ensure continuity, you must create a new space and then move the original elements to the new space, at the same time, reserve new space (and the new space is more than the original space), and finally release the original space. The advantage of this reserved space is that you do not need to re-allocate the space each time you add elements to the vector.

 

2. Common functions in the vecotr container

2.1.vector Constructor

The Declaration methods of vector containers mainly include:

Vector <ELEM> V,Create an empty vector.

Vector <ELEM> V1 (V ),Copy a vector.

Vector <ELEM> V (N ),Create a Vector Containing N pieces of data, which are generated by default.

Vector <ELEM> V (n, ELEM ),Create a Vector Containing n elem copies.

Vector <ELEM> V (beg, end ),Create a vector in the [beg; End) interval.

V .~ Vector <ELEM> (),Destroys all data and releases memory.

Use the following sectionCodeTo demonstrate several common methods to declare a vector:

View code

1 # include <iostream> 2 # include <vector> 3 4 using namespace STD; 5 6 int main () 7 {8 vector <int >:: iterator ITER; 9 // The first method is 10 vector <int> V1; 11 v1.push _ back (1); 12 v1.push _ back (2); 13 v1.push _ back (3 ); 14 cout <"output result of the first method:" <Endl; 15 for (iter = v1.begin (); iter! = V1.end (); ITER ++) 16 {17 cout <* ITER <""; 18} 19 cout <Endl; 20 // Method 21 vector <int> V2 (V1); 22 cout <"method 2 output result:" <Endl; 23 For (iter = v2.begin (); iter! = V2.end (); ITER ++) 24 {25 cout <* ITER <""; 26} 27 cout <Endl; 28 // method 29 vector <int> V3 (3); 30 cout <"method 3 output result:" <Endl; 31 For (iter = v3.begin (); iter! = V3.end (); ITER ++) 32 {33 cout <* ITER <""; 34} 35 cout <Endl; 36 // Method 4: 37 vector <int> V4 (3, 4); 38 cout <"output result of Method 4:" <Endl; 39 For (iter = v4.begin (); iter! = V4.end (); ITER ++) 40 {41 cout <* ITER <"; 42} 43 cout <Endl; 44 // Method 5 45 vector <int> V5 (v1.begin (), v1.end ()-1); 46 cout <"Method 5 output result: "<Endl; 47 for (iter = v5.begin (); iter! = V5.end (); ITER ++) 48 {49 cout <* ITER <"; 50} 51 cout <Endl; 52 // Method 6 53 int A [] = {1, 2, 3, 4}; 54 vector <int> V6 (a + 1, A + 2 ); 55 cout <"Sixth method output result:" <Endl; 56 for (iter = v6.begin (); iter! = V6.end (); ITER ++) 57 {58 cout <* ITER <""; 59} 60 cout <Endl; 61 // 62 V6 .~ Vector <int> (); 63 cout <"the result after memory is released is:" <Endl; 64 for (iter = v6.begin (); iter! = V6.end (); ITER ++) 65 {66 cout <* ITER <"; 67} 68 cout <Endl; 69 return 0; 70}

Running result:

Summary:Note:Vector <ELEM> C (beg, end)Declaration method: Create a vector that is the same as the [beg; End) range element. Note that the Left-closed and right-open intervals are used, in STL, both containers and algorithms use the left-closed and right-open intervals, including v. the end () function is also the bottom position of the returned vector end, which is equivalent to a [n] of int A [n] and cannot be accessed ~~~

2.2.other common function usage in Vector

V. Assign (beg, end), Assign the value of the data in the [beg; End) interval to v.

V. Assign (n, ELEM), Assign the copy value of n elem to v.

V. At (idx)Returns the data referred to by the index idx. If idx is out of bounds, out_of_range is thrown.

V. Begin ()To return the unique data of the iterator.

V. Capacity (), Returns the number of data in the container.

V. Clear ()To remove all data from the container.

V. Empty ()To determine whether the container is empty.

V. End (), Pointing to the last data address in the iterator.

WriteProgramDrill down:

View code

  1 # include 
   
     2 # include 
    
      3 4 using namespace STD; 5 6 int main () 7 {8 vector 
     
      : iterator ITER; 9 vector 
      
        V1; 10 int A [] = {1, 2, 4 }; 11 12 // program segment 1, exercise assign (n, T) 13 v1.assign (3, 2); 14 cout <"element in vector:"; 15 for (iter = v1.begin (); ITER! = V1.end (); ++ ITER) 16 {17 cout <* ITER <"; 18} 19 cout 
        
       
      
     
    
   

Running result:

Summary:For the assign function, assign values to the vector variable and modify the vector size automatically.

 

V. insert (Pos, ELEM)Insert an ELEM copy at the POs position,Return New Data Location(Location refers to the return address value ).

V. insert (Pos, N, ELEM)Data inserted in the [beg, end) range at the POs position.No return value.

V. insert (Pos, beg, end)Insert n ELEM data at the POs position.No return value.

V. Erase (POS)Delete the data at the POs position and return the dataLocation of the next data.

V. Erase (beg, end)Delete the data in the [beg, end) interval and return the data.The location of the next data.

Let's take a look at the insert and delete operations on the elements in the vector:

View code

1 # include <iostream> 2 # include <vector> 3 4 using namespace STD; 5 6 int main () 7 {8 int A [] = {2, 3, 4 }; 9 vector <int> V1; 10 vector <int>: iterator ITER; 11 12 // demonstrate the insert function 13 v1.insert (); 14 v1.insert (v1.begin () + 1, a, A + 3); 15 v1.insert (v1.begin () + 4, 2, 5); 16 cout <"data in vector:"; 17 for (iter = v1.begin (); ITER! = V1.end (); ++ ITER) 18 {19 cout <* ITER <"; 20} 21 cout <Endl; 22 // demonstrate erase function 23 v1.erase (v1.begin (), v1.begin () + 2); 24 v1.erase (v1.begin () + 1); 25 cout <"data in vector: "; 26 for (iter = v1.begin (); iter! = V1.end (); ++ ITER) 27 {28 cout <* ITER <"; 29} 30 cout <Endl; 31 return 0; 32}

Running result:

Summary:Note that the POs parameters of the insert and delete operations are passed in by the iterator. Pay attention to the return values of several insert functions.

 

V. Capacity ()Returns the number of data in the container.

V. Size ()Returns the actual number of data in the container.

V. Reserve ()Reserve the appropriate capacity.

V. Resize (Num)Specify the length of the queue again.

V. max_size ()Returns the maximum number of data in the container.

View code

1 # include <iostream> 2 # include <vector> 3 4 using namespace STD; 5 6 int main () 7 {8 vector <int> V1 (4, 1 ); 9 vector <int>: iterator ITER; 10 cout <"vector size value:" <v1.size () <Endl; 11 cout <"capacity value of vector:" <v1.capacity () <Endl; 12 cout <"max_size value of vector:" <v1.max _ SIZE () <Endl; 13 14 // use the reserve function 15 v1.reserve (6); 16 cout <Endl; 17 cout <"vector size value:" <v1.size () <Endl; 18 cout <"vector C Apacity value: "<v1.capacity () <Endl; 19 cout <" max_size value of vector: "<v1.max _ SIZE () <Endl; 20 cout <"the element in the vector is:"; 21 for (iter = v1.begin (); iter! = V1.end (); ITER ++) 22 {23 cout <* ITER <"; 24} 25 cout <Endl; 26 27 // use the resize function 28 v1.resize (6, 2); 29 cout <Endl; 30 cout <"vector size value:" <v1.size () <Endl; 31 cout <"capacity value of vector:" <v1.capacity () <Endl; 32 cout <"max_size value of vector:" <v1.max _ SIZE () <Endl; 33 cout <"the element in the vector is:"; 34 for (iter = v1.begin (); iter! = V1.end (); ITER ++) 35 {36 cout <* ITER <"; 37} 38 cout <Endl; 39 return 0; 40}

Output result:

Summary:The vector Reserve adds the vector capacity, but its size has not changed! Resize changes the capacity of the vector and increases its size! This is because: (1)ReserveIs reserved for containers, but does not really create element objects in the space. Therefore, elements in the container cannot be referenced before a new object is added. When adding a new element, call the push_back ()/insert () function. (2)ResizeIs to change the container size and create an object. Therefore, after calling this function, you can reference the objects in the container. Therefore, when a new element is added, use the operator [] operator or the iterator to reference element objects. Now call the push_back () function, which is added after the new space.

 

C. rbegin ()Returns the first data of a reverse queue.

C. rend ()Returns the next location of the last data in a reverse queue.

C. pop_back ()Delete the last data.

C. push_back (ELEM)Add a data entry to the end.

C. Front ()Return a data record.

C. Back ()Returns the last data and does not check whether the data exists.

C1.swap (C2)SWAps C1 and C2 elements.

Swap (C1, C2)Same as above.

These functions are relatively simple. I will not write any programs here. If you are interested, try them out !!!

 

Let's take a look at some of the lessons learned.

 

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.