vector的头文件
#include<vector>
vector的声明与初始化
vector<类型> 变量 = 赋值;
//整型
vector<int> a = { 1,2,3,4 };
//浮点型
vector<double> b = { 1.1,2.2,3.2,4.4 };
//字符型
vector<char> c = { 'a','b','c' };
//字符串型
vector<string> d = { "a","asad","asad"};
构造函数初始化
vector<int>v(10);
创建一个类型为int的数组,数组的元素个数为10,元素的值默认为0
vector<int>a(10);
for (int i = 0;i<10; i++) {
cout <<"a[" <<i<<"]=" << a[i] << endl;
}
vector<int>v(10,3);
创建一个类型为int的数组,数组的元素个数为10,元素的值为3
vector<int>a(10,3);
for (int i = 0; i < 10; i++) {
cout << "a[" << i << "]=" << a[i] << endl;
}
vector<int>v(a.begin(),a.end());
vector<int>a = { 1,3,5,6,4,2,8 };
vector<int>b(a.begin(), a.end());
for ( auto i=b.begin(); i < b.end(); i++) {
cout << *i<< endl;
}
将动态数组a在区间[begin,end)(注意这里是左开右闭)de的内容赋值给动态数组
注意:这里a.begin(),a.end()是迭代器,我们可以将其类似于数组看待和理解。如果将这里的a.begin(),a.end()的内容改成a.begin(),a.begin()+1,则只会拷贝a中一个元素的内容。
vector<int>a = { 1,3,5,6,4,2,8 };
vector<int>b(a.begin(),a.begin()+2);
for ( auto i=b.begin(); i < b.end(); i++) {
cout << *i<< endl;
}
而针对a.begin(),a.end()这样的迭代器,还有一类迭代器是可以取反的a.rbegin(),a.rend()。
vector<int>a = { 1,3,5,6,4,2,8 };
vector<int>b(a.begin(), a.end());
for ( auto i=b.rbegin(); i < b.rend(); i++) {
cout << *i<< endl;
}
常用函数
取单个元素的函数
vector<int>v = { 1,3,5,6,4,2,8 };
v.at(n)
取下标为n的元素值
cout << v.at(1) << endl;
v.front()
取首元素的值
cout << v.front() << endl;
v.back()
取尾元素的值
cout << v.back() << endl;
运行结果:
增加函数
void push_back(const T& x)
向量尾部增加一个元素x
vector<int>a = { 1,3,5,6,4,2,8 };
a.push_back(1000);
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
iterator insert(iterator it, const T&x)
向量中迭代器指向元素前增加一个元素x
vector<int>a = { 1,3,5,6,4,2,8 };
a.insert(a.begin() + 3, 566);
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
iterator insert(iterator it, int n, const T& x)
向量中迭代器指向元素前增加n个相同元素x
vector<int>a = { 1,3,5,6,4,2,8 };
a.insert(a.begin() + 3, 5,566);
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
iterator insert(iterator it, int n, const_iterator first, const_iterator last )
向量中迭代器指向元素前插入另一个相同类型向量在[first, last)间的数据
vector<int>b = { 11,11,22,22 };
vector<int>a = { 1,3,5,6,4,2,8 };
a.insert(a.begin() + 3, b.begin(),b.end());
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
删除函数
iterator erase(iterator it)
删除向量中迭代器指向元素
vector<int>a = { 1,3,5,6,4,2,8 };
a.erase(a.begin());
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
iterator erase(iterator first, iterator last)
删除向量中[first,last)中元素
vector<int>a = { 1,3,5,6,4,2,8 };
a.erase(a.begin(),a.end()-3);
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
void pop_back()
删除向量中最后一个元素
vector<int>a = { 1,3,5,6,4,2,8 };
a.pop_back();
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
void clear()
清空向量中所有元素
vector<int>a = { 1,3,5,6,4,2,8 };
a.clear();
for (auto i = a.begin(); i < a.end(); i++) {
cout << *i << endl;
}
判断函数
bool empty() const
判断向量是否为空,若为空,则返回1,若不为空,则返回0
vector<int>a = { 1,3,5,6,4,2,8 };
cout << a.empty() << endl;
a.erase(a.begin(),a.end());
cout << a.empty() << endl;
大小函数
int size() const
返回向量中元素的个数
vector<int>a = { 1,3,5,6,4,2,8 };
cout << a.size() << endl;
int capacity() const
返回当前向量所能容纳的最大元素值
vector<int>a = { 1,3,5,6,4,2,8};
cout << a.capacity() << endl;
int max_size()const
返回最大可允许的 vector 元素数量值
vector<int>a = { 1,3,5,6,4,2,8 ,11};
cout << a.max_size() << endl;
其他函数
void swap(vector&)
交换两个同类型向量的数据
vector<int>a = { 1,3,5,6,4,2,8 };
vector<int>b = { 0 };
b.swap(a);
for (auto i = b.begin(); i < b.end();i++) {
cout << *i << endl;
}
void assign(int n,const T& x)
设置向量为n个x
vector<int>a = { 1,3,5,6,4,2,8 };
a.assign(1,400);
for (auto i = a.begin(); i != a.end();i++) {
cout << *i << endl;
}
void assign(const iterator first,const iterator last)
设置向量为某个向量中[first,last)的元素
vector<int>a = { 1,3,5,6,4,2,8 };
vector<int>b = { 10,30,50,60,40,20,80 };
a.assign(b.begin(),b.end());
for (auto i = a.begin(); i != a.end();i++) {
cout << *i << endl;
}
标签:begin,end,cout,iterator,STL,C++,++,vector,vectora
From: https://blog.csdn.net/immnature/article/details/141369476