vectors:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
if(v1.empty())
{
cout << "空" << endl;
}
//调用vectors中提供的size函数
//cout << v1.capacity() << endl;
v1.push_back(12);
cout << v1.size() << endl;
cout << "插入一个元素后capacity = " << v1.capacity() << endl; //1
v1.push_back(7);
cout << "插入两个元素后capacity = " << v1.capacity() << endl; //2
v1.push_back(7);
cout << "插入三个元素后capacity = " << v1.capacity() << endl; //4 vector双倍扩容
v1.push_back(10);
v1.push_back(11);
cout << "size = "<<v1.size() << endl;
//打印元素的第一种写法
int i;
for(i=0;i<v1.size();i++)
{
//vector中对[]运算符重载
cout<<" 【"<< i<<"】 =" << v1[i] << endl;
}
//打印元素的第二种写法,语法糖机制
for(int j:v1)
{
cout << " 【"<< j<<"】 =" <<j << endl;
}
//begin返回的是第一个位置元素的迭代器---->理解为指针就可以了
//cout << *v1.begin() << endl;
//auto的作用自动获取,变量的类型,和C中的auto不是一个
//打印元素的第三种方式
for(auto it= v1.begin(); it != v1.end(); it++)
{
cout << " 【""】 =" <<*it << endl;
}
v1.pop_back();
cout << v1.capacity() << endl;
cout << v1.size() << endl;
cout << v1.at(3) << endl; //10
vector<int> v3;
v3 = v1;
v3.assign(5,1);
v3.at(2) = 89; //at返回的是元素的引用
// v3.clear();
v3.back() = 79;
for(int j:v3)
{
cout << j << endl;
}
cout << v3.size() << endl;
return 0;
}