You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
using namespace std;
/*
知识点内容C++ STL vector详解
vector(向量):是一种顺序容器,事实上和数组差不多,但它比数组更优越。一般来说数组不能动态拓展,
因此在程序运行的时候不是浪费内存就是造成越界。而vector正好弥补了这个缺陷
它的特征是相当于可分配拓展的数组,它的随机访问快,在中间插入和删除慢,但在末端插入和删除快。
文档内容参考:
https://www.cnblogs.com/aiguona/p/7228364.html
* */
int main() {
vector<int> v; //定义vector
vector<int>::iterator it; //定义一个vector迭代器
for (int i = 10; i >= 1; i--) //插入数据
v.push_back(i);
cout << "输出:";
for (it = v.begin(); it != v.end(); it++) //输出迭代器的值
cout << *it << " ";
cout << endl;
it -= 1;
cout << "最后一个的值为:" << *it << " " << endl;
v.erase(it); //删除最后一个元素
cout << "元素个数:" << v.size() << endl; //输出元素个数
sort(v.begin(), v.end()); //vector排序
cout << "排序后:";
for (it = v.begin(); it != v.end(); it++) //输出vector元素
cout << *it << " ";
cout << endl;
v.insert(v.begin(), 100); //在pos位置插入一个elem
cout << "第一个元素为:" << v.front() << endl;//输出第一个元素
v.pop_back(); //去掉最后一个元素
cout << "元素个数:" << v.size() << endl;//输出元素个数
v.clear(); //vector清空
cout << "清空后元素个数:" << v.size() << endl; //输出元素个数
return 0;
}