1 / 29

Vector Class in C++ (Dynamic Array) TA class

Vector Class in C++ (Dynamic Array) TA class. > April 2008, Amirkabir university, CS department. Provided by Sina Khak Abi. Introduction. Everybody are familiar with the Array. Does the Array satisfy you? Is there any problem with the Array? What are the problems?

Download Presentation

Vector Class in C++ (Dynamic Array) TA class

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. Vector Class in C++(Dynamic Array)TA class > April 2008, Amirkabir university, CS department Provided by Sina Khak Abi

  2. Introduction • Everybody are familiar with the Array. • Does the Array satisfy you? • Is there any problem with the Array? • What are the problems? • Do you think Array is constraint? • What are the constraints?

  3. Definition of Array • When the size is known (fix): int arr[40]; char ch[80]; • When the size is unknown: int n; … n=16; … int *arr; arr = new int[n];

  4. Constraints of Array • Size!!! Array has a fix size. Can you change the size easily? • Can you delete an element from the Array easily? • Can you easily find the Length of the Array at any time you need?

  5. Constraints • How can we delete an element? • How can we find the size of Array? • ... How can we overcome these constraints?

  6. Yes!!! VECTOR Really POWERFUL!!!

  7. What is Vector Class • Vector class is a template class. • You can use it exactly like array. (You can see it in examples later).

  8. Implementation • Vector header file should be included. (include <vector>;)

  9. Implementation vector<Type> VectorName; Examples: vector<int> integerVec; vector<double> doubleVec; Now your vector is defined. But it is not useable. Because it is EMPTY.

  10. Implementation Functions: push_back(value): Example: vector <int> vec1; vec1.push_back(18); Now vec1 has the value 18 in the first place. (vec1[0] has the value 18).

  11. Implementation Functions: push_back(value): Use of this function is not limited. As you push values using this function, the length increases.

  12. Implementation Functions: pop_back(): Example: vec1.pop_back(); This function deletes the last member of the vector. But does not returns any thing.

  13. Implementation Functions: size(): Example: int vecLength; vecLength = vec1.size(); This function returns the size (length) of the vector, at any time you need.

  14. Implementation Functions: clear(): Example: vec1.clear(); This function clears all members of the vector, So after calling this function the vector will become EMPTY.

  15. Implementation Functions: empty(): Example: if(vec1.empty()) { cout<<“The vector is empty”; } No description is needed.

  16. Questions ?

  17. Implementation/Iterator • Iterator is nearly like pointer. • You can have access to elements of the vector using Iterator. Example: vector<Type>::iterator iteratorName; Now the iteratorName is a pointer to your vector. You should point it to an element of your vector. You can use these functions:

  18. Implementation/Iterator Functions: begin(): Example: vector<int>::iterator iter; iter = vec1.begin(); This function returns an iterator for the first element of your vector.

  19. Implementation/Iterator Functions: end(): Example: iter = vec1.end(); This function returns an iterator for the last element of your vector.

  20. Implementation/Iterator Now how can we use iterator: Example: for(i=0; i<vec1.size(); i++) cout << *(iter+i);

  21. Implementation One of the most important usage of Iterator is in the function erase(). This function deletes an element. Which element? The element which its iterator is send for the function.

  22. Implementation Example: iter = vec1.begin(); vec1.erase(iter+4); The 5th element of vec1 will be deleted. (the element with index 4).

  23. Exercise What do these functions do? insert resize swap

  24. Questions ?

  25. 2D Vectors Is it possible to have 2D vector? Of course it is possible. But how?

  26. 2D Vectors YES!!! We can have vectors of vectors. (Every element of the vector is vector). Example: vector < vector <Type> > vectorName;

  27. Exercise • How can we put elements into a 2D vector? (How can we use push_back, pop_back or other functions?) • Is it possible to have different lengths in every row of a 2D vector? • Can we have 3D, 4D, … vectors? How?

  28. Questions ?

More Related