C + + vector usage detailed _c language

Source: Internet
Author: User
Tags reserved

The vector is part of the C + + Standard Template Library (Stl,standard Template). It is considered a container because it can hold various types of objects like a container, simply: A vector is a dynamic array of any type that can add and compress data.

<vector> header files must be added before using vector containers: #include <vector>;

Vector is the content of an STD named domain, so it needs to be named qualified: Using Std::vector, or you can use the global namespace directly: using namespace std;

Vector member functions

C.push_back (elem) inserts a elem data at the tail end.

Copy Code code as follows:

Vector<int> v;
V.push_back (1);

C.pop_back () deletes the data at the end.
Copy Code code as follows:

Vector<int> v;
V.pop_back ();

C.assign (beg,end) assigns the data of [Beg,end) to a left-right open interval to C.

Vector<int> V1,v2;
V1.push_back (ten);
V1.push_back (a);
V2.push_back (a);
V2.assign (V1.begin (), V1.end ());

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

Copy Code code as follows:

Vector<int> v;
V.assign (5,10)//V 5 10

c.at (int index) returns the data indexed to index, and throws a Out_of_range exception if the index crosses the bounds.

Vecto<int> v;
cout << v.at (2) << endl;//print vector lower mark is 2 data

C.begin () returns an iterator that points to the first data.

C.end () returns the iterator that points to the last data.

Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
Vector<int>::iterator it;
for (it = V.begin (); It!=v.end (); it++) {
  cout << *it << "\ t";
}
cout << Endl;

C.rbegin () returns the first data of the reverse queue, that is, the last data of the C container.

C.rend () returns the next position of the last data in the reverse queue, where the first data of the C container is moved forward.

Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
Vector<int>::reverse_iterator it;
for (it = V.rbegin (); It!=v.rend (); it++) {
 cout << *it << "\ t";
}
cout << Endl;

C.capacity () returns the number of data in the container, doubling the growth.

Vector<int> v;
V.push_back (1);
cout << v.capacity () << Endl; 1
v.push_back (2);
cout << v.capacity () << Endl; 2
V.push_back (3);
cout << v.capacity () << Endl; 4

C.clear () Removes all data in the container.

Vector<int>::iterator it;
for (it = V.begin (); It!=v.end (); it++) {
 cout << *it << "\ t";
}
V.clear ();
for (it = V.begin (); It!=v.end (); it++) {
 cout << *it << "\ t";
}
cout << Endl;

C.empty () Determines whether the container is empty.

Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
if (!v.empty ()) {
 cout << "V is not empty!" << Endl;  
}

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

Copy Code code as follows:

Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
V.erase (V.begin ());

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

Copy Code code as follows:

Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
V.erase (V.begin (), V.end ());

C.front () returns the first data.

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

Copy Code code as follows:

Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
if (!vec.empty ()) {
cout << "The" the "" the "the" the "the" is: "<< v.front () << Endl;
cout << "The last number is:" << v.back () << Endl;
}

C.insert (Pos,elem) inserts a elem copy at the POS position, returning the iterator for the inserted value.

C.insert (Pos,n,elem) inserts n-elem data at the POS location with no return value.

C.insert (pos,beg,end) inserts the data in the [Beg,end) interval at the POS position, with no return value.

Copy Code code as follows:

Vector<int> v;
V.insert (V.begin (), 10);
V.insert (V.begin (), 2,20);
V.insert (V.begin (), V1.begin (), V1.begin () +2);

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

C.resize (num) again specifies the length of the queue. (often used to increase the length of the vector, small-> big OK big-> small useless!) )

C.reserve () retains the appropriate capacity.

Do a little analysis for resize () and Reserver ():

The reserve is a container reservation, but does not really create an element object and cannot refer to elements within the container until the object is created, so when you add a new element, you need to use the push_back ()/insert () function.

Resize is to change the size of the container and create the object, so after calling the function, you can refer to the object within the container, so when you add the new element, use the operator[operator, or use an iterator to refer to the element object.

Furthermore, two functions in the form of a difference, the reserve function after a parameter, that is required to reserve the space of the container; The resize function can have two parameters, the first parameter is the new size of the container, and the second parameter is the new element to be added to the container, if the argument is omitted, Then the default constructor for the element object is invoked.

Reserve simply guarantees that the vector's space size (capacity) reaches at least the size n specified by its parameters. Within the range [0, n], if the subscript is index,vector[index] Such access may be lawful and may be unlawful, depending on the circumstances.
What the resize and the reserve interfaces have in common is that they ensure that the vector's space size (capacity) reaches at least the size specified by its parameters.

C.max_size () returns the maximum number of container capacity.

C1.swap (C2) will exchange C1 and C2.

Swap (C1,C2) ditto.

Copy Code code as follows:

Vector<int> V1,v2,v3;
V1.push_back (10);
V2.swap (v1);
Swap (V3,V1);

vector<type>c; creates an empty vector container.

Vector<type> C1 (c2); copy a vector.

Vector<type> c (n); Create a vector that contains n data, all of which are generated by default constructs, that is, 0;

Vector<type> C (n,elem) creates a vector containing n-elem copy data.

Vector<type> C (beg,end) creates a vector in the [beg,end] interval.

~vector<type> () destroys all data and casts memory.

Compression of a bloated vector

Many times a large amount of data is deleted, or by using reserver (), the result vector space is much larger than the actual need. So you need to compress the vector to its actual size. Resize () can increase the size of the vector. Clear () only removes the data in the container and cannot change the size of the capacity (), so it is important to compress the vector.

Test the Clear () function:

Copy Code code as follows:

//
Vector.cpp
Vector
//
Created by Scandy_yuan on 13-1-7.
Copyright (c) 2013 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace Std;
int main (int argc, const char * argv[])
{

Insert code here ...
Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
Vector<int>::iterator it;
cout << "clear before:" << "";
For (It=v.begin (); It!=v.end (); it++) {
cout << *it << "T";
}
cout << Endl;
cout << "Clear before Capacity:" << v.capacity () << Endl;
V.clear ();
cout << "After clear:" << "";
For (It=v.begin (); It!=v.end (); it++) {
cout << *it << "T";
}
cout << Endl;
cout << "After clear capacity:" << v.capacity () << Endl;
return 0;
}

Results:

Copy Code code as follows:

Clear Before:1 2 3
Clear before Capacity:4
After clear:
After clear Capacity:4

Why the result of the capacity () printed here is 4 without a detailed explanation, please refer to the above introduction about capacity. With the result, we can see that all the data is cleared after clear (), but capacity () is still 4.

Suppose: we create a new vector from the original vector, let's see what happens?

Copy Code code as follows:

//
Vector.cpp
Vector
//
Created by Scandy_yuan on 13-1-7.
Copyright (c) 2013 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace Std;
int main (int argc, const char * argv[])
{

Insert code here ...
Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
cout << "v.capacity ()" << v.capacity () << Endl;

Vector<int> v1 (v);
cout << "v1.capacity ()" << v1.capacity () << Endl;
return 0;
}

Results:

Copy Code code as follows:

V.capacity () 4
V1.capacity () 3

It can be seen that the V1 capacity () is the actual size of V, so it can be used to compress vectors. But we don't want to create a new one, we want to compress on the original vector (that is, V), and then think about the other way.

Let's say we swap v1 back to V via the SWAP function to see what happens.

Copy Code code as follows:

//
Vector.cpp
Vector
//
Created by Scandy_yuan on 13-1-7.
Copyright (c) 2013 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace Std;
int main (int argc, const char * argv[])
{

Insert code here ...
Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
cout << "v.capacity ()" << v.capacity () << Endl;

Vector<int> v1 (v);
cout << "v1.capacity ()" << v1.capacity () << Endl;

V.swap (v1);
cout << "V.swap (v1) Capacity ()" << v.capacity () << Endl;
return 0;
}

Results:

Copy Code code as follows:

V.capacity () 4
V1.capacity () 3
V.swap (v1). Capacity () 3

It can be seen that v.capacity () has become 3 to achieve. But the code is cumbersome, we reconsider a new way of writing, using anonymous objects instead of V1, the middle object:vector<int> (v). Swap (v);

Test:

Copy Code code as follows:

//
Vector.cpp
Vector
//
Created by Scandy_yuan on 13-1-7.
Copyright (c) 2013 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace Std;
int main (int argc, const char * argv[])
{

Insert code here ...
Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
cout << "v.capacity ()" << v.capacity () << Endl;

Vector<int> (v). Swap (v);
cout << "v.capacity ()" << v.capacity () << Endl;
return 0;
}

Results:

Copy Code code as follows:

V.capacity () 4
V.capacity () 3

You can see that v.capacity () is programmed by 4 for 3 to achieve.

No previous attention was paid to c++11, thanks to @egmkang, indeed the Shrink_to_fit () function has been provided in c++11 to achieve vector compression.

As follows:

Copy Code code as follows:

#include <iostream>
#include <vector>
int main ()
{
Std::vector<int> v;
Std::cout << "default-constructed capacity is" << v.capacity () << ' \ n ';
V.resize (100);
Std::cout << "Capacity of a 100-element vector is" << v.capacity () << ' \ n ';
V.clear ();
Std::cout << "Capacity after Clear ()" << v.capacity () << ' \ n ';
V.shrink_to_fit ();
Std::cout << "Capacity after Shrink_to_fit () is" << v.capacity () << ' \ n ';
}

Results:

Copy Code code as follows:

Default-constructed capacity is 0
Capacity of a 100-element vector is 100
Capacity after Clear () is 100
Capacity after Shrink_to_fit () is 0

Related Article

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.