Learn STL with me (2) -- vector container details

Source: Internet
Author: User

I. Introduction

In the previous topic, we introduced the six components in STL. The Container components are frequently used by most people, because STL containers implement the most widely used data structures, therefore, we use a lot of data when writing applications. However, containers can be sequential containers and associated containers. The C ++ language provides a sequential container array, and STL also provides vector, list, deque, and other sequential containers, this topic will detail the vector container.

Ii. vector details 2.1 Introduction to vector containers

The data structure of the vector container is a single-ended array. the operation method is very similar to that of the array. The only difference is that the array is a static space. Once the array size is configured, it cannot be changed, vector is a dynamic space. With the insertion of elements, the internal mechanism of the vector class will expand the space to accommodate new elements, we don't need to worry about applying for a large array at the beginning because of insufficient space, so we can use vector more flexibly. The differences between arrays in vector and C ++ are the same as those in List <T> classes in. NET.

2.2 create a vector object

Vector has eight constructors. The following code defines eight constructors:

        vector()        : _Mybase()        {          vector( _Alloc& vector(size_type _Count,  _Ty& vector(size_type _Count,  _Ty& _Val,  _Alloc&    vector( _Myt&        ->_Mylast =-> template< _Iter> template< _Iter> _Alloc&

The following example uses the constructor to create different vector objects and output the elements in each vector object. The Demo code is as follows:

#include <vector><iostream>     vector<>    vector<>    vector<> v1(    vector<> v2(,    vector<> v3(,    vector<>    vector<> v5(( index=;index<;index++=    vector<> v6(v5.begin()+,v5.begin()+    cout<<(v1_Iter=v1.begin();v1_Iter!=v1.end();v1_Iter++<< <<*<<    cout<<(v2_Iter=v2.begin();v2_Iter!=v2.end();v2_Iter++<< <<*<<    cout<<(v3_Iter=v3.begin();v3_Iter!=v3.end();v3_Iter++<< <<*<<    cout<<(v4_Iter=v4.begin();v4_Iter!=v4.end();v4_Iter++<< <<*<<<<  ( v5_Iter = v5.begin( ) ; v5_Iter != v5.end( ) ; v5_Iter++<<   << *<<<<  ( v6_Iter = v6.begin( ) ; v6_Iter != v6.end( ) ; v6_Iter++<<   << *<<    vector<><><<  ( v7_Iter = v7.begin( ) ; v7_Iter != v7.end( ) ; v7_Iter++<<   << *<<

Shows the running result of the Demo code above:

2.3 vector element operations

Many member functions are provided in the vector class. The following shows how to use some common functions. The Demo code is as follows:

# Include <vector> <iostream> print (vector <> & (size_t I =; I <v. size (); I ++ <v [I] <region element traversal access vector <> vint. push_back (cout <vint [] <(size_t I =; I <vint. size (); I ++ cout <I <vint. at (I) <endregion region uses the erase function to delete the element vint. erase (vint. begin () + <> (iter = vint. begin (), index1 =; iter! = Vint. end (); iter ++, index1 ++ <index1 <* iter <endregion region deletes the tail element using the pop_back function <> (iter1 = vint. begin (), j =; iter1! = Vint. end (); iter1 ++, j ++ <j <* iter1 <endregion region reverse traversal vector element vint. insert (vint. begin () +, vint. insert (vint. end (), <><< (iter2 = vint. begin (), k =; iter2! = Vint. end (); iter2 ++, k ++ <k <* iter2 <> (riter = vint. rbegin (), index =; riter! = Vint. rend (); riter ++, index ++ <index <* riter <endregion region two vector container elements interchange vector <> <endregion

The running result of the above Code is:

Iii. Summary

At this point, the content of this topic is over. vector containers are implemented as generic classes for data-line storage. This article introduces the use of array subscript, () function and iterator for element access, and then demonstrate the use of some common functions. The efficiency of adding and removing elements at the end of a vector is very high, but the efficiency of inserting and deleting elements in the middle or header is low, which is closely related to its data structure (linear continuous storage mode.

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.