目录
2.详细用法(使用了上述常用的函数和用swap真正的释放内存)
迭代器
容量
修饰器
1.初始化(4种方法)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>vec(10);
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
vector<int>vec1(10, 1);
for (it = vec1.begin(); it != vec1.end(); ++it) {
cout << *it << " ";
}
cout << endl;
vector<int>vec2 = { 0,1,2,3,4,5 };
for (it = vec2.begin(); it != vec2.end(); ++it) {
cout << *it << " ";
}
cout << endl;
int arr[5] = { 5,6,7,8,9 };
vector<int>vec3(arr, arr + 5);//注意这里初始化5个元素是+5,即最后一个是指向下一个元素的
for (it = vec3.begin(); it != vec3.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
2.详细用法(使用了上述常用的函数和用swap真正的释放内存)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
cout << "push_back" << endl;
//vec.resize(5);
vec.reserve(10);
for (int i = 0; i < 10; ++i) {
vec.push_back(i);
}
if (!vec.empty()) {
cout << "vec is not empty" << endl;
}
cout << "iterator begin end" << endl;
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "reverse_iterator rbegin rend" << endl;
vector<int>::reverse_iterator ir;
for (ir = vec.rbegin(); ir != vec.rend(); ++ir) {
cout << *ir << " ";
}
cout << endl;
cout << "vec.size = " << vec.size() << endl;
cout << "vec.capacity = " << vec.capacity() << endl;
cout << "push_back" << endl;
vec.push_back(10);
cout << "vec.size = " << vec.size() << endl;
cout << "vec.capacity = " << vec.capacity() << endl;
cout << "pop_back" << endl;
vec.pop_back();
cout << "vec.size = " << vec.size() << endl;
cout << "vec.capacity = " << vec.capacity() << endl;
it = vec.begin();
cout << "earse" << endl;
vec.erase(it);
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "vec.size = " << vec.size() << endl;
cout << "vec.capacity = " << vec.capacity() << endl;
cout << "vec.clear" << endl;
vec.clear();
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "vec.size = " << vec.size() << endl;
cout << "vec.capacity = " << vec.capacity() << endl;
{
vector<int> temp;
vec.swap(temp);
}
//vector<int>().swap(vec);
cout << endl;
cout << "vec.size = " << vec.size() << endl;
cout << "vec.capacity = " << vec.capacity() << endl;
return 0;
}
3.insert的用法(3种插入方法)
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
vector<int>::iterator it;
int arr[5] = { 5,6,7,8,9 };
vector<int>vec(arr, arr + 5);
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
vec.insert(vec.begin()+1, 4);//在第二个位置插入数据4,不过会使迭代器失效
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
vec.insert(vec.end() - 2, 2, 3);//在倒数第二个位置插入2个3
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
int arr1[2] = { 1,2 };
vec.insert(vec.begin(), arr1, arr1 + 2);//使用迭代器在首位插入1,2
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}