c++ vector容器
1.vector
头文件:#include<vector>
template < class T, class Alloc = allocator<T> > class vector;
vector容器向量,是一个类模板,但不是一种数据类型,vector<char>等才是数据类型。vector的结构类似于动态数组,能自动扩展容量。
vector将元素复制到内部的动态数组里,元素之间存在着某种顺序,是一种有序群集。存储空间是连续的,支持随机存取,能在常数级时间内存取任何一个元素。
在尾端添加或删除元素时,vector的性能相当好。但在中部或前端按插或删除时,性能就不太好来,因为操作位置后面的元素都要移动,所以vector并没有push_front() (头插)和pop_front()(删除第一个元素)成员函数,
2.vector容器中常用的函数。(c为一个容器对象)
c.push_back(elem); 在容器最后位置添加一个元素elem
c.pop_back(); 删除容器最后位置处的元素
c.at(index); 返回指定index位置处的元素
c.begin(); 返回指向容器最开始位置数据的指针
c.end(); 返回指向容器最后一个数据单元的指针+1
c.front(); 返回容器最开始单元数据的引用
c.back(); 返回容器最后一个数据的引用
c.max_size(); 返回容器的最大容量
c.size(); 返回当前容器中实际存放元素的个数
c.capacity(); 同c.size()
c.resize(); 重新设置vector的容量
c.reserve(); 同c.resize()
c.erase(p); 删除指针p指向位置的数据,返回下指向下一个数据位置的指针(迭代器)
c.erase(begin,end) 删除begin,end区间的数据,返回指向下一个数据位置的指针(迭代器)
c.clear(); 清除所有数据
c.rbegin(); 将vector反转后的开始指针返回(其实就是原来的end-1)
c.rend(); 将vector反转后的结束指针返回(其实就是原来的begin-1)
c.empty(); 判断容器是否为空,若为空返回true,否则返回false
c1.swap(c2); 交换两个容器中的数据
c.insert(p,elem); 在指针p指向的位置插入数据elem,返回指向elem位置的指针
c.insert(p,n,elem); 在位置p插入n个elem数据,无返回值
c.insert(p,begin,end) 在位置p插入在区间[begin,end)的数据,无返回值
3.#include<algorithm>中的vecor常用的泛函算法
搜索算法:find() 、search() 、count() 、find_if() 、search_if() 、count_if()
分类排序:sort() 、merge()
删除算法:unique() 、remove()
生成和变异:generate() 、fill() 、transformation() 、copy()
关系算法:equal() 、min() 、max()
3.实例:
1)大小和容量:
vector的用于操作大小的函数有size()(元素的个数),empty() (判断是否为空),max_size()(能容纳的最大元素个数),capacity()(vector实际能容纳的元素数量)。
可以使用reserve()保留适当容量,避免一再重新分配内存。
#include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> void shrinkCapacity(std::vector<T>& v) { std::vector<T> temp(v); v.swap(temp); } int main() { vector<int> v(100);//初始化期间向构造函数传递附加参数,构造出足够的空间。 cout << "v.capacity()" << v.capacity() << endl; v.reserve(80); cout << "v.reserve(80)后,v.size() = " << v.size(); cout << ", v.capacity() =" << v.capacity() <<endl; for (int i =1; i <=10; ++i) v.push_back(i); cout << "v.capacity() =" << v.capacity() << ",v.size=" <<v.size()<<endl; shrinkCapacity(v); cout << "v.capacity() =" << v.capacity() << ",v.size=" <<v.size()<<endl; return 0; } ~ ~
输出:
v.capacity() =100
v.reserve(80)后,v.size() = 100, v.capacity() =100
v.capacity() =200,v.size=110
v.capacity() =110,v.size=110
size()的结果为什么是100或110呢,前100Xint大小的空间并没有插入元素啊??
从结果看到,使用push_back()从后面插入10个元素后,v.size()变成了110,从第100个位置插入元素。capacity()由实作版本决定,上面的例子,插入元素后,capacity()的大小增加了一倍。
vector的容量,概念上和strings类似,不过有一个不同:vector不能使用reserve()来缩减容量。如果调用reserve()所给的容量比当前的vector容量要小,不会有任何反应(如上面的例子)。
实例2:
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { vector<string> sentence; sentence.reserve(5); sentence.pusn_back("Hello,"); sentence.pusn_back("how"); sentence.pusn_back("are"); sentence.pusn_back("you"); sentence.pusn_back("?"); copy(sentence.begin(),sentence.end(),ostream_iterator<string>(cout," ")); cout << endl; cout << "max_size(): " << sentence.max_size() << endl; cout << "size(): " << sentence.size() <<endl; cout << "capacity(): " << sentence.capacity() <<endl; swap(sentence[1],sentence[3]); sentence.insert(find(sentence.begin(),sentence.end(),"?"),"always"); sentence.back() ="!"; copy(sentence.begin(),sentence.end(),ostream_iterator<string>(cout," ")); cout <,endl; cout << "max_size(): " << sentence.max_size() << endl; cout << "size(): " << sentence.size() <<endl; cout << "capacity(): " << sentence.capacity() <<endl; }
输出:
Hello, how are you ?
max_size(): 1073741823
size(): 5
capacity(): 5
Hello, you are how always !
max_size(): 1073741823
size(): 6
capacity(): 10 //又增加来一倍
max_size()和capaticy()的结果由实作版本决定。
2)vector的构造函数与赋值(初始化操作)
vector<T> C 产生一个空的vector,
vector<T> C1(C2) 拷贝C2的所有元素,用于初始化C1
vector<T> C(n) 利用元素的default构造函数生成一个大小为n的vector
vector<T> c(n,elem) 产生一个大小为n,每个元素值都是elem的vector
vector<T> c(beg,end) 产生一个vector,以区间[beg,end]作为元素初值。
实例:
#include <iostream> #include <vector> #include <list> #include <algorithm> #include <iterator> #include "print.h" using namespace std; int main() { list<int> lst; for (int i =1; i <9; i++) lst.push_back(i); vector<float> v(lst.begin(),lst.end()); PRINT_ELEMENTS(v,"copy all elements of the list as floats into a vector:\n"); int array[] ={10,11,12,13,14,15,16,17,18}; vector<int> v2(array,array+sizeof(array)/sizeof(array[0])); PRINT_ELEMENTS(v2,"copy all the elements of array into a vector:\n"); // vector<int> v3((std::istream_iterator<int>(std::cin)),(std::istream_iterator<int>())); // PRINT_ELEMENTS(v3,"read all integer elements of the deque from standard input:\n"); vector<int> v4(v2); PRINT_ELEMENTS(v4,"copy all elements of v3:"); vector<int> v5(10,10); PRINT_ELEMENTS(v5,"Create vector with all the elem is 10:"); return 0; } ~
实例3:
#include <iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std; int main() { vector<int> v; for (int i =1; i <11; ++i) v.push_back(i); vector<int>::iterator it; cout << "顺序输出v:"; for (it =v.begin(); it !=v.end(); ++it) cout << *it << ' '; cout <<endl; cout << "逆序输出v:" ; // for (it =v.rbegin(); it !=v.rend(); ++it)//error ??why? for (it =v.end()-1; it !=v.begin();--it) cout << *it << ' '; cout <<*it <<endl; cout << "v的第一个元素:" <<v.front() <<endl; cout << "v的最后一个元素:" << v.back() <<endl; it =v.begin(); vector<int> v2(5,*it); vector<int> v3(it+1,it+5); vector<int> v4(v); cout << "v2(5,*it),用5个v.begin()初始化v2:"; for (it =v2.begin(); it !=v2.end(); ++it) cout << *it << ' '; cout <<endl; cout << "it=v.begin(),用(it+1,it+5)区间的元素初始化v3:"; for (it =v3.begin(); it !=v3.end(); ++it) cout << *it << ' '; cout << endl; cout << "用v初始化v4:"; for (it =v4.begin(); it !=v4.end(); ++it) cout << *it << ' '; cout << endl; it =v4.end()-1; v.insert(v.end(),*it+1); cout << "it =v4.end()-1,v.insert(v.end(),*it+1):"; for (it =v.begin(); it !=v.end(); ++it) cout << *it << ' '; cout << endl; it =v.end()-1; v.resize(15,*it+1); cout <<"v.resize(15,*it+1):将v的元素数量改为15,多出来的元素都是(*it+1)的副本:"<<endl; for (it =v.begin(); it !=v.end(); ++it) cout << *it << ' '; cout <<endl; v4.pop_back(); cout << "v4 pop_back删除最后一个元素后:"; for (it =v4.begin(); it!=v4.end(); ++it) cout << *it << ' ' ; cout <<endl; v.erase(v.begin()+10,v.end()); cout << "v.erase(v.begin()+10,v.end()): "; for (it =v.begin(); it!=v.end(); ++it) cout << *it << ' ' ; cout <<endl; cout << "v2.clear()前v2.empty()=" <<v2.empty() <<endl; v2.clear(); cout << "v2.clear()后v2.empty()=" <<v2.empty() <<endl; }
输出:
顺序输出v:1 2 3 4 5 6 7 8 9 10
逆序输出v:10 9 8 7 6 5 4 3 2 1
v的第一个元素:1
v的最后一个元素:10
v2(5,*it),用5个v.begin()初始化v2:1 1 1 1 1
it=v.begin(),用(it+1,it+5)区间的元素初始化v3:2 3 4 5
用v初始化v4:1 2 3 4 5 6 7 8 9 10
it =v4.end()-1,v.insert(v.end(),*it+1):1 2 3 4 5 6 7 8 9 10 11
v.resize(15,*it+1):将v的元素数量改为15,多出来的元素都是(*it+1)的副本:
1 2 3 4 5 6 7 8 9 10 11 12 12 12 12
v4 pop_back删除最后一个元素后:1 2 3 4 5 6 7 8 9
v.erase(v.begin()+10,v.end()): 1 2 3 4 5 6 7 8 9 10
v2.clear()前v2.empty()=0
v2.clear()后v2.empty()=1