目录
1.vector基本概念
2.vector的创建
3.vector赋值操作
4.vector容量和大小的操作
5.vector容器的插入和删除
6.vector容器的数据存取
7.vector互换容器
8.vector容器预留空间
#include
using namespace std;
int main()
{
vector v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
for(vector::iterator i = v1.begin(); i != v1.end(); i++)
cout << *i << ' ';
cout << endl;
vector v2(v1.begin(),v1.end());
for(vector::iterator i = v2.begin(); i != v2.end(); i++)
cout << *i << ' ';
cout << endl;
vector v3(5,100);
//第一个为个数,第二个为赋值
for(vector::iterator i = v3.begin(); i != v3.end(); i++)
cout << *i << ' ';
cout << endl;
vector v4(v3);
//拷贝构造
for(vector::iterator i = v4.begin(); i != v4.end(); i++)
cout << *i << ' ';
return 0;
}
#include
using namespace std;
void print(vector &v)
{
for(vector::iterator i = v.begin(); i != v.end(); i++)
cout << *i << ' ';
cout << endl;
}
int main()
{
vector v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
print(v1);
vector v2;
v2 = v1;
print(v2);
vector v3;
v3.assign(v1.begin(),v1.end());
print(v3);
vector v4;
v4.assign(3,100);
print(v4);
return 0;
}
#include
using namespace std;
void print(vector &v)
{
for(vector::iterator i = v.begin(); i != v.end(); i++)
cout << *i << ' ';
cout << endl;
}
int main()
{
vector v1;
cout << v1.empty() << endl;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
cout << v1.empty() << endl;
cout << endl << v1.capacity() << endl;
cout << endl << v1.size() << endl << endl;
v1.resize(8,10);
//将长度变为8,用10填充新的位置,(新的位置默认填充是0)
//如果重新指定的size比原来短,多余的会被删除
print(v1);
return 0;
}
#include
using namespace std;
void print(vector &v)
{
for(vector::iterator i = v.begin(); i != v.end(); i++)
cout << *i << ' ';
cout << endl;
}
int main()
{
vector v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
print(v1);
v1.pop_back();
print(v1);
v1.insert(v1.begin(),2,100);
//第一个参数是迭代器
print(v1);
v1.erase(v1.begin());
print(v1);
v1.erase(v1.begin(),v1.end());
print(v1);
v1.clear();
//全部清空的操作
return 0;
}
#include
using namespace std;
void print(vector &v)
{
for(vector::iterator i = v.begin(); i != v.end(); i++)
cout << *i << ' ';
cout << endl;
}
int main()
{
vector v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
for(int i = 0; i < v1.size(); i ++)
{
cout << v1[i] << ' ';
}
cout << endl;
for(int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << ' ';
}
cout << endl;
cout << v1.front() << endl;
cout << v1.back() << endl;
return 0;
}
#include
using namespace std;
void print(vector &v)
{
for(vector::iterator i = v.begin(); i != v.end(); i++)
cout << *i << ' ';
cout << endl;
}
int main()
{
vector v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
vector v2;
v2.push_back(1);
v2.push_back(1);
v2.push_back(1);
print(v1);
print(v2);
v1.swap(v2);
print(v1);
print(v2);
//如果我们创建了一个vectorv,让它存了很多东西
//之后又用resize把它的大小变小了,但它的capacity没有改变,还是很大
//使用vector(v).swap(v)可以巧妙的收缩内存
//这行代码创建了一个大小为v的vector容器的匿名对象与v进行了交换
//匿名对象在下一行后内存就会被释放
return 0;
}
#include
using namespace std;
void print(vector &v)
{
for(vector::iterator i = v.begin(); i != v.end(); i++)
cout << *i << ' ';
cout << endl;
}
int main()
{
vector v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(7);
v1.push_back(1);
v1.push_back(2);
cout << v1.capacity() << endl;
cout << v1.size() << endl;
v1.reserve(100);
cout << v1.capacity() << endl;
cout << v1.size() << endl;
return 0;
}