#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printVector(vector<int> &v)
{
for(vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(int argc, char** argv)
{
vector<int> v1;
for(int i = 0;i < 10;i++)
{
v1.push_back(i);
}
printVector(v1);
//通过区间方式进行构造
vector<int>v2(v1.begin(),v1.end());
printVector(v2);
//n个elem方式构造
vector<int>v3(10,100);
printVector(v3);
//拷贝构造
vector<int>v4(v3);
printVector(v4);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//vector的赋值
void printVector(vector<int> &v)
{
for(vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(int argc, char** argv)
{
vector<int> v1;
for(int i = 0;i < 10;i++)
{
v1.push_back(i);
}
printVector(v1);
//赋值 operator=
vector<int>v2;
v2 = v1;
printVector(v2);
//assign
vector<int>v3;
v3.assign(v1.begin(),v1.end());
printVector(v3);
//n个elem方式赋值
vector<int>v4;
v4.assign(10,100);
printVector(v4);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void printVector(vector<int>&v)
{
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> v1;
for(int i = 0;i < 10;i++)
{
v1.push_back(i);
}
printVector(v1);
if(v1.empty())
{
cout << "v1空" << endl;
}
else
{
cout << "v1非空" << endl;
cout << "v1的容量:" << v1.capacity() << endl;
cout << "v1的大小:" << v1.size() << endl;
}
//重新指定大小
v1.resize(20);
v1.resize(30,100);
printVector(v1);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
//插入和删除
void printVector(vector<int>&v)
{
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> v1;
//尾插
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
printVector(v1);\
//尾删
v1.pop_back();
printVector(v1);
//插入
v1.insert(v1.begin(),100);
printVector(v1);
v1.insert(v1.begin(),2,1000);
printVector(v1);
//删除
v1.erase(v1.begin());
printVector(v1);
//清空
v1.clear();
printVector(v1);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
//存取
void printVector(vector<int>&v)
{
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> v;
for(int i = 0;i < 10;i++)
{
v.push_back(i);
}
//利用[]访问数组中元素
for(int i = 0;i < v.size();i++)
{
cout << v[i] << " ";
}
cout << endl;
//利用at访问元素
for(int i = 0;i < v.size();i++)
{
cout << v.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << "获取第一个元素:" << v.front() << endl;
//获取最后一个元素
cout << "获取最后一个元素:" << v.back() << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
//容器互换
void printVector(vector<int>&v)
{
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> v1;
for(int i = 0;i < 10;i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int> v2;
for(int i = 10;i > 0;i--)
{
v2.push_back(i);
}
printVector(v2);
//交换
v1.swap(v2);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
//预留空间
void printVector(vector<int>&v)
{
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> v;
v.reserve(100000);
int num = 0;
int * p = NULL;
for(int i = 0;i < 100000;i++)
{
v.push_back(i);
if(p!=&v[0])
{
p = &v[0];
num++;
}
}
cout << num << endl;
return 0;
}