DATA STRUCTURE WITH C++ PROGRAMMING

USING VECTOR

An array is of fixed size, C++ doesn't have inbuilt function to add elements to or delete element from an array (Except you use cin and declare variable to store elements to be added to the end (rear) of the existing elements), and array cannot handle data dynamically. Vector comes into the picture to solve the deficiency of array. A vector is a dynamic array that can change in size during runtime, hence vector  has dynamic/heap memory allocation and handles memory management. You need to add the header file of a vector to your program: #include<vector>

Vector operations
The following are the basic functions we can perform on a vector: add, access, change and remove elements

Vector Inbuilt functions 
The following are functions that can be used to perform several operations on a vector which are obtained from C++ STL (Standard Template Library)
front() returns the first element of the vector
back() returns the last element of the vector
size() returns the number of elements present in the vector
empty() returns true (1) if the vector is empty
begin() returns the pointer that points to the first element of the vector
end() returns the pointer that points to the last element of the vector
others are: clear(), capacity(), max_size(), resize(x), rbegin(), insert()
at() access an element from the specified index
push_back() inserts a single element into a vector at the end (rear)
pop_back() deletes a single element from a vector, while you use clear() function to delete all the elements in a vector.

Printing the elements of a vector
We can print the elements in a vector using simple for loop, ranged for loop, or iterator.

Terms use in vector

We don't need to indicate a vector size, the vector container will adjust itself dynamically whenever elements are added or deleted (and the C++ compiler will determine the vector size at runtime). Vector uses more memory (space) than array.

vector element: it represent the data in the array

square bracket: it does double function, it is used to wrap array size and is also used to wrap array index.

dot syntax: it allows us to use the vector name to access the inbuilt function.

//syntax

vectorName.push(myelement);    

//example

score.push_back(80);

parentheses ( ): it used to pass argument to the inbuilt function

//example

score.at(3);    //locate the index of the fourth element in the vector container


Types of vector

One dimensional vector (1D vector)

Two dimensional vector (2D vector) or nested vector or vector of vectors


                                fig1: vector container storing three cgpa elements

Syntax to declare 1D vector

vector<datatype> variableName;
example: vector<int>score;

Syntax to initialize a vector
vector<datatype> variableName = {value1, value2,...,valueN};
example: vector<int>score = {40, 21, 19, 15};

aliter: to initialize a vector
vector<datatype> variableName{value1, value2,...,valueN};
example: vector<int>score{40, 21, 19, 15};

PS: to access the index of a vector, we use ( ) attached to the in-built function. 

PRACTICE:
1. Write a runnable C++ program using 1D vector, declare a vector variable called colour; use empty() method to test whether the vector is empty or not (if empty print "the vector is empty", else print "the vector is not empty new elements can be added"); store six colours to the vector; add a print header to describe the output; print the vector element using iterator (hint: the keyword auto must be part of the parameter); use empty() method to test whether the vector is empty or not again.


SAMPLE CODE

Phase 1a:
Problem: A non-interactive program using vector to store and display four different integer elements using simple for loop

//CODE IMPLEMENTATION

#include<iostream>
#include <vector>

using namespace std;

int main(){

//declare and initialize vector with 4 elements
vector<int>score{40, 21, 19, 15};

//access the third element
cout<<"\nThe third element is:: "<<score.at(2)<<endl;

//output headline or title
cout<<"\nAll the elements in the vector are:: ";

//traverse the vector container (elements) using simple for loop
for(int xcount = 0; xcount<score.size(); xcount++){
cout<<score.at(xcount)<<" ";
}

return 0;

}

Phase 1b:
Problem: A non-interactive program using vector to store and display four different integer elements using range for loop

//CODE IMPLEMENTATION

#include<iostream>
#include <vector>

using namespace std;

int main(){

//declare and initialize vector with 4 elements
vector<int>score{40, 21, 19, 15};

//access the third element
cout<<"\nThe third element is:: "<<score.at(2)<<endl;

//output headline or title
cout<<"\nAll the elements in the vector are:: ";

//traverse the vector container (elements) using range for loop
for(int xcount : score){
cout<<xcount<<" ";
}

return 0;

}

Phase 1c:
Problem: A non-interactive program using vector to store and display four different integer elements using iterator

//CODE IMPLEMENTATION

#include<iostream>
#include <vector>

using namespace std;

int main(){

//declare and initialize vector with 4 elements
vector<int>score{40, 21, 19, 15};

//access the third element
cout<<"\nThe third element is:: "<<score.at(2)<<endl;

//output headline or title
cout<<"\nAll the elements in the vector are:: ";
//declare the iterator
vector<int>::iterator xit;

//traverse the vector container (elements) using iterator
for(xit = score.begin(); xit != score.end(); xit++){
cout<< *xit<<" "; //the asterisk(*) is the pointer to point to each element
}

return 0;
}

Phase 1d:
Problem: An interactive program using vector to ask user to insert element into a vector, store and display the elements using loop.

//CODE IMPLEMENTATION

#include<iostream>
#include <vector>

using namespace std;

int main(){

int x,y;
//declare and initialize vector with 4 elements
vector<int>score;

//user action
cout<<"Enter a value:: "<<endl;
cin>>x;
cout<<"Enter a value:: "<<endl;
cin>>y;
//add elements to vector
score.push_back(x);
score.push_back(y);
//output headline or title
cout<<"\nAll the elements in the vector are:: ";

//traverse the vector container (elements) using simple for loop
for(int xcount : score){
cout<<xcount<<" ";
}

return 0;

}

phase 2a:
Problem: Add string elements to a vector and print the elements

//CODE IMPLEMENTATION
#include<iostream>
#include <vector>

using namespace std;

int main(){

//declare the vector
vector<string>color;

//add three elements to the vector
color.push_back("blue");
color.push_back("red");
color.push_back("pink");
//output headline or title
cout<<"\nAll the elements in the vector are:: ";

//traverse the vector container (elements) using range for loop
       //we use string datatype for the range for loop and not int
for(string xcount : color){
cout<<xcount<<" ";
}
return 0;
}

phase 2b:
Problem: With reference to phase 2a problem, Delete string elements from the existing vector and print the elements. Print the original vector and also print the updated vector.

//CODE IMPLEMENTATION
#include<iostream>
#include <vector>

using namespace std;

int main(){

//declare the vector
vector<string>color;

//add three elements to the vector
color.push_back("blue");
color.push_back("red");
color.push_back("pink");
//output headline or title
cout<<"\nthe ORIGINIAL elements in the vector are:: ";

//traverse the vector container (elements) using range for loop
for(string xcount : color){
cout<<xcount<<" ";
}
//this function deletes the last element by default from the vector
color.pop_back();
//output headline or title
cout<<"\nthe UPDATED elements in the vector are:: ";

//traverse the vector container (elements) using range for loop
for(string xcount : color){
cout<<xcount<<" ";
}
return 0;
}


Practical Application of vector

Vector can be used when developing projects like object detector (such as face, face emotion, licence vehicle plate number etc.). 

We can use C++ as a programming language, Dev C++ as an IDE, an OpenCV (Computer Vision) to develop a simple face detector be it still image or moving image. During the course of the implementation, we will see how vector will be used to store the detected images indicated with rectangle(s) shape(s). However, we need to configure the Dev C++ IDE with the OpenCV compiler before compilation can take place successfully.

Vector is a good option for computer vision project that requires a grid representation in which the size of the grid is not known before hand. We need random access to the elements stored in the vector container. Vector is a good option in tree algorithm when you do not known the number of nodes before hand, so you just add and store the indices of their nodes.
C++ can be use in applications like desktop application development, OS design, Database design, Compiler design, Web browser and web server design, design any new programming language, embedded system etc.

Prerequisites for mastering Computer Vision with OpenCV

- Ability to use data structures for developing codes (that will represent geometric designs and image characteristics)
- Have good knowledge of C++, or any other programming language that can support computer vision
- Have strong mathematical knowledge of topics such as calculus, statistics

Comments

Popular posts from this blog

Basic Web Page - phase 1