C ++ data type-Vector

Source: Internet
Author: User
Vector provides an alternative to built-in arrays, called vectors, and is a type of STL container.
* Reference header files
# I nclude
# I nclude

# If _ msc_ver> 1020
Using namespace STD;
# Endif

* Definition
Vector Ivec (10); // The initialization size is 10 elements.
Vector Ivec (10, 8); // use 8 to initialize each element.

Char A [10] = {1, 2, 8, 7, 20, 88, 22, 35, 72 };
Vector Ivec (A, A + 4); // initialize with an existing array and give the start address and end address respectively.
Vector Ivec (& A [3], & A [6]);

Vector cannot use the definition method below the Array
Vector Ivec = {2, 8, 3}; // Error

Vector Ivec2 = ivec; // you can directly assign values using another vector of the same type.
* Size () query size. Check whether empty () is empty.
If (ivec. Empty ())
Return;

* Push_back (): inserts an element.
Ivec. push_back (8 );
The size of the extended vector does not overwrite the original element.

* Access by ARRAY
Vector Ivec (10 );
For (INT I = 0; I : Iterator it = ivec. Begin (); it! = Ivec. End (); ++ it)
{
Printf ("% d/N", * it );
}

Bool is_equal (const int * IA, int ia_size,
Const Vector & Ivec)
{
If (ia_size = ivec. Size () return true;
Int I = 0;
For (vector : Const_iterator it = ivec. Begin (); it! = Ivec. End (); It ++)
{
If (* It! = Ia [I ++])
Return false;

If (I = ia_size)
Break;
}
Return true;
}
When declared as const Vector & Ivec, use the constant iteration object Vector : Const_iterator 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.