C + + Primer (Fifth edition) learning Note _2_ Standard Template Library vector (1)

Source: Internet
Author: User

C + + Primer (Fifth edition) learning Note _2_ Standard Template Library vector (1)

Welcome to read the reference, if there are errors or questions, please leave a message to correct, thank you

Vector container vectors not only can be randomly accessed as arrays, but can also be used to insert elements at the tail, which can replace arrays.

It is important to note that vector has the function of automatic memory management, for the insertion and deletion of elements, can dynamically adjust the occupied memory space.

The subscript of the container vector starts at 0, and if the size of the vector container is n, the element is labeled 0~n-1, which is the same as the array. Not the same, the vector can be resized at any time.

Vector has three important methods: Use the Push_back () method to expand the element from the tail, insert a new element before an element position using the Insert () method, return the size of the container using the size () method, and use the end () to return the position of the first element; An iterator that returns the position of the next element of the last element. It should be noted that the iterator of iterators should be utilized in STL, and not be limited by the idea of array subscript.

The element type of a vector can be a simple type such as Int,double,char, or it can be a struct or string basic character sequence container.

1. Create a Vector object

There are three common ways to create vector objects:

(1) Do not specify the number of elements of the container:

vector<int>v;

(2) When created, specifies the size of the container, such as defining a vector container that stores 10 double elements.

Vector<double>v (10); Each element is initialized to 0.0

(3) Create a vector container with 10 elements, each with a specified initial value of 8.6.

Vector<double>v (10, 8.6);

Vector<int>v ({1, 2}); Create a vector that contains 1, 2

Vector<int>v (V1.begin (), V1.end ()); Copy the v1 to vector v

(4) V.reserve (k) indicates that the container reserves k space capacity, or capacity, which is the total number of elements that the container can store before allocating new storage space, but is not a true object creation and requires creation of objects through insert () or push_back ().

(5) V.resize (k) Allocates space and creates objects. The usage is similar to (2), V.resize (10), each element is initialized to 0.0, and the space is reassigned to V.

Conclusion: Reserve only modifies the capacity size, does not modify the size, resize both modifies the capacity size, and also modifies size.


#include <iostream> #include <vector> usingnamespace std; int main (INTARGC, char* argv[]) {    vector<int> num;    cout << "Initial capacity:" << num.capacity () <<endl;    cout << "Initial size:" << num.size () << Endl;     Num.reserve (ten);    cout << "Num's capacity:" << num.capacity () <<endl;    cout << "num size:" << num.size () << Endl;     Num.resize (a);    cout << "Num's capacity:" << num.capacity () <<endl;    cout << "num size:" << num.size () << Endl;    return 0;}

Operation Result:

The initial capacity:0

The initial size:0

Num's Capacity:10

Num's size:0

Num's capacity:20

Num's size:20

(6) with the Assign () method, the new element can be re-assigned to the vector container and the contents of the old vector container will be cleared. Usage is similar to (3).

V.assign (10,8.6);

V.assign ({); Create a vector that contains 1, 2

V.assign (V1.begin (), V1.end ()); Copy the v1 to vector v

2. Tail element expansion

A new element is usually appended to the tail of the vector container using push_back (). The vector container automatically allocates new memory space. You can either expand on an empty vector object, or expand the vector object of an existing element.

The following code adds 2,7,9 three elements from the trailer to the NUM container, so that there are three elements in the NUM container, with the values sequentially 2,7,9.

#include <iostream> #include <vector> using namespace std; int main (int argc, char* argv[]) {    vector<int> num;    Num.push_back (2);    Num.push_back (7);    Num.push_back (9);     return 0;}

3. Access vector elements in subscript mode

accessing or traversing a vector object is a common thing to do. For vector objects, you can use subscript to arbitrarily access an element, or you can use subscript to reassign an element, similar to how an array is accessed.

#include <iostream> #include <vector> using namespace std; int main (int argc, char* argv[]) {    vector<int> num;    Num.push_back (2);    Num.push_back (7);    Num.push_back (9);     for (int i = 0; i < num.size (); i++)        cout << num[i] << "";    cout << Endl;    return 0;}

Operation Result:

2 7 9

4, the Subscript method is assigned value, when the size of the vector container has been exceeded, you can not use subscript container assignment. However, you can also use the Push_back () method to assign a value.

#include <iostream> #include <vector> using namespace std; int main (int argc, char* argv[]) {    vector<int> num (3);       Num[0] = 2;    NUM[1] = 7;    NUM[2] = 9;       Num.push_back (one);     for (int i = 0; i < num.size (); i++)        cout << num[i] << "";    cout << Endl;    return 0;}

Run results

2 7 9 11

5. Using iterators to access vector elements

Iterate through the vector object using the iterator iterator, which must be the same as the element type of the vector object it traverses.

#include <iostream> #include <vector> using namespace std; int main (int argc, char* argv[]) {    vector<int> num;    Num.push_back (2);    Num.push_back (7);    Num.push_back (9);     for (Vector<int>::iterator iter = Num.begin (); Iter!=num.end (); iter++)        cout << *iter << "";    cout << Endl;    return 0;}

Operation Result:

2 7 9


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

C + + Primer (Fifth edition) learning Note _2_ Standard Template Library vector (1)

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.