vector STL Standard Template Library's

STL provides a set of templates represent containers, iterators, function objects and algorithms.

  • Is a container similar to the array element, can store a number of values. STL containers are homogeneous, i.e., the same type of stored value;
  • Algorithm is to accomplish a specific task (such as an array of sort or find a specific value in the list) prescription;
  • Object iterator can be used to traverse the vessel, and the like can be through the array of pointers, the pointer is generalized;
  • Function similar to the function object is an object, the object class may be a pointer or function (including the function name, the function name is used as a pointer).

STL can be configured so that various containers (including arrays, linked lists and queues) and perform various operations (including searching, sorting, and randomly arranged)

Next comes several ACMer several members must master

vector container

1) What is the vector

Vector (the Vector) is a dynamic sequential container encapsulates the array size (Sequence Container). Like any other type of container, it is possible to store various types of objects. Simply believed that the vector is a dynamic array can store any type.

In general dynamic arrays can not expand, so when the program is running is not a waste of memory, is caused by cross-border. The vector just fills this gap, characterized in that it corresponds to the expansion allocatable arrays (dynamic array) and its fast random access, insertion and deletion in the middle of a slow, but at the end of the fast insertion and deletion.

2) How to define

//头文件必须包含:
#include<vector>
//定义一个vector,int为数组元素的数据类型,test1为动态数组名
vector<int>test1;
//定义一个元素为结构体型的vector
vector<information>test2
//定义一个迭代器
vector<int>::iterator it;

The initialization vector can have a variety of ways:

//定义10个整型元素的向量(尖括号中为元素类型名,它可以是任何合法的数据类型),但没有给出初值,其值是不确定的。
① vector<int> a(10);
//定义了10个整型元素的向量,且给出每个元素的初值为1
② vector<int> a(10,1); 
//用b向量来创建a向量,整体复制性赋值
③ vector<int> a(b); 
//定义了a值为b中第0个到第2个(共3个)元素
④ vector<int> a(b.begin(),b.begin+3);
//从数组中获得初值
⑤ int b[7]={1,2,3,4,5,9,8};
  vector<int> a(b,b+7); 

3) common functions Vector

1, capacity function

  • Container size: a.size (); // returns the number of elements in a

  • Capacity of the container: a.capacity (); // pre-allocated memory space of different size, returns the number of elements in a memory can accommodate a total of

  • Empty containers sentence: a.empty (); return true // if empty, false otherwise

  • Change the size of the container: a.resize (NUM); a.resize (NUM, value);

a.resize(10); //将a的现有元素个数调至10个,多则删,少则补,其值随机
a.resize(10,2); //将a的现有元素个数调至10个,多则删,少则补,其值为2

2, increasing function

  • The data section [first, end) is assigned to a (note that the opening and closing section): a.assign (First, End)

  • with only a n elements, and each element is elem: a.assign (n, elem)

a.assign(b.begin(), b.begin()+3); //b为向量,将b的0~2个元素构成的向量赋给a
a.assign(4,2);//a只含4个元素,且每个元素为2
  • End additive element: a.push_back (value) // inserted after the last element of vector a has a value of value
  • Insert element anywhere: a.insert (location, value) // value is inserted at the position of the location of a
  • Inserted anywhere num identical elements: a.insert (location, num, value) // number of num is inserted at the position of the first value of the location of a
  • Between data inserted into the other vector [First, End): a.insert (LOCATION, First, End)
//假设 a:5 7 3 1 4;b: 2 3 4 5 6 7 8
a.push_back(2);//a:5 7 3 1 4 2
a.insert(a.begin()+1,2);//a:5 2 7 3 1 4
a.insert(a.begin()+1,2,3);//a:5 3 3 7 3 1 4
a.insert(a.begin()+1,b.begin()+2,b.begin()+5);//a:5 4 5 6 7 3 1 4

3, delete function

  • Head remove elements: a.pop_front ();
  • End remove elements: a.pop_back ();
  • Anywhere delete an element: a.erase (LOCATION);
  • Elements between delete [First, End): a.erase (First, End);
  • Clear all the elements: a.clear ();

4, iterators

  • The start pointers: a.begin ();
  • Pointer to the end: a.end (); next position // points to the last element

5, access function

  • A first return element: a.front ();
  • A return to the last element: a.back ();
  • Indexed access: A [1]; // does not check whether the cross-border
  • at ways to access: a.at (1); // checks for cross-border, cross-border if an exception is thrown out of range

6, operations and other functions

  • Swap function: a.swap (b); // b be a vector, the elements a and b of the switching elements in the overall
  • Comparison Operation: A == B; // B is also a vector, the vector comparison there =,> =, <=,>, <!

7, Algorithm

Need to include the header file:

#include<algorithm>
(1)sort(a.begin(),a.end()); //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素进行从小到大排列
(2)reverse(a.begin(),a.end()); //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1
(3)copy(a.begin(),a.end(),b.begin()+1); //把a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
(4)find(a.begin(),a.end(),10); //在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置

Guess you like

Origin www.cnblogs.com/jiyi-conding/p/11355521.html