C + + vector

Source: Internet
Author: User

#include <vector>

Vector<t> v1;

Vector<int> IVEC1;

Vector<string> Svec (IVEC1);

Std::vector<tedit*> MV;

V.empty ()

V.size ()

V.push_back (t)

V[i]

Vec1.front ()//vec1 0th member

Vec1.back ()the last member of the//VEC1

vec1.at (4)the fifth member of//VEC1

VEC1[4]

Vec1.pop_back (); //Move the last member out of the VEC1

Vec1.erase (Vec1.begin () +1,vec1.end ()-2); //Remove members

Tedit *medit=new Tedit ();

V.push_back (Medit);

vector<int>::iterator it;

For (it=vec1.begin (); It!=vec1.end (); ++it)

cout<<it->name;

cout<<' \ n ' <<' vec2: ' <<endl;

typedef vector<int> intvector; Custom Type Intvector

Intvector VEC1;

Sort find
Sort (V.begin (); V.end ());
It=find (V.beggin (); V.end ());
Reverse (V.beggin (); V.end ());

Vector is part of the C + + Standard Template Library, which is a multifunctional template class and function library that can manipulate a variety of data structures and algorithms. Vector is considered to be a container because it can store various types of objects like a container, in short, a vector is a dynamic array that can hold any type and can add and compress data. In order to use the vector, you must include the following code in your header file:

#include <vector>

The vector belongs to the STD named domain, so you need to complete your code by naming the qualification as follows:

Using Std::vector; Vector<int> v;

Or even together, use the full name:

Std::vector<int> v;

It is recommended to use the global naming domain method:

using namespace Std;

Statement of the 1.vector

Vector<elemtype> C; Create an empty vector

Vector<elemtype> C1 (C2); Create a vector c1 and use C2 to initialize the C1

Vector<elemtype> c (n); Create a vector containing n elemtype type data;

Vector<elemtype> c (N,elem); Create a vector containing n elemtype type data and initialize it all to Elem;

C.~vector<elemtype> (); Destroy all data and release resources;

Functions commonly used in 2.vector containers. (c is a container object)

C.push_back (Elem); Add an element at the last position of the container Elem

C.pop_back (); Delete the element at the last position of the container

c.at (index); Returns the element at the specified index position

C.begin (); Returns a pointer to the container's starting position data

C.end (); Returns a pointer to the last data cell of the container +1

C.front (); Returns a reference to the container's first cell data

C.back (); Returns a reference to the last data in the container

C.max_size (); Returns the maximum capacity of a container

C.size (); Returns the number of actual stored elements in the current container

C.capacity (); With C.size ()

C.resize (); Re-set the vector's capacity

C.reserve (); With C.resize ()

C.erase (P); Delete pointer p pointing to the position of the data, return the next pointer to the next data position (iterator)

C.erase (begin,end) deletes data from the Begin,end interval, returning a pointer to the next data position (iterator)

C.clear (); Clear All data

C.rbegin (); Returns the starting pointer after the vector is reversed (actually the original end-1)

C.rend (); Returns the end pointer after the vector is reversed (actually the original begin-1)

C.empty (); Determines if the container is empty, returns true if NULL, otherwise false

C1.swap (C2); Exchanging data in two containers

C.insert (P,elem); Inserts the data elem where the pointer p points, returning a pointer to the Elem position

C.insert (P,n,elem); Insert n elem data in position p, no return value

C.insert (p,begin,end) inserts data in the interval [begin,end) at position p, no return value

Operations in 3.vector

Operator[] such as: c.[i];

The same as the at () function, which takes the data from the container.

C + + vector

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.