Basic concept of vector
Function:
The vector data structure is very similar to an array, also known as a single-ended array.
The difference between a vector and an ordinary array:
the difference is that an array is a static space, while a vector can be dynamically expanded.
Dynamic expansion:
Instead of adding a new space after the original space, Instead, find a larger memory space, then copy the original data to the new space, and release the original space.
The iterator of the vector container is an iterator that supports random access.
vector constructor
Function description:
create vector container
Function prototype:
vector<T> v; //采用模板实现类实现,默认构造函数
vector(v.begin(),v,end()); //将v[begin(),end())区间中的元素拷贝给本身
vector(n,elem); //构造函数将n个elem拷贝给本身
vector(const vector & vec); //拷贝构造函数
#include<iostream>
using namespace std;
#include<vector>
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器构造
void test01()
{
vector<int> v1; //默认构造 无参构造
for (int i = 0; i < 10; ++i)
{
v1.push_back(i);
}
printVector(v1);
vector<int>::iterator it1 = v1.begin();
it1 += 2;
//通过区间方式进行构造
vector<int> v2(v1.begin(), it1);//0 1
printVector(v2);
//n个elem方式构造
vector<int>v3(10, 100);
printVector(v3);
//拷贝构造
vector<int> v4(v3);
printVector(v4);
}
int main()
{
test01();
return 0;
}
Summary: The multiple construction methods of vector are not comparable, and can be used flexibly.
vector assignment operation
Function description:
assign value to vector container
Function prototype:
vector& operator=(const vector &vec);//overload equal sign operator
assign(beg,end);//assign the data copy in [beg,end] range to itself.
assign(n,elem);//Assign n elem copies to itself.
#include<iostream>
using namespace std;
#include<vector>
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector赋值
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//赋值 operator=
vector<int>v2;
v2 = v1;
printVector(v2);
//赋值 assign
vector <int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
//n个elem方式赋值
vector<int>v4;
v4.assign(10, 100);
printVector(v4);
}
int main()
{
test01();
return 0;
}
Summary: The vector assignment method is relatively simple, you can use operator= or assign
vector capacity and size
Function description:
operate on the capacity and size of the vector container
Function prototype:
empty();//judging whether the container is empty
capacity();//capacity of the container
size();//returning the number of elements in the container
resize(int num);//Re-establish the length of the container as num, if the container becomes longer, fill the new position with the default value (0).
// If the container gets shorter, elements at the end that exceed the length of the container are removed.
resize(int num, elem);//Re-establish the length of the container as num, if the container becomes longer, fill the new position with the value of elem.
// If the container gets shorter, elements at the end that exceed the length of the container are removed.
#include<iostream>
using namespace std;
#include<vector>
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器的容量和大小操作
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty())//为真 代表容器为空
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
//重新指定大小
v1.resize(15);
printVector(v1);//如果重新指定的比原来长了,默认用0填充新的位置
v1.resize(20,33);
printVector(v1);
v1.resize(5);
printVector(v1);
v1.resize(20);
printVector(v1);
}
int main()
{
test01();
return 0;
}
Summary:
1. Determine whether it is empty
2. Return the number of elements size
3. Return the container capacity capacity
4. Re-specify the size resize