C++ Vectors

来源:互联网 发布:it服务与外包 编辑:程序博客网 时间:2024/06/06 02:16
C++ Vectors
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/91c8cabc-403d-419a-9ff8-612f16671f9a.htmVectors 包含着一系列连续存储的元素,其行为和数组类似。访问Vector中的任意元素或从末尾添加元素都可以在常量级时间复杂度内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是线性时间复杂度。Constructors 构造函数 Operators 对vector进行赋值或比较 assign() 对Vector中的元素赋值 at() 返回指定位置的元素 back() 返回最末一个元素 begin() 返回第一个元素的迭代器 capacity() 返回vector所能容纳的元素数量(在不重新分配内存的情况下) clear() 清空所有元素 empty() 判断Vector是否为空(返回true时为空) end() 返回最末元素的迭代器(译注:实指向最末元素的下一个位置) erase() 删除指定元素 front() 返回第一个元素 get_allocator() 返回vector的内存分配器 insert() 插入元素到Vector中 max_size() 返回Vector所能容纳元素的最大数量(上限) pop_back() 移除最后一个元素 push_back() 在Vector最后添加一个元素 rbegin() 返回Vector尾部的逆迭代器 rend() 返回Vector起始的逆迭代器 reserve() 设置Vector最小的元素容纳数量 resize() 改变Vector元素数量的大小 size() 返回Vector元素数量的大小 swap() 交换两个Vector 
#include <vector>#include <iostream>//构造函数void VectorConstructor(void);//擦除的载体和复制指定元素的空载体void Vector_assign(void);//返回在载体中的指定位置的参考元件void Vector_at(void);//返回到向量的最后一个元素的引用void Vector_back(void);//返回一个迭代到在容器中的第一个元素void Vector_begin(void);//返回存储分配的长度void Vector_capacity(void);//擦除向量的元素void Vector_clear(void);//测试容器是否为空void Vector_empty(void);//返回迭代结束void Vector_end(void);//擦除元素void Vector_erase(void);//返回一个矢量到第一元素的引用void Vector_front(void);//返回用于构造矢量分配器对象的副本void Vector_get_allocator(void);//插入一个元素或多个元素到载体的指定位置的。void Vector_insert(void);//返回向量的最大长度void Vector_max_size(void);//删除在矢量的末端的元件void Vector_pop_back(void);//一个元素添加到载体的末端void Vector_push_back(void);//返回一个迭代器在反方向向量的第一个元素void Vector_rbegin(void);//返回一个迭代一个反方向向量的结束void Vector_rend(void);//保留存储的为一个矢量对象的最小长度void Vector_reserve(void);//指定的载体的新的大小void Vector_resize(void);//返回该向量的元素数void Vector_size(void);//交换两个向量的元素void Vector_swap(void);//返回在指定的位置上的参照矢量元素void Vector_operator(void);int main(){VectorConstructor();Vector_assign();Vector_at();Vector_back();Vector_begin();Vector_capacity();Vector_clear();Vector_empty();Vector_end();Vector_erase();Vector_front();Vector_get_allocator();Vector_insert();Vector_max_size();Vector_pop_back();Vector_push_back();Vector_rbegin();Vector_rend();Vector_reserve();Vector_resize();Vector_size();Vector_swap();Vector_operator();return 0;}//构造函数void VectorConstructor(void){using namespace std;vector <int>::iterator v1_Iter, v2_Iter, v3_Iter, v4_Iter, v5_Iter;// Create an empty vector v0vector <int> v0;// Create a vector v1 with 3 elements of default value 0vector <int> v1(3);// Create a vector v2 with 5 elements of value 2vector <int> v2(5, 2);// Create a vector v3 with 3 elements of value 1 and with the allocator // of vector v2vector <int> v3(3, 1, v2.get_allocator());// Create a copy, vector v4, of vector v2vector <int> v4(v2);// Create a vector v5 by copying the range v4[_First, _Last)vector <int> v5(v4.begin() + 1, v4.begin() + 3);cout << "v1 =";for (v1_Iter = v1.begin(); v1_Iter != v1.end(); v1_Iter++)cout << " " << *v1_Iter;cout << endl;cout << "v2 =";for (v2_Iter = v2.begin(); v2_Iter != v2.end(); v2_Iter++)cout << " " << *v2_Iter;cout << endl;cout << "v3 =";for (v3_Iter = v3.begin(); v3_Iter != v3.end(); v3_Iter++)cout << " " << *v3_Iter;cout << endl;cout << "v4 =";for (v4_Iter = v4.begin(); v4_Iter != v4.end(); v4_Iter++)cout << " " << *v4_Iter;cout << endl;cout << "v5 =";for (v5_Iter = v5.begin(); v5_Iter != v5.end(); v5_Iter++)cout << " " << *v5_Iter;cout << endl;return ;/*程序执行结果v1 = 0 0 0v2 = 2 2 2 2 2v3 = 1 1 1v4 = 2 2 2 2 2v5 = 2 2请按任意键继续. . .*/}//将区间[first, last)的元素赋值到当前的vector容器中,或者赋n个值为x的元素到vector容器中//这个容器会清除掉vector容器中以前的内容void Vector_assign(void){using namespace std;vector<int> v1, v2, v3;vector<int>::iterator iter;v1.push_back(10);v1.push_back(20);v1.push_back(30);v1.push_back(40);v1.push_back(50);cout << "v1 = ";for (iter = v1.begin(); iter != v1.end(); iter++)cout << *iter << " ";cout << endl;v2.assign(v1.begin(), v1.end());cout << "v2 = ";for (iter = v2.begin(); iter != v2.end(); iter++)cout << *iter << " ";cout << endl;v3.assign(7, 4);cout << "v3 = ";for (iter = v3.begin(); iter != v3.end(); iter++)cout << *iter << " ";cout << endl;return;/*程序执行结果v1 = 10 20 30 40 50v2 = 10 20 30 40 50v3 = 4 4 4 4 4 4 4请按任意键继续. . .*/}//返回在载体中的指定位置的参考元件void Vector_at(void){using namespace std;vector <int> v1;v1.push_back(10);v1.push_back(20);const int &i = v1.at(0);int &j = v1.at(1);cout << "The first element is " << i << endl;cout << "The second element is " << j << endl;return;/*程序执行结果如下The first element is 10The second element is 20请按任意键继续. . .*/}//返回到向量的最后一个元素的引用void Vector_back(void){using namespace std;vector <int> v1;v1.push_back(10);v1.push_back(11);int& i = v1.back();const int& ii = v1.front();cout << "The last integer of v1 is " << i << endl;i--;cout << "The next-to-last integer of v1 is " << ii << endl;return;/*程序执行结果如下The last integer of v1 is 11The next-to-last integer of v1 is 10请按任意键继续. . .*/}//返回一个迭代到在容器中的第一个元素void Vector_begin(void){using namespace std;vector<int> c1;vector<int>::iterator c1_Iter;vector<int>::const_iterator c1_cIter;c1.push_back(1);c1.push_back(2);cout << "The vector c1 contains elements:";c1_Iter = c1.begin();for (; c1_Iter != c1.end(); c1_Iter++){cout << " " << *c1_Iter;}cout << endl;cout << "The vector c1 now contains elements:";c1_Iter = c1.begin();*c1_Iter = 20;for (; c1_Iter != c1.end(); c1_Iter++){cout << " " << *c1_Iter;}cout << endl;// The following line would be an error because iterator is const// *c1_cIter = 200;return;/*程序执行结果如下The vector c1 contains elements: 1 2The vector c1 now contains elements: 20 2请按任意键继续. . .*/}//返回存储分配的长度void Vector_capacity(void){using namespace std;vector <int> v1;v1.push_back(1);cout << "The length of storage allocated is "<< v1.capacity() << "." << endl;v1.push_back(2);cout << "The length of storage allocated is now "<< v1.capacity() << "." << endl;return;/*程序执行结果如下The length of storage allocated is 1.The length of storage allocated is now 2.请按任意键继续. . .*/}//擦除向量的元素void Vector_clear(void){using namespace std;vector <int> v1;v1.push_back(10);v1.push_back(20);v1.push_back(30);cout << "The size of v1 is " << v1.size() << endl;v1.clear();cout << "The size of v1 after clearing is " << v1.size() << endl;return;/*程序执行结果如下The size of v1 is 3The size of v1 after clearing is 0请按任意键继续. . .*/}//测试容器是否为空void Vector_empty(void){using namespace std;vector <int> v1;v1.push_back(10);if (v1.empty())cout << "The vector is empty." << endl;elsecout << "The vector is not empty." << endl;return;/*程序执行结果如下The vector is not empty.请按任意键继续. . .*/}//返回迭代结束void Vector_end(void){using namespace std;vector <int> v1;vector <int>::iterator v1_Iter;v1.push_back(1);v1.push_back(2);for (v1_Iter = v1.begin(); v1_Iter != v1.end(); v1_Iter++)cout << *v1_Iter << endl;//error//vector <int>::iterator v1_IterTest = v1.end();//cout << *v1_IterTest << endl;/*程序执行结果如下:12请按任意键继续. . .*/return;}//擦除元素void Vector_erase(void){using namespace std;vector <int> v1;vector <int>::iterator Iter;v1.push_back(10);v1.push_back(20);v1.push_back(30);v1.push_back(40);v1.push_back(50);cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;v1.erase(v1.begin());cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;v1.erase(v1.begin() + 1, v1.begin() + 3);cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;return;/*程序执行结果如下v1 = 10 20 30 40 50v1 = 20 30 40 50v1 = 20 50请按任意键继续. . .*/}//返回一个矢量到第一元素的引用void Vector_front(void){using namespace std;vector <int> v1;v1.push_back(10);v1.push_back(11);int& i = v1.front();const int& ii = v1.front();cout << "The first integer of v1 is " << i << endl;i++;cout << "The second integer of v1 is " << ii << endl;return;/*The first integer of v1 is 10The second integer of v1 is 11请按任意键继续. . .*/}//返回用于构造矢量分配器对象的副本void Vector_get_allocator(void){using namespace std;// The following lines declare objects that use the default allocator.vector<int> v1;vector<int, allocator<int> > v2 = vector<int, allocator<int> >(allocator<int>());// v3 will use the same allocator class as v1vector <int> v3(v1.get_allocator());vector<int>::allocator_type xvec = v3.get_allocator();// You can now call functions on the allocator class used by vecreturn;}//插入一个元素或多个元素到载体的指定位置的。void Vector_insert(void){using namespace std;vector <int> v1;vector <int>::iterator Iter;v1.push_back(10);v1.push_back(20);v1.push_back(30);cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;v1.insert(v1.begin() + 1, 40);cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;v1.insert(v1.begin() + 2, 4, 50);cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;v1.insert(v1.begin() + 1, v1.begin() + 2, v1.begin() + 4);cout << "v1 =";for (Iter = v1.begin(); Iter != v1.end(); Iter++)cout << " " << *Iter;cout << endl;return;/*程序执行结果如下v1 = 10 20 30v1 = 10 40 20 30v1 = 10 40 50 50 50 50 20 30v1 = 10 50 50 40 50 50 50 50 20 30请按任意键继续. . .*/}//返回向量的最大长度void Vector_max_size(void){using namespace std;vector <int> v1;vector <int>::size_type i;i = v1.max_size();cout << "The maximum possible length of the vector is " << i << "." << endl;return;/*The maximum possible length of the vector is 1073741823.请按任意键继续. . .*/}//删除在矢量的末端的元件void Vector_pop_back(void){using namespace std;vector <int> v1;v1.push_back(1);cout << v1.back() << endl;v1.push_back(2);cout << v1.back() << endl;v1.pop_back();cout << v1.back() << endl;return;/*程序执行结果如下121请按任意键继续. . .*/}//一个元素添加到载体的末端void Vector_push_back(void){using namespace std;vector <int> v1;v1.push_back(1);if (v1.size() != 0)cout << "Last element: " << v1.back() << endl;v1.push_back(2);if (v1.size() != 0)cout << "New last element: " << v1.back() << endl;return;/*程序执行结果如下Last element: 1New last element: 2请按任意键继续. . .*/}//返回一个迭代器在反方向向量的第一个元素void Vector_rbegin(void){using namespace std;vector <int> v1;vector <int>::iterator v1_Iter;vector <int>::reverse_iterator v1_rIter;v1.push_back(1);v1.push_back(2);v1_Iter = v1.begin();cout << "The first element of vector is "<< *v1_Iter << "." << endl;v1_rIter = v1.rbegin();cout << "The first element of the reversed vector is "<< *v1_rIter << "." << endl;return;/*程序执行结果如下The first element of vector is 1.The first element of the reversed vector is 2.请按任意键继续. . .*/}//返回一个迭代一个反方向向量的结束void Vector_rend(void){using namespace std;vector <int> v1;vector <int>::reverse_iterator v1_rIter;v1.push_back(1);v1.push_back(2);for (v1_rIter = v1.rbegin(); v1_rIter != v1.rend(); v1_rIter++)cout << *v1_rIter << endl;return;/*程序执行结果如下21请按任意键继续. . .*/}//保留存储的为一个矢量对象的最小长度void Vector_reserve(void){using namespace std;vector <int> v1;//vector <int>::iterator Iter;v1.push_back(1);cout << "Current capacity of v1 = "<< v1.capacity() << endl;v1.reserve(20);cout << "Current capacity of v1 = "<< v1.capacity() << endl;return;/*程序执行结果如下Current capacity of v1 = 1Current capacity of v1 = 20请按任意键继续. . .*/}//指定的载体的新的大小void Vector_resize(void){using namespace std;vector <int> v1;v1.push_back(10);v1.push_back(20);v1.push_back(30);v1.resize(4, 40);cout << "The size of v1 is " << v1.size() << endl;cout << "The value of the last object is " << v1.back() << endl;v1.resize(5);cout << "The size of v1 is now " << v1.size() << endl;cout << "The value of the last object is now " << v1.back() << endl;return;/*The size of v1 is 4The value of the last object is 40The size of v1 is now 5The value of the last object is now 0请按任意键继续. . .*/}//返回该向量的元素数void Vector_size(void){using namespace std;vector <int> v1;vector <int>::size_type i;v1.push_back(1);i = v1.size();cout << "Vector length is " << i << "." << endl;v1.push_back(2);i = v1.size();cout << "Vector length is now " << i << "." << endl;return;/*Vector length is 1.Vector length is now 2.请按任意键继续. . .*/}//交换两个向量的元素void Vector_swap(void){using namespace std;vector <int> v1, v2;v1.push_back(1);v1.push_back(2);v1.push_back(3);v2.push_back(10);v2.push_back(20);cout << "The number of elements in v1 = " << v1.size() << endl;cout << "The number of elements in v2 = " << v2.size() << endl;cout << endl;v1.swap(v2);cout << "The number of elements in v1 = " << v1.size() << endl;cout << "The number of elements in v2 = " << v2.size() << endl;return;/*The number of elements in v1 = 3The number of elements in v2 = 2The number of elements in v1 = 2The number of elements in v2 = 3请按任意键继续. . .*/}//返回在指定的位置上的参照矢量元素void Vector_operator(void){using namespace std;vector <int> v1;v1.push_back(10);v1.push_back(20);int& i = v1[1];cout << "The second integer of v1 is " << i << endl;return;/*The second integer of v1 is 20请按任意键继续. . .*/}


0 0