Some summarization of vector containers in STL _c language

Source: Internet
Author: User
Tags array length data structures

A brief introduction to 1.vector

Vector, as one of the standard containers provided by STL, is often used and has a very important position, and it is also easy to use. Vector, also known as vectors, can be depicted as an array of lengths that can be dynamically altered, with similar functions and arrays. In fact, the more professional description is that vector is a versatile template class and function library that can manipulate a variety of data structures and algorithms, and the vector is considered a container because it can store various types of objects like a container, simply put, A vector is a dynamic array of any type that can be stored to add and compress data. (Note: The STL container can be said to be a class template from the implementation point of view (class Teplate). )

So what is the main difference between a vector and an array? This is very helpful for understanding vectors ~ ~ ~ ~

Arrays: The allocation of static space, the general distribution can not be changed, as we know the definition of an array, then the length of the array can not be changed, we can not do cross-border access, but the compiler does not check the bounds, This is especially true when we are programming (a lot of the bugs are probably annoying!!) )。 The array length of the general application does not meet our requirements, we need to reapply the larger array, and then copy the data from the original array.

Vector: The allocation is dynamic space, namely: we found that when declaring a vector container can also not specify the size of the container, vector is with the addition of elements, space automatically extended. However, we must be responsible for affirming that the vector allocated space is continuous, that is, to support the subscript random access in the array, in fact, vector implementation mechanism is: reserving a part of the space, and the size of the reservation is increased by a certain ratio, if the space is not enough, to ensure continuous, You have to get new space, and then move the original elements to the new space, while reserving the space (and the new allocated space than the original allocated space), and finally release the original part of the space. The advantage of reserving space is that you don't have to redistribute space every time you add elements to the vector.

Functions commonly used in 2.VECOTR containers

Constructor for 2.1.vector Container

The way vector containers are declared consists of a few:

--------------------------------------------------------------------------------

Vector<elem> V, create an empty vector.

Vector <Elem> V1 (v), copying a vector.

Vector <Elem> V (n), create a vector containing n data, all of which are created by default.

Vector <Elem> V (n, Elem), create a vector containing n Elem copies.

Vector <Elem> V (beg,end), create a vector in the [beg;end] interval.

v.~ Vector <Elem> (), destroying all data, freeing up memory.

--------------------------------------------------------------------------------

Here's a piece of code to illustrate several common ways to declare vectors:

Copy Code code as follows:

#include <iostream>
#include <vector>

using namespace Std;

int main ()
{
Vector<int>::iterator ITER;
The first way
Vector<int> v1;
V1.push_back (1);
V1.push_back (2);
V1.push_back (3);
cout<< "The output of the first way:" <<endl;
for (iter = V1.begin (); Iter!= v1.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
The second way
Vector<int> v2 (v1);
cout<< "The output of the second way:" <<endl;
for (iter = V2.begin (); Iter!= v2.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
The Third Way
Vector<int> v3 (3);
cout<< "The output of the third way:" <<endl;
for (iter = V3.begin (); Iter!= v3.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
Fourth Way
Vector<int> v4 (3,4);
cout<< "The output of the fourth way:" <<endl;
for (iter = V4.begin (); Iter!= v4.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
Fifth Way
Vector<int> V5 (V1.begin (), V1.end ()-1);
cout<< "The output of the fifth way:" <<endl;
for (iter = V5.begin (); Iter!= v5.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
Sixth Way
int a[] = {1,2,3,4};
vector<int> V6 (A+1,A+2);
cout<< "The output of the sixth way:" <<endl;
for (iter = V6.begin (); Iter!= v6.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
//
V6.~vector<int> ();
cout<< "The result of releasing memory is:" <<endl;
for (iter = V6.begin (); Iter!= v6.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl;
return 0;
}


Run Result:

Summary: Note this: vector <Elem> C (beg,end) declarative way to create a vector that is the same as the [beg;end] interval element, be sure to be aware of the left-right open range, and the need to say, STL whether the container or algorithm are used in this left-right open range, including the V.end () function is the return vector at the end of the position, the equivalent of int a[n] a[n], and can not visit ~ ~ ~

Other commonly used functional usage in 2.2.vector

--------------------------------------------------------------------------------

V.assign (beg,end) assigns the data in the [Beg end) interval to V.

V.assign (N,elem) assigns a copy of n elem to V.

v.at (IDX), returns the index IDX the data indicated, if IDX crosses the line, throws the Out_of_range.

V.begin (), returns the iterator's one-valued data.

V.capacity () returns the number of data in the container.

V.clear () Removes all data from the container.

V.empty () to determine whether the container is empty.

V.end (), pointing to the last data address in the iterator.

--------------------------------------------------------------------------------

Use the function mentioned above to write a program and rehearse it:

Copy Code code as follows:

#include <iostream>
#include <vector>

using namespace Std;

int main ()
{
Vector<int>::iterator ITER;
vector<int>v1;
int a[] = {1,2,3,4};

Program Section 1, Practice assign (N,T)
V1.assign (3,2);
cout<< "The elements in the vector:";
for (iter = V1.begin (); Iter!= v1.end (); ++iter)
{
cout<<*iter<< "";
}
cout<<endl<<endl;

Program Section 2, Practice assign (Beg,end)
V1.assign (A,A+4);
cout<< "The length of the vector is:" <<v1.capacity () <<endl;
cout<< "The elements in the vector:";
for (int i = 0; i < 4; ++i)
{
cout<<v1.at (i) << "";
}
cout<<endl<<endl;

Program section 3, practice the clear () function and the Enpty () function
V1.clear ();
if (V1.empty ())
{
cout<< "vector is empty!!!" <<endl;
}

return 0;
}


Run Result:

Summary: About the Assign function, the vector variable is assigned, and the vector size can be modified automatically.

--------------------------------------------------------------------------------

V.insert (Pos,elem) inserts a elem copy at the POS position and returns the new data position (the position refers to the return address value).

V.insert (Pos,n,elem) inserts data in the [Beg,end] interval at the POS position. no return value.

V.insert (pos,beg,end) inserts n elem data at the POS position. no return value.

V.erase (POS) deletes data from the POS location and returns the location of the next data.

V.erase (beg,end) deletes the data from the [beg,end) interval and returns the location of the next data.

--------------------------------------------------------------------------------

Look at the insertion and deletion of elements in the vector:

Copy Code code as follows:

#include <iostream>
#include <vector>

using namespace Std;

int main ()
{
int a[] = {2,3,4};
Vector<int> v1;
Vector<int>::iterator ITER;

Demo Insert function
V1.insert (0,1);
V1.insert (V1.begin () +1,a,a+3);
V1.insert (V1.begin () +4,2,5);
cout<< "The data in the vector:";
for (iter = V1.begin (); Iter!= v1.end (); ++iter)
{
cout<<*iter<< "";
}
cout<<endl<<endl;
Demo Erase function
V1.erase (V1.begin (), V1.begin () +2);
V1.erase (V1.begin () +1);
cout<< "The data in the vector:";
for (iter = V1.begin (); Iter!= v1.end (); ++iter)
{
cout<<*iter<< "";
}
cout<<endl<<endl;
return 0;
}


Run Result:

Summary: Note that the POS parameters for insert and delete operations are passed in by iterators. Also note the return values of several insert functions.

--------------------------------------------------------------------------------

V.capacity () returns the number of data in the container.

V.size () returns the number of actual data in the container.

V.reserve () retains the appropriate capacity.

V.resize (num) again specifies the length of the queue.

V.max_size () returns the maximum number of data in the container.

--------------------------------------------------------------------------------

Copy Code code as follows:

#include <iostream>
#include <vector>

using namespace Std;

int main ()
{
Vector<int> v1 (4,1);
Vector<int>::iterator ITER;
cout<< "The value of the size of the vector:" <<v1.size () <<endl;
cout<< "Vector capacity Value:" <<v1.capacity () <<endl;
cout<< "Vector max_size value:" <<v1.max_size () <<endl;

Using the Reserve function
V1.reserve (6);
cout<<endl;
cout<< "The value of the size of the vector:" <<v1.size () <<endl;
cout<< "Vector capacity Value:" <<v1.capacity () <<endl;
cout<< "Vector max_size value:" <<v1.max_size () <<endl;
cout<< "The elements of the vector are:";
for (iter = V1.begin (); Iter!= v1.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl<<endl;

Using the Resize function
V1.resize (6,2);
cout<<endl;
cout<< "The value of the size of the vector:" <<v1.size () <<endl;
cout<< "Vector capacity Value:" <<v1.capacity () <<endl;
cout<< "Vector max_size value:" <<v1.max_size () <<endl;
cout<< "The elements of the vector are:";
for (iter = V1.begin (); Iter!= v1.end (); iter++)
{
cout<<*iter<< "";
}
cout<<endl<<endl;
return 0;
}


Output results:

Summary:the vector reserve increases the vector's capacity, but its size does not change! And resize changed the vector's capacity and also increased its size!. This is because: (1) The reserve is reserved for the container space, but in the space does not really create the element object, so before adding a new object, cannot refer to the elements inside the container. When you add a new element, you invoke the Push_back ()/insert () function. (2) Resize changes the size of the container and creates the object, so the function is invoked to refer to the object within the container, so when the new element is added, the operator[operator is used, or an iterator is used to refer to the element object. The call to the Push_back () function at this point is appended to the new space.

--------------------------------------------------------------------------------

C.rbegin () returns the first data of a reverse queue.

C.rend () returns the next position of the last data in a reverse queue.

C.pop_back () deletes the last data.

C.push_back (elem) adds a data to the tail.

C.front () returns a data.

C.back () returns the last data and does not check whether the data exists.

C1.swap (C2) swaps C1 and C2 elements.

Swap (C1,C2) ibid.

--------------------------------------------------------------------------------

These functions are relatively simple, here will not write the program, interested in their own practice it!!!

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.