六大组件:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
Vector所采用的数据结构非常简单,线性连续空间,它以两个迭代器_Myfirst和_Mylast分别指向配置得来的连续空间中目前已被使用的范围,并以迭代器_Myend指向整块连续内存空间的尾端。
为了降低空间配置时的速度成本,vector实际配置的大小可能比客户端需求大一些,以备将来可能的扩充,这边是容量的概念。换句话说,一个vector的容量永远大于或等于其大小,一旦容量等于大小,便是满载,下次再有新增元素,整个vector容器就得另觅居所。
注意:
所谓动态增加大小,并不是在原空间之后续接新空间(因为无法保证原空间之后尚有可配置的空间),而是一块更大的内存空间,然后将原数据拷贝新空间,并释放原空间。因此,对vector的任何操作,一旦引起空间的重新配置,指向原vector的所有迭代器就都失效了。这是程序员容易犯的一个错误,务必小心。 |
vector常用赋值操作
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
vector& operator=(const vector &vec);//重载等号操作符
swap(vec);// 将vec与本身的元素互换。
vector大小操作
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(int num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长>度的元素被删除。
capacity();//容器的容量
reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
vector数据存取操作
at(int idx); //返回索引idx所指的数据,如果idx越界,抛出out_of_range异常。
operator[];//返回索引idx所指的数据,越界时,运行直接报错
front();//返回容器中第一个数据元素
back();//返回容器中最后一个数据元素
vector插入和删除操作
insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele.
push_back(ele); //尾部插入元素ele
pop_back();//删除最后一个元素
erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
erase(const_iterator pos);//删除迭代器指向的元素
clear();//删除容器中所有元素
示例
#include<iostream>
#include<string>
#include<vector>
#include<list>
using namespace std;
void printVector(vector<int> v) {
for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
cout << *i <<" ";
}
cout << "\n";
}
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i + 1);
cout << v1.capacity() << " ";
}
cout << "\n";
printVector(v1);
vector<int> v2(v1.begin(),v1.end());
printVector(v2);
vector<int> v3;
v3.assign(v1.begin(),v1.end());
printVector(v3);
vector<int>v4(10, 100);
printVector(v4);
v3.swap(v4);
printVector(v3);
printVector(v4);
}
void test02() {
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//v1.resize(10);
printVector(v1);
cout << v1.empty() << endl;
cout << "第一个" << v1.front() << endl;
cout << "最后一个" << v1.back() << endl;
v1.pop_back();
printVector(v1);
v1.clear();
cout << v1.empty() << endl;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.erase(v1.begin());
printVector(v1);
v1.erase(v1.begin(), v1.end());
cout << v1.empty() << endl;
}
//内存压缩
void test03() {
vector<int> v1;
for (int i = 0; i < 10000; i++) {
v1.push_back(i + 1);
}
cout << v1.capacity() << endl;
cout << v1.size() << endl;
v1.resize(10000);
vector<int>(v1).swap(v1);
cout << v1.capacity() << endl;
cout << v1.size() << endl;
}
//内存预留
void test04() {
vector<int> v1;
v1.reserve(10000);
int* p = NULL;
int num = 0;
for (int i = 0; i < 10000; i++) {
v1.push_back(i + 1);
if (p!=&v1[0]) {
p = &v1[0];
num++;
}
}
cout << v1.capacity() << endl;
cout << v1.size() << endl;
cout << "num=" << num << endl;
}
void test05() {
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//逆序遍历
for (vector<int>::reverse_iterator i = v1.rbegin(); i != v1.rend(); i++) {
cout << *i << endl;
}
vector<int>::iterator num = v1.begin()+3;
cout << *num << endl;
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
list<int>::iterator i = l1.begin();
i++;
cout << *i << endl;
}
int main() {
test01();
test02();
test03();
test04();
test05();
system("pause");
return EXIT_SUCCESS;
}
实例二,STL的简单实现
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
template<class T>
class myArray {
friend ostream& operator<<(ostream& out, myArray<T>& arr) {
cout << "this Array size " << arr.size << " capacity " << arr.capacity ;
return out;
}
private:
T *pArr;
int size;
int capacity;
public:
typedef T* iterator;
myArray(int capacity):capacity(capacity) {
this->size = 0;
this->pArr = new T[capacity];
}
~myArray() {
this->size = 0;
this->capacity = 0;
delete [] this->pArr;
this->pArr = NULL;
}
myArray* push(T val) {
if (this->size == this->capacity) {
return NULL;
}
this->pArr[this->size] = val;
this->size++;
return this;
}
T operator[](int index) {
if (index < this->size) {
return this->pArr[index];
}
else {
return NULL;
}
}
int Size() {
return this->size;
}
T* begin() {
return this->pArr;
}
T* end() {
return this->pArr + this->size;
}
};
template<typename T>
void print(T begin, T end) {
while (begin!=end) {
cout << (*begin) << endl;
begin++;
}
}
void test() {
myArray<int> arr(10);
for (int i = 0; i < 10; ++i) {
arr.push(i);
}
for (myArray<int>::iterator i = arr.begin();i!=arr.end(); ++i) {
cout << (*i) << endl;
}
cout << arr << endl;
print(arr.begin(), arr.end());
}
int main() {
test();
system("pause");
return EXIT_SUCCESS;
}