590 likes | 1.07k Views
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?
E N D
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? • Do you think Array is constraint? • What are the constraints?
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];
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?
Constraints • How can we delete an element? • How can we find the size of Array? • ... How can we overcome these constraints?
Yes!!! VECTOR Really POWERFUL!!!
What is Vector Class • Vector class is a template class. • You can use it exactly like array. (You can see it in examples later).
Implementation • Vector header file should be included. (include <vector>;)
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.
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).
Implementation Functions: push_back(value): Use of this function is not limited. As you push values using this function, the length increases.
Implementation Functions: pop_back(): Example: vec1.pop_back(); This function deletes the last member of the vector. But does not returns any thing.
Implementation Functions: size(): Example: int vecLength; vecLength = vec1.size(); This function returns the size (length) of the vector, at any time you need.
Implementation Functions: clear(): Example: vec1.clear(); This function clears all members of the vector, So after calling this function the vector will become EMPTY.
Implementation Functions: empty(): Example: if(vec1.empty()) { cout<<“The vector is empty”; } No description is needed.
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:
Implementation/Iterator Functions: begin(): Example: vector<int>::iterator iter; iter = vec1.begin(); This function returns an iterator for the first element of your vector.
Implementation/Iterator Functions: end(): Example: iter = vec1.end(); This function returns an iterator for the last element of your vector.
Implementation/Iterator Now how can we use iterator: Example: for(i=0; i<vec1.size(); i++) cout << *(iter+i);
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.
Implementation Example: iter = vec1.begin(); vec1.erase(iter+4); The 5th element of vec1 will be deleted. (the element with index 4).
Exercise What do these functions do? insert resize swap
2D Vectors Is it possible to have 2D vector? Of course it is possible. But how?
2D Vectors YES!!! We can have vectors of vectors. (Every element of the vector is vector). Example: vector < vector <Type> > vectorName;
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?