赞
踩
动态扩展机制:扩展时不是续接新空间,直接找一个更大的空间
vector的迭代器支持随机访问
#include <vector>
#include <algorithm>
1.构造函数
无参构造,只定义未初期化
void test () { vector<vector<int>>vip; vector<int>v1; vector<int>v2; vector<int>v3; vector<int>v4; for (int i = 0; i < 4; i++) { v1.push_back(i + 1); v2.push_back(i + 5); v3.push_back(i + 9); v4.push_back(i + 13); } vip.push_back(v1); vip.push_back(v2); vip.push_back(v3); vip.push_back(v4); for (vector<vector<int>>::iterator it = vip.begin(); it != vip.end(); it++) { for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) { cout << *vit << " "; } cout<< endl; } }
for (vector<vector<int>>::iterator it = vip.begin(); it != vip.end(); it++) {
vector<int> vip2((*it).begin(), (*it).end());
showInfo(vip2);
}
vector<int> vip3(10, 100);
showInfo(vip3);
vector<int> vip4(vip3);
showInfo(vip4);
void showInfo(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
2.赋值操作
vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(256); } showInfo(v1); vector <int>v2; v2 = v1; showInfo(v2); vector<int>v3; v3.assign(v2.begin(), v2.end()); showInfo(v3); vector<int>v4; v4.assign(10, 65535); showInfo(v4);
3.插入和删除
void test01()
{
vectorv1;
//尾插
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//遍历 showInfo(v1); //尾删 v1.pop_back(); showInfo(v1); //插入 第一个参数是迭代器 v1.insert(v1.begin(), 100); showInfo(v1); v1.insert(v1.begin(), 2, 1000); showInfo(v1); //删除 参数也是迭代器 v1.erase(v1.begin()); showInfo(v1); //清空 //v1.erase(v1.begin(), v1.end()); v1.clear(); showInfo(v1);
}
4.数据存取
void test()
{
vector<int>v1;
v1.push_back(pow(2,1));
v1.push_back(pow(2,2));
v1.push_back(pow(2,3));
v1.push_back(pow(2,4));
v1.push_back(pow(2,5));
cout << "第一个元素为: " << v1.front() << endl;
cout << "最后一个元素为: " << v1.back() << endl;
}
5.容器互换
void test() { vector<int>v1; vector<int>v2; v1.push_back(pow(2,1)); v1.push_back(pow(2,2)); v1.push_back(pow(2,3)); v1.push_back(pow(2,4)); v1.push_back(pow(2,5)); v2.push_back(pow(2,6)); v2.push_back(pow(2,7)); v2.push_back(pow(2,8)); v2.push_back(pow(2,9)); v2.push_back(pow(2,10)); for (int i = 0; i < v1.size(); i++) { cout<< v1[i] << " "; } cout << endl; for (int i = 0; i < v2.size(); i++) { cout<< v2.at(i) << " "; } cout << endl; v1.swap(v2); for (int i = 0; i < v1.size(); i++) { cout<< v1[i] << " "; } cout << endl; for (int i = 0; i < v2.size(); i++) { cout<< v2.at(i) << " "; } cout << endl; }
6.预留空间
/
int num = 0; //统计开辟次数
int * p = NULL;
***//v.reserve(100000);***
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num = " << num << endl;
加入v.reserve(100000);
7.容器大小
void test() { vector<int>v1; vector<int>v2; v1.push_back(pow(2,1)); v1.push_back(pow(2,2)); v1.push_back(pow(2,3)); v1.push_back(pow(2,4)); v1.push_back(pow(2,5)); if (v1.empty()) //为真 代表容器为空 { cout << "v1为空" << endl; } else { cout << "v1不为空" << endl; cout << "v1的容量为: " << v1.capacity() << endl; cout << "v1的大小为: " << v1.size() << endl; } //重新指定大小 v1.resize(15 , 100); //利用重载版本,可以指定默认填充值,参数2 showInfo(v1);//如果重新指定的比原来长了,默认用0填充新的位置 v1.resize(1); showInfo(v1); //如果重新指定的比原来短了,超出部分会删除掉 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。