Vector vectors container

Source: Internet
Author: User

1.0 Vector Overview:

Vector vectors container, I have seen a lot of the definition of vector names, for example, indefinite long arrays, vectors, variable groups and so on. In fact, these are called according to the unique nature of the vector.

It can not only be like an array of elements to random access, but also the end of the insertion of elements, is an efficient, simple container, completely to a certain extent can replace the array.

It is worth noting that vector has the function of automatic memory management, that is, when we want to insert the new element into the container, we can not manually request the memory space. For the deletion of the old element,

Instead of deliberately delete memory space, he can dynamically adjust the occupied memory space.

The index of the vector container is counted starting at 0, that is, if the vector container is the size of n, then the index of the element will be 0-n-1.

For the definition of vector size, you can define a fixed size in advance and adjust his size at any time afterwards. You can also not define his size, then use Push_back () from the tail

Constantly expanding elements, and inserting () a new element in front of an element's position.

1.1 Creating a Vector object

The creation of vector objects is commonly used by three different forms.

(1) Do not specify the number of elements of the vector container, such as defining a container to store the shaping.

vector<int>v;

vector<xxx>v; XXX: Can be a data type that contains all of the own and that is considered defined

(2) Create a container size, for example, we want to create a vector container of size 10 to store elements of type double

Vector<double>v (10);

PS: The subscript of the element will change from 0-9, and the value of each element is initialized to 0.0.

(3) Create a vector container object with n elements, each of which has a value of 8.6

Vector<double>v (10,8.6);

This code says that a vector container named V is defined, with 10 elements in the container, all 10 of which are 8.6

If we define VECTOR<INT>V (10,8.6), we try to traverse the container, and we find that all the elements in the container are 8, which means

After the type has been automatically converted,vector<xxx> defined, all elements that are added will be coerced into the xxx type.

1.2 Expansion of the tail element

A new element is usually appended to the tail of the vector container with push_back (). When a new element is appended to the tail, the vector automatically allocates the memory space for the new element. And we Also

You can expand on an empty vector object, or you can extend the vector of an existing element.

The following code is a container that adds 2,7,9 three elements from the tail to the V, so that the three elements in the V container are 2,7,9 because the tail is added.

Code:

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>//The accumulate algorithm requires# include<numeric>using namespacestd;intMainvoid) {vector<int>v; V.push_back (2); V.push_back (7); V.push_back (9); Vector<int>:: Iterator it;  for(It=v.begin (); It!=v.end (); it++) cout<<*it<<Endl; return 0;}

1.3 Subscript to access the elements of the vector

  

It is important to access or traverse the elements in the vector, because we know that we want to do what we expect for some of the target data, and we will use a container to

First stored, and then for this stored data, we want to query, the vector query process is actually similar to the data, directly through the index of the element to access an element.

The following code is used to assign a value to an array using the subscript, 2,7,9 the value of the output element.

Code:

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>using namespacestd;intMainvoid) {vector<int>v (3); v[0] =2; v[1] =7; v[2] =9; cout<<v[0]<<Endl; cout<<v[1]<<Endl; cout<<v[2]<<Endl; return 0;}

1.4 using iterators to access vector elements

It is often necessary to iterate through the objects in the vector with an iterator with a looping statement, and the type of the iterator must be the same as the element type of the vector object it is accessing.

The following code uses a vector to iterate over an element,

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>using namespacestd;intMainvoid) {vector<int>v (3); v[0] =2; v[1] =7; v[2] =9; Vector<int>:: Iterator it;  for(It=v.begin (); It!=v.end (); it++) cout<<*it<<Endl; return 0;}

1.5 Insertion of elements

The Insert () method inserts a new element in front of any position in the vector object, while the vector automatically expands the space of an element, and all elements after the insertion position are sequentially backward.

element to move backward one position in turn.

Note that the insert () method is the position where the insertion is required and is the iterator position of the element, not the subscript position of the element.

The result of the following code output is 8,2,1,7,9,3

Code:

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>using namespacestd;intMainvoid) {vector<int>v (3); v[0] =2; v[1] =7; v[2] =9; V.insert (V.begin (),8); V.insert (V.begin ()+2,1); V.insert (V.end (),3); Vector<int>:: Iterator it;  for(It=v.begin (); It!=v.end (); it++) cout<<*it<<Endl; return 0;}

2.6 Deletion of elements

The erase () method can remove an element that is referred to by an iterator in a vector or all elements in a range.

The clear () method deletes all elements in the vector.

The following code shows how to delete a vector element:

Code:

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>using namespacestd;intMainvoid) {vector<int>v (Ten);  for(inti =0; I <Ten; i++) {V[i]=i; }    //The 3rd element is removed because it is counted starting at 0.V.erase (V.begin () +2); Vector<int>:: Iterator it;  for(It=v.begin (); It!=v.end (); it++) {cout<<*it<<Endl; } cout<<Endl; //Delete 2nd to 6th elementsV.erase (V.begin () +1, V.begin () +5);  for(It=v.begin (); It!=v.end (); it++) {cout<<*it<<Endl; } cout<<Endl; cout<<v.size () <<Endl; cout<<Endl;    V.clear (); cout<<v.size () <<Endl; return 0;}

1.7 algorithm using reverse to reverse permutation

Reverse the reverse permutation algorithm needs to define the header file #include<algorithm>

The reverse inverse permutation algorithm can reverse the interval of an iterator in a vector, opening the following code:

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>//Reverse Need# include<algorithm>using namespacestd;intMainvoid) {vector<int>v (Ten);  for(inti =0; I <Ten; i++) {V[i]=i; } Vector<int>:: Iterator it; //reverse arranges all elements of a vector from beginning to endReverse (V.begin (), V.end ());  for(It=v.begin (); It!=v.end (); it++) cout<<*it<<Endl; return 0;}

1.8 Sorting vector elements using the sort () algorithm

Using sort () and using reverse () also requires #include<algorithm>

The sort algorithm requires a random-access iterator to sort the vector elements in ascending order by default, which is what we say from small to large, and this program is a good illustration of the sort algorithm

Method of use.

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>//Reverse Need# include<algorithm>using namespacestd;intMainvoid) {vector<int>v (Ten);  for(inti =0; I <Ten; i++) {V[i]=9-i; } Vector<int>:: Iterator it;  for(It=v.begin (); It!=v.end (); it++) cout<<*it<<Endl; cout<<Endl; //sort from small to large for elements in a vector containersort (V.begin (), V.end ());  for(it = V.begin (); It!=v.end (); it++) cout<<*it<<Endl; return 0;}

The above sort (V.begin (), V.end (), XX); XX default is empty, according to the order from small to large, if we according to our request to write a CMP, he will follow

Our request is to sort the elements in the vector.

The following code is for the elements in the vector to be large to small to sort,

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>//Reverse Need# include<algorithm>using namespacestd;intCMP (Const int& A,Const int&b) {    returnA >b;}intMainvoid) {vector<int>v (Ten);  for(inti =0; I <Ten; i++) {V[i]=i; }     for(inti =0; I <Ten; i++) cout<<v[i]<<" "; cout<<Endl; //sort the elements in the vector container according to the CMP we wrotesort (V.begin (), V.end (), CMP);  for(inti =0; I <Ten; i++) {cout<<v[i]<<" "; } cout<<Endl; return 0;}

1.9 Size of the vector

Using the size () method returns the size of the vector, which means the number of elements in the vector container.

Use the empty () method to return whether the vector is empty.

The following code demonstrates how size () and empty () are used

//Cin,cout Need# include<iostream>//Vectot Need# include<vector>//Reverse Need# include<algorithm>using namespacestd;intMainvoid) {vector<int>v (Ten);  for(inti =0; I <Ten; i++) {V[i]=i; }     for(inti =0; I <Ten; i++) cout<<v[i]<<" "; cout<<Endl; cout<<v.size () <<Endl; //Note: The vector container does not have the length function. //cout<<v.length () <<endl;Cout<<v.empty () <<Endl;    V.clear (); //if the vector container is empty, then return 1, if not empty, return 0Cout<<v.empty () <<Endl; return 0;}

These 9 classes are all about vector's simplest application, and if we want to learn more about vector, we can learn it in the STL's detailed book.
Vector<>v, the elements loaded into the container can be more types such as char,string,double, or it can be a struct, so it is very flexible to use.

Vector vectors container

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.