Summary of the usage of vector in C ++

Source: Internet
Author: User

Summary of the usage of vector in C ++

This article mainly introduces that vector is a very useful container in c ++. Let's make a summary of this container.

1. Basic operations

 

(1) header file # include <vector>.

 

(2) create a vector object, vector <int> vec;

 

(3) Insert numbers at the end: vec. push_back ();

 

(4) use a subscript to access the element. cout <vec [0] <endl; remember that the subscript starts from 0.

 

(5) use an iterator to access elements.

 

 

The Code is as follows:

Vector <int>: iterator it;

For (it = vec. begin (); it! = Vec. end (); it ++)

Cout <* it <endl;

 

 

(6) insert element: vec. insert (vec. begin () + I, a); insert a before the I + 1 element;

 

(7) delete element: vec. erase (vec. begin () + 2); Delete 3rd Elements

 

Vec. erase (vec. begin () + I, vec. end () + j); Delete interval [I, J-1]; interval starts from 0

 

(8) vector size: vec. size ();

 

(9) clear: vec. clear ();

 

2

 

The element of a vector can not only be int, double, string, but also struct. Note that the struct must be defined as global; otherwise, an error may occur. The following is a short program code:

 

 

The Code is as follows:

# Include <stdio. h>

# Include <algorithm>

# Include <vector>

# Include <iostream>

Using namespace std;

 

Typedef struct rect

{

Int id;

Int length;

Int width;

 

// For a vector element that is a struct, you can define a comparison function within the struct, Which is sorted in ascending order by id, length, and width.

Bool operator <(const rect & a) const

{

If (id! = A. id)

Return id <a. id;

Else

{

If (length! = A. length)

Return length <a. length;

Else

Return width <a. width;

}

}

} Rect;

 

Int main ()

{

Vector <Rect> vec;

Rect rect;

Rect. id = 1;

Rect. length = 2;

Rect. width = 3;

Vec. push_back (rect );

Vector <Rect>: iterator it = vec. begin ();

Cout <(* it). id <''<(* it). length <'' <(* it). width <endl;

 

Return 0;

 

}

 

 

3 Algorithm

 

(1) Use reverse to flip the element: header file # include <algorithm>

 

Reverse (vec. begin (), vec. end (); flip the element (in vector, if two iterators are required in a function,

 

Generally, the last one does not contain .)

 

(2) sort by sort: the header file is required # include <algorithm>,

 

Sort (vec. begin (), vec. end (); (Ascending by default, that is, ascending ).

 

You can rewrite the sort comparison function to compare data in descending order, as shown below:

 

Define the sorting comparison function:

 

 

Copy the Code as follows:

Bool Comp (const int & a, const int & B)

{

Return a> B;

}

 

When calling: sort (vec. begin (), vec. end (), Comp) to sort in descending order.

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.