无需多言
一代码可了解天下:
理解每一行代码的时候
因为代码解释采用递进的方式
大家可以根据实际边观看输出
边理解:
#include<iostream> using namespace std; #include<vector> vector<int> q; #include<algorithm> int main() { q.push_back(1);//从尾部插入 1 q.push_back(2); q.push_back(3); q.push_back(4); q.push_back(5); cout << q.size()<<endl;//输出q里面包含元素的个数 cout << q.empty()<<endl;//为空则返回1,不然返回0 cout << q[1]<<endl;//输出下标为 1 的元素 q.insert(q.begin() + 1, 999);//在下标为1的元素前插入999 cout << q[1]<<endl; q.insert(q.begin(), 3, 25);//在开始插入三个25 cout << q[0] << " " << q[1] << " " << q[2]<<endl; q.erase(q.begin(), q.begin() + 2);//删除前2两个元素 cout << q[0] << " " << q[1] << " " << q[2]<<endl; q.erase(q.begin());//删除下标为0的元素 25 cout << q[0] << " " << q[1] << " " << q[2] << endl; q.erase(q.begin() + 1);//删除下标为1的元素 999 cout << q[0] << " " << q[1] << " " << q[2]<<endl; q.resize(6);//调整空间大小,剩余没使用过的空间会变成0 reverse(q.begin(), q.end()); cout << q[0] << " " << q[1] << " " << q[2] << endl; sort(q.begin(), q.end());//从大到小排序 cout << q[0] << " " << q[1] << " " << q[2] << endl; return 0; }