1 / 49

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. 数据访问是否安全?

Télécharger la présentation

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 vArr[vSize] = item; vSize++; } } 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类-Standard C++ Library Char str[]=‘A string’; Str字符串长度是8。 字符数组不是C语言风格的字符串,除非结尾加入NULL Char arr[10]; arr[0]=‘C’;arr[1]=‘+’;arr[2]=‘+’;arr[3]=0;

  38. lecture 2 – Dynamic array and Vector Container String 在C++程序设计环境里,许多类的声明都包括C语言风格的字符串,如文件流的open()函数,需要使用C语言风格的字符串来标识文件。 Ifstream fin; fin.open(<C-style string>) 例如: string filename=“input.dat”; fin.open(filename.c_str()); 参考:http://wenku.baidu.com/view/a1a727fb770bf78a65295486.html

  39. lecture 2 – Dynamic array and Vector Container String类-Standard C++ Library Members: (字符串比较大小) operator+ operator!= operator== operator< operator<= operator<< operator> operator>= operator>>

  40. lecture 2 – Dynamic array and Vector Container String类-Standard C++ Library Function: getline( _Istr(输入流), _Str(读入后的字符串), ( ‘\n’ )(结束符) ). 例如: 输入:Jdoe:vswerfa string username,userpwd; ifstream fin; getline(fin,usename,’:’); getline(fin,usepwd,’/n’);

  41. lecture 2 – Dynamic array and Vector Container basic_string Members append :追加 assign :追加新字符到字串 at :返回特定位置的元素引用 c_str :Converts the contents of a string as a C-style compare copy erase find find_first_of find_last_of insert push_back replace reserve resize size substr swap ……

  42. lecture 2 – Dynamic array and Vector Container C风格字符串 #include<string.h> char a[4]=“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); String类 #include<string> string s1(“abc”),s2(5,’d’),s3(s1,2); string s4,s5; cout<<s1.size(); cin>>s4; 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);

  43. lecture 2 – Dynamic array and Vector Container 模式匹配 1、简单模式匹配算法 O(m*n) 正文:a b a b c a b c a a b c b (n) 字串:a b a b b (m) 匹配过程: i=1 a b a b c a b c a a b c b a b a b b i=2 a b a b c a b c a a b c b a b a bb i=3 a b a b c a b c a a b c b a b a bb ……. 时间复杂度O(m*n)

  44. lecture 2 – Dynamic array and Vector Container 2、Knuth-Morris-Pratt(KMP)模式匹配算法 O(m+n) (基于Cook提出理论,任何一个可以使用被称为下推自动机的计算机抽象模型来解决的问题,也可以使用一个实际的计算机在与问题规模对应的时间内解决,这个理论暗示模式匹配算法理论上可以达到O(m+n)) 正文:a b a b c a b c a a b c b a a b a a b c 子串:a b a b c a b a b a b c 匹配过程: i=8 a b a b c a b c a a b c b a a b a a b c a b a b c a b a b a b c j=8

  45. lecture 2 – Dynamic array and Vector Container 2、Knuth-Morris-Pratt(KMP)模式匹配算法 i=8 a b a b c a bc a a b c b a a b a a b c a b a b c a b a b a b c j=8 i=8 a b a b c a b c a a b c b a a b a a b c a ba b c a bab a b c j=3 如何知道将字串移到此处?

  46. lecture 2 – Dynamic array and Vector Container 正文:a b a b c a b c a a b c b a a b a a b c 子串: a b a b c a b a b a b c 对字串计算下一次比较位的next[ ]值: next[ ] 0 1 1 2 3 1 2 3 4 5 4 5 a b a b c a b a b a b c (计算b之前与子串从头开始最长的匹配字符长度) 建立了next[ ]后,就可以直接使用next[ ]确定下次移动、比较的字符位置,如: a b a b c a bc a a b c b a a b a a b c a b a b c a b a b a b c 0 1 1 2 3 1 2 3 4 5 4 5 a b a b c a b a b a b c 0 1 1 2 3 1 2 3 4 5 4 5 a b ab c a b a b a b c

  47. lecture 2 – Dynamic array and Vector Container a b a b c a b c aa b c b a a b a a b c a b ab c a b a b a b c 0 1 1 2 3 1 2 3 4 5 4 5 ab a b c a b a b a b c 0 1 1 2 3 1 2 3 4 5 4 5 a bab c a b a b a b c 0 11 2 3 1 2 3 4 5 4 5 a b a b c a b c aa b cba a b a a b c a bab c a b a b a b c 0 11 2 3 1 2 3 4 5 4 5 a b ab c a b a b a b c 011 2 3 1 2 3 4 5 4 5 ……. KMP算法避免i下标回溯,即一直向前不折回;j下标会部分折回;当最终完全匹配后,j下标完成一趟扫描。所以,算法最终会达到O(m+n)。

  48. lecture 2 – Dynamic array and Vector Container 3、Boyer-Moore(BM)字符串匹配算法 例子:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 w e t h e p e o p l e o f 子串 b l e s s i n g s b l e s s i n g s 需提前构造辅助字符数组,记录子串中每种字符在子串中最后出现的下标值,如: position[a]=0,posotion[b]=1,position[c]=0,…… Position[e]=2;position[s]=8;……. BM算法预处理时间复杂度为O(m+s),空间复杂度为O(s),s是与P, T相关的有限字符集长度,搜索阶段时间复杂度为O(m·n)。 最好情况下的时间复杂度为O(n/m),最坏情况下时间复杂度为O(m·n)。

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

More Related