1. Dynamic array
A dynamic array is an array that can grow or shrink in size dynamically as needed, usually using the malloc() and free() functions or other similar memory allocation and deallocation functions. The size of dynamic arrays can be determined at runtime and can be automatically resized as needed, which makes them more flexible than static arrays. The operation of a dynamic array is similar to that of a static array, and elements can be manipulated using indexes, which are stored contiguously in memory. Using dynamic array can manage memory conveniently, improve code flexibility and maintainability.
2, Operation of dynamic array
1, the creation of dynamic array
1. Create an empty array
vector< type > name
2. Create an array with several numbers
vector < type > name (several, value)
3. Copy the array, iterator method
vector < type > name ( name2 )
#include <iostream>
#include <vector>
using namespace std ;
int main ()
{
int n ;
//1 . 空数组
vector <int> name ;
//2, 简单的赋值
vector <int> an(3,4) ;
vector <int> ansf(4) ;
// 3,完成简单的赋值的操作
vector <int> ans(an) ;
vector<int> ans ( b.begin(),b.end());
return 0 ;
}
3, the operation of dynamic array
Insert and pop operation of dynamic array
//压入操作
q.push_back()
//压入操作
q.push_back()
are all operations on the last digit
Pop-up of head and tail nodes
//头结点
cout << q.front() << endl ;
//尾结点
cout << q.back() << endl;
size function
cout << q.size() << endl ;
Insert operation INSERT
We know that all insertion operations are actually an iterator operation, so we generally use the find function to find the position of the iterator
vector <int> :: iterator itr = find( a,begin() , a.end() , 1) ;
//第一种方式
vector <int> :: iterator it = a,beign() + 34 ;
- insert a data
q.insert(itr , a);
- Insert a set of (several, numeric) data
q.insert(itr , 2, 6) ;
- insert iterator address
q.insert( itr , v.beign() , v.end() ) ;
4, Element-specific characteristics
Array property calls