1 / 39

Lecture 2 – Dynamic array and Vector Container

Lecture 2 – Dynamic array and Vector Container. 1.Dynamic array 2.Template 3.Vector(STL). lecture 2 – Dynamic array and Vector Container. lecture 2 – Dynamic array and Vector Container. 数组的特点: 1. 数据存储空间是连续的,存储的数据是相同类型的数据; 2. 数据可以通过首个数据基址+偏移量访问,即下标; 3. 数据空间是否可变更(扩大、缩小)? 4. 数据访问是否安全?

morse
Download Presentation

Lecture 2 – Dynamic array and Vector Container

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2 – Dynamic array and Vector Container • 1.Dynamic array • 2.Template • 3.Vector(STL)

  2. lecture 2 – Dynamic array and Vector Container

  3. lecture 2 – Dynamic array and Vector Container 数组的特点: 1.数据存储空间是连续的,存储的数据是相同类型的数据; 2.数据可以通过首个数据基址+偏移量访问,即下标; 3.数据空间是否可变更(扩大、缩小)? 4.数据访问是否安全? 线性静态连续存储结构(数组) 线性动态连续存储结构(动态数组) 

  4. lecture 2 – Dynamic array and Vector Container C++语言支持的简单和结构化的数据类型: 1.数据存储空间自动分配; 2.数据操作方法系统给定,如[]操作; 3.用户使用时需声明变量类型,即可直接使用; 用户设计的复杂数据类型或数据结构: 1.用户自己分数据存储空间; 2.用户自己设计数据操作方法,如[]操作; 3.用户使用时需声明变量类型,即可直接使用; 数据+方法 数据+方法 ADT(抽象数据类型) 

  5. lecture 2 – Dynamic array and Vector Container Dynamic array: 1.动态申请一定空间  2.动态释放所申请的空间 3.数据输入(初始化) 4.数据访问(下标访问) 5.数据空间变更(扩大、缩小) 6.数据输出 Dynamic array: 1.动态申请一定空间 new 2.动态释放所申请的空间 delete [] 3.数据输入(初始化)construct 4.数据访问(下标访问)a[] 5.数据空间变更(扩大、缩小) new/delete 6.数据输出 /输入输出重载

  6. lecture 2 – Dynamic array and Vector Container 如何设计Dynamic array 面向对象程序设计:类设计和实现 类:数据+方法 1.数据的空间设计  总容量:capacity 实际存放数据量:size 数据的存放空间(动态申请):指针地址 2.方法是针对这些数据的访问和操作  下标操作(包括安全检查)  尾部元素操作  数组是否为空/数据量  Capacity Size Array address 0 0 0 0 0 0 0

  7. lecture 2 – Dynamic array and Vector Container Capacity Size Array address 1 2 3 4 5 6 7 空间扩大,原有数据需复制 1 2 3 4 5 6 7 8 1 2 3 4 5 空间缩小,原有数据需部分保留

  8. lecture 2 – Dynamic array and Vector Container class miniVector { public: miniVector(int size = 0); miniVector(const miniVector& obj); ~miniVector(); miniVector& operator= (const miniVector& rhs); int& back(); const int& back() const; int& operator[] (int i); const int& operator[] (int i) const;

  9. lecture 2 – Dynamic array and Vector Container void push_back(const int& item); void pop_back(); int size() const; bool empty() const; int capacity() const; private: int vCapacity; int vSize; int *vArr; void reserve(int n, bool copy); };

  10. lecture 2 – Dynamic array and Vector Container void miniVector::reserve(int n, bool copy) { int *newArr; int i; newArr = new int[n]; if (newArr == NULL) throw memoryAllocationError( "miniVector reserve(): memory allocation failure"); if (copy) for(i = 0; i < vSize; i++)newArr[i] = vArr[i]; if (vArr != NULL)delete [] vArr; vArr = newArr; vCapacity = n; }

  11. lecture 2 – Dynamic array and Vector Container miniVector::miniVector(int size): vSize(0), vCapacity(0), vArr(NULL) { int i; if (size == 0) return; reserve(size, false); vSize = size; for (i=0;i < vSize;i++) vArr[i] = 0; }

  12. lecture 2 – Dynamic array and Vector Container miniVector::miniVector (const miniVector& obj): vSize(0), vCapacity(0), vArr(NULL) { int i; if (obj.vSize == 0) return; reserve(obj.vSize, false); vSize = obj.vSize; for (i = 0; i < vSize; i++) vArr[i] = obj.vArr[i]; }

  13. lecture 2 – Dynamic array and Vector Container miniVector::~miniVector() { if (vArr != NULL) delete [] vArr; } int& miniVector::operator[] (int i) { if (i < 0 || i >= vSize) throw indexRangeError( "miniVector: index range error", i, vSize); return vArr[i]; }

  14. lecture 2 – Dynamic array and Vector Container int& miniVector::back() { if (vSize == 0) throw underflowError( "miniVector back(): vector empty"); return vArr[vSize-1]; } void miniVector::pop_back() { if (vSize == 0) throw underflowError( "miniVector pop_back(): vector is empty"); vSize--; }

  15. lecture 2 – Dynamic array and Vector Container void miniVector::push_back(const int& item) { if (vSize == vCapacity) { if (vCapacity == 0)reserve(1,false); else reserve(2 * vCapacity, true);//double size } vArr[vSize] = item; vSize++; } bool miniVector::empty() const { return vSize == 0; }

  16. lecture 2 – Dynamic array and Vector Container 思考: 1.若想申请char动态数组或float动态数组,该如何修改类声明和实现? 2.若想申请用户自定义数据类型,例如time类,又该如何修改类声明和实现? 解决办法: 使用模板-解决类通用性 

  17. lecture 2 – Dynamic array and Vector Container template <typename T> class miniVector { public: miniVector(int size = 0); miniVector(const miniVector<T>& obj); ~miniVector(); miniVector& operator= (const miniVector<T>& rhs); T& back(); const T& back() const; T& operator[] (int i); const T& operator[] (int i) const;

  18. lecture 2 – Dynamic array and Vector Container void push_back(const T& item); void pop_back(); int size() const; bool empty() const; int capacity() const; private: int vCapacity; int vSize; T *vArr; void reserve(int n, bool copy); };

  19. lecture 2 – Dynamic array and Vector Container template <typename T> void miniVector<T>::reserve(int n, bool copy) { T *newArr; int i; newArr = new T[n]; if (newArr == NULL) throw memoryAllocationError( "miniVector reserve(): memory allocation failure"); if (copy) for(i = 0; i < vSize; i++)newArr[i] = vArr[i]; if (vArr != NULL)delete [] vArr; vArr = newArr; vCapacity = n; }

  20. lecture 2 – Dynamic array and Vector Container miniVector<T>::miniVector(int size): vSize(0), vCapacity(0), vArr(NULL) { int i; if (size == 0) return; reserve(size, false); vSize = size; for (i=0;i < vSize;i++) vArr[i] = 0; }

  21. lecture 2 – Dynamic array and Vector Container miniVector<T>::miniVector (const miniVector& obj): vSize(0), vCapacity(0), vArr(NULL) { int i; if (obj.vSize == 0) return; reserve(obj.vSize, false); vSize = obj.vSize; for (i = 0; i < vSize; i++) vArr[i] = obj.vArr[i]; }

  22. lecture 2 – Dynamic array and Vector Container miniVector<T>::~miniVector() { if (vArr != NULL) delete [] vArr; } T& miniVector<T>::operator[] (int i) { if (i < 0 || i >= vSize) throw indexRangeError( "miniVector: index range error", i, vSize); return vArr[i]; }

  23. lecture 2 – Dynamic array and Vector Container T& miniVector<T>::back() { if (vSize == 0) throw underflowError( "miniVector back(): vector empty"); return vArr[vSize-1]; } void miniVector<T>::pop_back() { if (vSize == 0) throw underflowError( "miniVector pop_back(): vector is empty"); vSize--; }

  24. lecture 2 – Dynamic array and Vector Container void miniVector<T>::push_back(const int& item) { if (vSize == vCapacity) { if (vCapacity == 0)reserve(1,false); else reserve(2 * vCapacity, true);//double size } } bool miniVector<T>::empty() const { return vSize == 0; }

  25. lecture 2 – Dynamic array and Vector Container 声明变量: miniVector v1,v2(10),v3(5); 赋初值: v2.push_back(1); v2.push_back(2); V2.pop_back(); V2(v3); V2[0]=10; 思考:如何输出向量?

  26. Vector Class : STL vector container(容器) //Template class Template <typename T> Class Vector { public: vector(); vector(int n, const T& value=T()); vector(T *first, T *last); T& back(); void push_back(); void pop_back(); T& operator[](int i); … Private: int vCapacity; //amount of available space int vSize; //number of elements in the vector T *vArr; //the dynamic array }

  27. Construction Template <typename T> Vector<T>::Vector(int n, const T& value=T()) { T *newArr; int i; newArr=new T[n]; if(newArr==NULL) throw memoryAllocationError( “Vector Memory allocation failure”); vSize=n; vCapacity=n; for(i=0;i<vSize;i++) vArr[i]=T(); vArr=newArr; }

  28. CLASS vector <vector> Constructors vector(); Create an empty vector. This is the default constructor. vector(int n, const T& value = T()); Create a vector with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T(). 28 Main Index Contents

  29. CLASS vector <vector> Constructors vector(T *first, T *last); Initialize the vector using the address range [first, last). The notation *first and *last is an example of pointer notation 29 Main Index Contents

  30. CLASS vector <vector> Operations T& back(); Return the value of the item at the rear of the vector. Precondition: The vector must contain at least one element. const T& back() const; Constant version of back(). bool empty() const; Return true if the vector is empty and false otherwise. 30 Main Index Contents

  31. CLASS vector <vector> Operations T& operator[] (int i); Allow the vector element at index i to be retrieved or modified. Precondition: The index, i, must be in the range 0  i < n, where n is the number of elements in the vector. Postcondition: If the operator appears on the left side of an assignment statement, the expression on the right side modifies the element referenced by the index. const T& operator[] (int i) const; Constant version of the index operator. 31 Main Index Contents

  32. CLASS vector <vector> Operations void push_back(const T& value); Add a value at the rear of the vector. Postcondition: The vector has a new element at the rear and its size increases by 1. void pop_back(); Remove the item at the rear of the vector. Precondition: The vector is not empty. Postcondition: The vector has a new element at the rear or is empty. 32 Main Index Contents

  33. CLASS vector <vector> Operations void resize((int n, const T& fill = T()); Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained. Postcondition: The vector has size n. int size() const; Return the number of elements in the vector. 33 Main Index Contents

  34. lecture 2 – Dynamic array and Vector Container int intArray[5]={9,2,7,3,12} int arrSize=sizeof(intArray)/sizeof(int); Vector<int> intVector(intArray, intArray+arrSize); intVector.push_back(10); 一般文件应包含: #include<vector> #include<iostream> using namespace std;

  35. lecture 2 – Dynamic array and Vector Container http://msdn.microsoft.com/library MSDN Library开发工具和语言 \Visual Studio 2008 \Visual Studio \Visual C++ \Visual C++ 参考 \Visual C++ Libraries Reference \STL/CLR Library vector (STL/CLR)

  36. vector (STL/CLR) operator!= (vector) (STL/CLR) operator< (vector) (STL/CLR) operator<= (vector) (STL/CLR) operator== (vector) (STL/CLR) operator> (vector) (STL/CLR) operator>= (vector) (STL/CLR) vector::assign (STL/CLR) vector::at (STL/CLR) vector::back (STL/CLR) vector::back_item (STL/CLR) vector::begin (STL/CLR) vector::capacity (STL/CLR) vector::clear (STL/CLR) vector::const_iterator (STL/CLR) vector::const_reference (STL/CLR) vector::const_reverse_iterator (STL/CLR) vector::difference_type (STL/CLR) vector::empty (STL/CLR) vector::end (STL/CLR) vector::erase (STL/CLR) vector::front (STL/CLR) vector::front_item (STL/CLR) vector::generic_container (STL/CLR) vector::generic_iterator (STL/CLR) vector::generic_reverse_iterator (STL/CLR) vector::generic_value (STL/CLR) vector::insert (STL/CLR) vector::iterator (STL/CLR) vector::operator= (STL/CLR) vector::operator[] (STL/CLR) vector::pop_back (STL/CLR) vector::push_back (STL/CLR) vector::rbegin (STL/CLR) vector::reference (STL/CLR) vector::rend (STL/CLR) vector::reserve (STL/CLR) vector::resize (STL/CLR) vector::reverse_iterator (STL/CLR) vector::size (STL/CLR) vector::size_type (STL/CLR) vector::swap (STL/CLR) vector::to_array (STL/CLR) vector::value_type (STL/CLR) vector::vector (STL/CLR)

  37. lecture 2 – Dynamic array and Vector Container String类 string s1(“abc”),s2(5,’d’),s3(s1,2); string s4,s5; cout<<s1.size(); cin>>s4; //需包含#include<sstream> getline(cin,s5); S1.append(s2); S1.insert(1,s3,2); S1.replace(2,3,s4); S1.swap(s5); S1.find_first_of(s2,2); C风格字符串 Char a[]=“abc”,b[5]=“kxyz”; For(int i=0;a[i]!=‘\0’;i++) cout<<a[i]; strcat(a,b); strcpy(a,b); strcmp(a,b); strlen(a); //需包含#include<cstring>

  38. lecture 2 – Dynamic array and Vector Container 实验1 a.对比数组和向量的使用方法 使用函数实现冒泡排序算法,传递参数分别为数组和向量,要求原数据不被破坏; b.设计实现完整的miniVector类,并验证其正确性 主要在miniVector类中增加插入和删除方法。

  39. 主要的数据结构类型 两类基本结构:线性结构,非线性结构 线性结构包括:数组,向量,链表,栈,队列(列表结构) 非线性结构包括:二叉树,图 数据结构两个基本问题: 物理结构:物理存储方式 (1.静态的 2.动态的) 逻辑结构:数据间的逻辑关系 (1.线性 2.非线性) 注:线性结构:每个元素具有唯一前驱和唯一后继的数据结构,只有 首元素无前驱,只有尾元素无后继; 非线性结构:每个元素前驱唯一、后继元素不唯一(树); 每个元素前驱、后继元素均不唯一(图); 

More Related