Vector vectors container

Source: Internet
Author: User

Vector containers are a generalization of arrays, not only random access to elements like arrays, but also the insertion of new elements at the end of a container, which implements the concept of random access container and back insertion sequence. Vector has an automatic memory management function, which can dynamically adjust the occupied memory space for the insertion and deletion of elements.

Creating a Vector Object

There are many ways
(1) vector (Consta&a=a ())
Create an empty vector object, A is a memory allocator, this parameter can be omitted, equivalent to a vector () call, such as:

vector<int> v;

(2) vector (size_type N)
Creates a vector object with n elements, each of which has a default value under the corresponding type, and the memory space for n vector elements has been allocated, such as:

vector<double> v(10);//每个元素的默认值为0.0

(3) vector (Size_type n,const t&value)
Creates a vector object with n elements, each with an initial value of Valuevector V (10,9.9); Vector (const vector&)
Create a new vector object by copying the values of each element of a vector object. Such as:

vector<char> v1(10,’c’);vector<char> v2(v1)

(5) Vector (const inputiterator First, const inputiterator last,const a&a=a ())
The inputiterator is an input iterator, by copying the iteration interval [first,last), such as:

int iArray[]={1,3,5,7,9vector<int> v(iArray,iArray+5);
Vector initialization

The vector container is initialized using the Push_back function provided by the vector. Such as:

vector<int> v;v.push_back(10);
Traversal access of elements

The elements of a vector can be traversed through an array or an iterator.
Access vector elements using an array:

#include <vector>#include <iostream>using namespace STD;intMain () { vector<int>V V.push_back (1); V.push_back (2); V.push_back (3); V.push_back (4); V.push_back (5); for(intI=0; I<v.size (); i++) {cout<<"v["<<i<<"]="<<v[i]<<endl; }return 0;}

The vector provides the begin () and end () functions, which are used to get iterators for the first element and the next position of the last element, using iterators to access the vector elements:

#include <vector>#include <iostream>using namespace STD;intMain () { vector<int>V V.push_back (1); V.push_back (2); V.push_back (3); V.push_back (4); V.push_back (5); vector<int>:: Iterator Begin,end; End=v.end ();intJ for(Begin=v.begin (), j=0; begin!=end;begin++,j++) {cout<<"v["<<j<<"]="<<*begin<<endl; }return 0;}
Insertion of elements

Push_back () Adds an element at the tail of the vector container, and the Insert function can insert the element anywhere.

#include <iostream>#include <vector>using namespace STD;intMain () { vector<int>V V.push_back (1); V.push_back (2); V.push_back (3); V.push_back (4); V.insert (V.begin () +3,5);//Insert 3 in front of 4V.insert (V.begin (),6);//Insert 6 for the first elementV.insert (V.end (),7);//Insert 7 for end element     for(intI=0; I<v.size (); i++) {cout<<"v["<<i<<"]="<<v[i]<<endl; }return 0;}
Deletion of elements

The erase function is used to delete all elements of an iterator or iterator interval [first,last] that are referred to by the iterators POS, and the prototype is as follows:

iterator erase(iterator pos);iterator erase(iterator first,iterator last);

Example:

#include <iostream>#include <vector>using namespace STD;classmyanimal{ Public:Char*name;intAge MyAnimal (Char*name,intAge) { This->name=name; This->age=age; } ~myanimal () {}};intMain () {MyAnimal *pdog=NewMyAnimal ("Dog",1); MyAnimal *pmonkey=NewMyAnimal ("Monkey",2); MyAnimal *pcat=NewMyAnimal ("Cat",3); MyAnimal *psnake=NewMyAnimal ("Snake",4); vector<myanimal *>V//v storing addresses for individual objectsV.push_back (Pdog);    V.push_back (Pmonkey);    V.push_back (PCat); V.push_back (Psnake);DeletePmonkey;//physical deletion of objects referred to by PmonkeyV.erase (V.beigin () +1);//Delete element 2nd     vector<myanimal *>:: Iterator Begin,end; End=v.end (); for(Begin=v.begin (); begin!=end;begin++) {cout<< (*begin)->name<<" "<< (*begin)->age<<endl; } v.clear ();cout<<"Execute clear,vector elements are all cleared"    return 0;}
Reverse traversal of elements

The vector's inverse iterator reverse_itrator and the corresponding rbegin and rend functions can be used to traverse the elements of the vector container in reverse.

#include <iostream>#include <vector>using namespace STD;intMain () { vector<int>V V.push_back (1); V.push_back (2); V.push_back (3); V.push_back (4); V.push_back (5); vector<int>:: Reverse_iterator rbegin,rend; Rend=v.rend (); for(Rbegin=v.rbegin (); rbegin!=rend;rbegin++) {cout<<*rbegin<<endl; }return 0;}
Exchange of vectors

The swap algorithm is used to realize the exchange of two vector container elements, the prototype is as follows:

void swap(vector &)

Cases:

#include <iostream>#include <vector>using namespace STD;voidPrint vector<int>& V) { for(intI=0; I<v.size (); i++)cout<<v[i]<<" ";cout<<endl;}intMain () {//v1     vector<int>v1; V1.push_back (1); V1.push_back (2);cout<<"-------v1 and V2 before Exchange"<<endl;cout<<"v1="; Print (v1);//v2     vector<int>v2; V2.push_back (3); V2.push_back (4); V2.push_back (5); V2.push_back (6);cout<<"v2="; Print (v2);//v1 and v2 ExchangeSwap (V1,V2);cout<<"-------v1 and V2 after Exchange"<<endl;cout<<"v1="; Print (v1);cout<<"v2="; Print (v2);return 0;}

Other functions

BOOL Empty ()
Determines whether the empty
Size_type size ()
Actual number of elements
Size_type max_size ()
Maximum number of elements allowed by the system for vector containers
Size_type Capacity ()
The number of vector elements that can now be accommodated, and the reserve function can adjust the size of the data space
Reference Front ()
Reference to the first element of the container
Reference back ()
A reference to the end element of the container
void Pop_back ()
In contrast to Push_back, Pop_back is used to delete an element at the end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.