C ++ STL learning notes 2 vector container

Source: Internet
Author: User

/*
*
**************************************** ****
* Basic instructions for vector containers:
**************************************** ****
*
* Random access is allowed, and new elements are inserted at the end of the container.
* Random Access container and back Insertion Sequence
* The time complexity for inserting and deleting elements at the end is O (1), the other positions are O (n), and N is the number of elements.
* The occupied space of inserted elements can be dynamically adjusted.
* The macro statement # include <vector> must be used to use vector.
*
**************************************** **************************************** ******
*
* Create a vector object:
* 1. Vector <int>;
* 2. Vector <int> A (10); // object a with 10 elements. The default value of each element is 0.
* 3. Vector <char> A (5, 'k ');
* 4. Vector <char> B (a); // vector <char> C (A. Begin (), A. End ())
*
**************************************** **************************************** ******
*
* Initialization assignment
* Void push_back (const T & value)
*
**************************************** **************************************** ******
*
* Traversal
* Reference operator [] (size_type N) // you can use arrays to access the vector element.
*
**************************************** **************************************** ******
*
* Common functions
*
* Bool empty ();
* Size_type size (); size_type max_size (); size_type capacity ();
* Reference Front (); reference back ();
* Void pop_back (); void push_back ();
* Void clear ();
*
*
*
**************************************** ****
* Author: cumirror
* Email: tongjinooo@163.com
**************************************** ****
*
*/

# Include <iostream>
# Include <vector>

Int main ()
{
Using namespace STD;
Vector <int> A (10, 5 );
// Array
Cout <"array" <Endl;
A [0] = 100;
For (int s = 0; S <A. Size (); s ++ ){
Cout <A [s] <Endl;
}
// Iterator Method
Cout <"iterator" <Endl;
Vector <int>: iterator I, iend;
I = A. Begin ();
Iend = A. End ();
* I = 101;
For (vector <int>: iterator J = I; J! = Iend; j ++ ){
Cout <* j <Endl;
}
// Insert an element before the iterator
// Note: After element insertion, the original iterator will become invalid.
Cout <"insert" <Endl;
A. insert (A. Begin (), 102 );
// Note when deleting a table. It is a semi-closed interval.
// Deletion of a single element of erase (iterator POS) is also supported.
Cout <"delete" <Endl;
A. Erase (A. Begin () + 4, A. Begin () + 6 );
For (vector <int>: iterator K = A. Begin (); k! = A. End (); k ++ ){
Cout <* k <Endl;
}
// Reverse Traversal
Cout <"reverse access" <Endl;
Vector <int>: reverse_iterator Ri;
For (Ri = A. rbegin (); Ri! = A. rend (); ri ++ ){
Cout <* RI <Endl;
}
Return 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.