Notes for using Vector (1)

Source: Internet
Author: User

Vector is a many C ++ STL class. We haven't systematically seen how to use vector before, which makes it difficult to use it. Vector is used to replace directly defined arrays. It can perform subscript out-of-bounds checks and dynamically increase the size. We always recommend that you use vector instead of Type array [size]. When vector is used for the first time, an error is reported,

vector<int> vInt;
vInt[0] = 1;
cout << vInt[0] << endl;

Segmetation fault. So I changed it,

vInt.push_back(1);

That's right. Later, I used it like this. Recently I found myself too stupid, so I systematically looked at the use of vector.

1. vector Constructor

Http://www.cplusplus.com/reference/stl/vector/vector/

explicit vector ( const Allocator& = Allocator() );
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
template <class InputIterator>
vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
vector ( const vector<T,Allocator>& x );

There are also reference examples. We should have a look. As follows,

// Constructors used in the same order as described above:
// Define an empty vector without allocating memory space. This is why vInt [0] = 1; // The Value assignment causes Segmentation fault. When push_back () is used, an element is appended and the space is allocated again. No error occurs.
Vector <int> first; // empty vector of ints
// Define a vector with four elements whose initial values are 100. You can also choose not to give an initial value, but to specify the size.
Vector <int> second (4,100); // four ints with value 100
// Use the vector iterator to initialize the source vector.
Vector <int> third (second. begin (), second. end (); // iterating through second
// Initialize with a vector
Vector <int> fourth (third); // a copy of third

// The iterator constructor can also be used to construct from arrays:
Int myints [] = {16, 2, 77,29 };
// Use the start address and end address of array for initialization
Vector <int> TH (myints, myints + sizeof (myints)/sizeof (int ));

2. Access and assignment of vector Elements

For a single element

Operator [], subscript operator, which is consistent with the access array. It does not check whether the subscript is out of bounds.

At (size_type n), which performs subscript check, returns a reference, or const reference, and can be used to access and assign values. We recommend that you use at () instead of [] Access vectors. When the subscript is out of range, [] is used to report segmetation fault and at () is used to report out of range. The error information is clearer.

When [] or at () elements are in the vector, they can be used to assign values.

Sequential Traversal

for(unsigned int i=0;i<vInt.size();i++)
{
cout << vInt[i] <<"  ";
}

The size () function of the vector returns the number of elements in the vector, which is the number of unsigned elements. Therefore, the definition of unsigned int I has a problem, when accessing the first element from the last element, for (unsigned int I = vInt. size ()-1; I> = 0; I.

Containers in STL all have a mechanism called iterator. You can use the iterator to traverse a vector. As follows,

For (vector <int >:: iterator it = vi. begin (); it! = Vi. end (); it ++) // The second call Method
{
Cout <* it <"";
}

3. dynamic growth of vector

One advantage of vector is that it can grow dynamically, although at the cost of performance. If you need to append the element push_back or pop_back, the size of the vector will change accordingly.

4. swap () function

Swap is still very useful. As mentioned in the previous article about the release of vector memory, swap is used to release the memory occupied by the vector. For example, a polynomial multiplication function,

Void polyMultiAssign (vector <double> & ak, vector <double> & bk)
{
// Apply for a vector with the size of na + nb-1 to store the results
Vector <double> ck (ak. size () + bk. size ()-1 );
//... Operation
// Save the result to the ak,
Ak. swap (ck );
// Save the result in the ak, and release the result when the function in the ck ends.
}

4. allocator of the vecrtor

The allocator parameter can be input in the vector constructor. The default allocator parameter is generally used.

This example is mentioned in some places,

Vector <int> vd;
// Use allocator of vector to apply for space,
Int * p = vd. get_allocator (). allocate (10 );
P [0] = 1;
...
// Use allocator of vector to release space,
Vd. get_allocator (). deallocate (p, 10 );

The allocator of the vector is used here, not the vector itself. What is the difference between this and applying for an Array Using new/delete? It seems that there is no difference.

int *p = new int[10];
delete [] p;

We hope that we can better understand vector in the future.

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.