Detailed usage of C + + vectors

Source: Internet
Author: User

Vector container Type
A vector container is a template class that can hold objects of any type (but must be of the same class object). Vector objects can add elements efficiently at run time, and elements in the vector are stored continuously.
The structure of vectors

Function Prototypes:
Template<typename t>
explicit vector (); Default constructor, vector object is empty
Explicit vector (size_type n, const t& v = T ()); Creating a Vector object with n elements
Vector (const vector& x);
Vector (const_iterator first, const_iterator last);

Note: All objects stored in the vector container are initialized. If you do not specify the initial value of the storage object, then for the built-in type will be initialized with 0, for the class type will call its default constructor for initialization (if there are other constructors without a default constructor, then you must provide the element initial value to be placed in the container).

Example:
Vector<string> v1; Creates an empty container whose object type is the String class
Vector<string> v2 (10); Create a container of 10 string class objects with an initial value (that is, an empty string)
Vector<string> v3 (5, "Hello"); Create a container for a string class object with 5 values of "Hello"
Vector<string> v4 (V3.begin (), V3.end ()); V4 is the same container as the V3 (full replication)

Vector operation (The following function is a member function)

BOOL empty () const; Returns true if the container is empty, otherwise false
Size_type max_size () const; Returns the maximum number of elements the container can hold
Size_type size () const; Returns the number of elements in a container
Size_type capacity () const; The number of elements the container can store: capacity () >= size ()
void Reserve (Size_type n); Make sure Capacity () >= n
void Resize (size_type n, t x = t ()); Ensure that after return, there is: size () = = N; if the size () is <n, then the value of element x is complete.

Reference Front (); Returns a reference to the first element in the container (the container must be non-empty)
Const_reference Front () const;
Reference back (); Returns a reference to the last element in the container (the container must be non-empty)
Const_reference back () const;

Reference operator[] (size_type POS); Returns a reference to the element that is labeled POS (the subscript starts at 0; if the subscript is incorrect, it is undefined behavior.)
Const_reference operator[] (size_type pos) const;
Reference at (size_type POS); Returns a reference to the element labeled POS, or an exception if the subscript is incorrect Out_of_range
Const_reference at (size_type pos) const;

void push_back (const t& x); Add an element to the end of the container
void Pop_back (); POPs the last element in the container (the container must be non-empty)

Note: The following insert and delete operations will take place element movement (in order to maintain the nature of continuous storage), so the previous iterator may fail
Iterator insert (iterator it, const t& x = T ()); Insert element before insertion point element (or insert element at insertion point)
void Insert (iterator it, Size_type N, const t& x); Note iterators may no longer be valid (may reallocate space)
void Insert (iterator it, Const_iterator first, const_iterator last);

Iterator Erase (iterator it); Deletes the specified element and returns the position of an element after the element is deleted (if no element, returns end ())
Iterator Erase (iterator first, iterator last); Note: After deleting an element, the iterator corresponding to the element after the delete point is no longer valid.

void clear () const; Empties the container, which is equivalent to calling erase (begin (), End ())

void Assign (size_type n, const t& x = T ()); assignment, replacing all elements within the container with the specified sequence of elements
void Assign (Const_iterator first, const_iterator last);

Const_iterator begin () const; Iterative sequences
Iterator begin ();
Const_iterator end () const;
Iterator End ();

Const_reverse_iterator rbegin () const;
Reverse_iterator Rbegin ();
Const_reverse_iterator rend () const;
Reverse_iterator rend ();

Comparison of Vector objects (non-member functions)

There are six comparison operators for vector objects: operator==, operator!=, operator<, operator<=, operator>, operator>=.

For operator== and operator!=, if the vector object has the same number of elements, and the corresponding position elements are all equal, then two vector objects are equal;
For operator<, operator<=, operator>, operator>=, a dictionary sorting strategy is used.

Note: In fact, only need to implement operator== and operator!= can be, the other can be based on these two implementations. Because, operator!= (LHS, RHS) is! (LHS = = RHS), operator<= (LHS, RHS) is! (RHS < LHS),operator> (LHS, RHS) is (RHS < LHS), operator>= (LHS, RHS) is! (LHS, RHS).

An iterator to the vector class

The vector class iterator supports arithmetic operations in addition to the universal prefix increment operator: it + N, it-n, it2-it1. Note The It2-it1 return value is Difference_type (signed type).

Note that any operation that alters the size of the container can cause the previous iterator to fail.

Application examples

#include <iostream>
#include <cassert>
#include <vector>

using namespace Std;

int main ()
{
Vector<string> V (5, "Hello");
Vector<string> v2 (V.begin (), V.end ());

assert (v = = v2);

cout<< "> Before operation" <<endl;
for (Vector<string>::const_iterator it = V.begin (); it < V.end (); ++it)
cout<<*it<<endl;

V.insert (V.begin () + 3, 4, "Hello, World");
cout<< "> After Insert" <<endl;
for (Vector<string>::size_type i = 0; i < v.size (); ++i)
cout<<v[i]<<endl;

Vector<string>::iterator it = v.erase (V.begin () + 3, V.begin () + 6);
ASSERT (*it = = "Hello, world");
cout<< "> After Erase" <<endl;
for (Vector<string>::size_type i = 0; I! = V.size (); ++i)
cout<<v[i]<<endl;

ASSERT (V.begin () + v.size () = = V.end ());
ASSERT (V.end ()-v.size () = = V.begin ());
ASSERT (V.begin ()-v.end () = =-vector<string>::d Ifference_type (V.size ()));

return 0;
}
Program Description: The above program uses three loops to output the elements in the container, each loop traversal way is not the same. It is particularly necessary to note that the second loop uses the size () function in conditional judgment instead of saving it in a variable before the loop. There are two reasons to do this: one, if you modify a program in the future, the number of container elements is modified in the loop, the loop still works well, and if the value of the size () function is not correct first; Since these small functions (whose implementations require only one return statement) are basically declared inline, there is no need to consider efficiency issues.
There are a lot of suggestions on the Internet. You'd better buy an STL and see what's in it. More detailed and more basic

Detailed usage of C + + vectors

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.