C++vector是标准库中的一员,vector直译过来是“向量”、“矢量”的意思,在C++中,是一个动态的数组容器,可以动态的开辟空间,自动实现内存的管理,不需要我们手动操作,在标准库中,写作一个类模版,可以传递不同的类型实例化出不同类型的vector。
template < class T, class Alloc = allocator<T> > class vector;
1.构造函数(constructor)
常见的构造方式有下面几种,由于vector在实例化的时候,传递不同的类型就会实例化出不同的vector,所以标准库中没有设计打印的接口,因此在讲解的时候,单独添加了一个vector_print模版函数,可以打印不同类型的vector:
template<class T>
void vector_print(const vector<T>& v)
{
// 这里的typename不可省略,因为编译器还不知道const_iterator是一个类型,
// 需要添加typename告诉编译器,我们需要从vector<T>这个类中获取const_iterator类型
// (也可以用auto就可以避免这样的问题)
typename vector<T>::const_iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << "\n";
}
下面是常见的构造方式:
vector<int> v0; //默认构造初始化
vector<int> v1(5,1); //用n个值初始化
vector<int> v2(v1.begin(),v1.end()); //利用迭代器区间初始化
vector<int> v3(v2); //拷贝构造初始化
cout << "v0:"; vector_print(v0);
cout << "v1:"; vector_print(v1);
cout << "v2:"; vector_print(v2);
cout << "v3:"; vector_print(v3);
2.迭代器(Iterators)
begin:返回vector中的第一个位置
end:返回vector中的最后一个位置的下一个位置
rbegin:返回vector中的最后一个位置
rend:返回第一位置的前一个位置
const vector在前面+c(cbegin、cend、crbegin、crend)
vector<int> v0(5,0);
vector<int>::iterator itb = v0.begin();
vector<int>::reverse_iterator ritb = v0.rbegin();
vector<int>::iterator ite = v0.end()-1;
vector<int>::reverse_iterator rite = v0.rend() - 1;
*itb = 1;
*ite = 1;
cout << "v0:"; vector_print(v0);
*ritb = 2;
*rite = 2;
cout << "v0:"; vector_print(v0);
3.容器(Capacity)
size:返回vector的有效数据数量
max_size:vector能储存的最大数量(取决于实例化的类型和编译器)
resize(n):重置vector的size(若当前size > n,那么多的数据会被移除,若当前size < n的话,多的数据会以默认值初始化)
capacity:返回vector的容量
empty:检查vector是否为空
reserve(n):重置vector的容量为所给值(若当前capacity > n,不会缩容,因为官方文档中说明了此接口除了n > capacity的情况,其余情况不会影响原来的capacity,若当前capacity < n,则扩容)
shrink_to_fit:将capacity减为size
vector<int> v0(5, 1);
cout << "原始v0 :";
vector_print(v0);
cout << "size: " << v0.size() << "\n"
<< "max_size: " << v0.max_size() << "\n"
<< "capacity: " << v0.capacity() << endl;
cout << "----------------------------------" << endl;
v0.reserve(2);
cout << "将capacity置为2后的v0: ";
vector_print(v0);
cout << "size: " << v0.size() << "\n"
<< "capacity: " << v0.capacity() << endl;
v0.reserve(10);
cout << "将capacity置为10后的v0:";
vector_print(v0);
cout << "size: " << v0.size() << "\n"
<< "capacity: " << v0.capacity() << endl;
cout << "----------------------------------" << endl;
v0.resize(3);
cout << "将size置为3后的v0:";
vector_print(v0);
cout << "size: " << v0.size() << "\n"
<< "capacity: " << v0.capacity() << endl;
v0.resize(8);
cout << "将size置为8后的v0:";
vector_print(v0);
cout << "size: " << v0.size() << "\n"
<< "capacity: " << v0.capacity() << endl;
cout << "----------------------------------" << endl;
v0.shrink_to_fit();
cout << "shrink_to_fit后的v0:";
vector_print(v0);
cout << "size: " << v0.size() << "\n"
<< "capacity: " << v0.capacity() << endl;
cout << "----------------------------------" << endl;
4.元素访问符(Element access)
operator[ i ]:返回下标为i的元素(像数组一样访问)
at(n):返回在第n个位置的元素
front:获取第一个元素
back:获取最后一个元素
data:返回第一个元素的指针
vector<int> v0(6, 1);
cout << "v0: ";
for (int i = 0; i < v0.size(); i++)
{
v0[i] += i;
cout << v0[i] << " "; //通过下标方式遍历
}
cout << endl;
cout << "v0.front(): " << v0.front() << endl;
cout << "v0.back(): " << v0.back() << endl;
cout << "v0.at(3): " << v0.at(3) << endl;
cout << "*(v0.data()): " << *(v0.data()) << endl;
cout << "首元素地址:" << endl;
cout << v0.data() << endl;
cout << &v0[0] << endl;
5.修饰符(Modifiers)
assign:重置vector的内容
push_back:尾插一个元素
pop_back:尾删一个数据
insert:插入一个数据
erase:清除vector中某个内容或者某段范围的内容
swap:交换两个vector
clear:清除vector的整个内容
vector<char> v0(1,'h');
cout << "assign前的v0: ";
vector_print(v0);
v0.assign(1,'v');
cout << "assign后的v0: ";
vector_print(v0);
cout << "尾插七个字符后的v0: " << endl;
v0.push_back('e');
v0.push_back('c');
v0.push_back('t');
v0.push_back('o');
v0.push_back('r');
v0.push_back('o');
v0.push_back('r');
vector_print(v0);
cout << "pop_back v0后二个字符: " << endl;
v0.pop_back();
v0.pop_back();
vector_print(v0);
cout << "头插六个字符后的v0: " << endl;
v0.insert(v0.begin(), 'h');
v0.insert(v0.begin()+1, 'e');
v0.insert(v0.begin()+2, 'l');
v0.insert(v0.begin()+3, 'l');
v0.insert(v0.begin()+4, 'o');
v0.insert(v0.begin()+5, ' ');
vector_print(v0);
v0.erase(v0.begin(), v0.begin() + 6);
cout << "erase v0前五个字符: " << endl;
vector_print(v0);
vector<char> v1;
v1.swap(v0);
cout << "v0和v1 swap:" << endl;
cout << "v0: ";
vector_print(v0);
cout << "v1: ";
vector_print(v1);
v1.clear();
cout << "v1: ";
vector_print(v1);