Saturday, June 30, 2012

Vector Operations in C++ -- Part 3

In the last post, we implemented the searching procedure followed in the Employee Register. Now we will discuss the CRUD operations on the vector with Employee class.

To go to the previous two posts,
Vector Operations in C++ -- Part 1 and
Vector Operations in C++ -- Part 2

For adding an employee, we need to first check if another employee of the same employee ID exists in the vector. If it exists, we should give the relevant message to the user. Since we know that adding or deleting the employees at the ends is done very quickly, we have used the function "push_back" to add the new employee to the end.



For displaying the employee details, we must first check if the vector is empty. If it is empty, we need not traverse through the vector.



For deleting and updating an employee details, we need to check if the employee with the given ID exists. If it does not exist, the relevant message must be displayed.



Till now we have done all the operations required for manipulating or displaying the employee data. But a good user interface which is easily understandable by the user is equally important. Thus, we will have a menu with all the above discussed operations. To take numerical and string input from the user, we use two member functions in the class "EmployeeDisplay". Validation to the complete extent is not done for numerical and string inputs.

Compiling and running the case study:

Compiling:
g++ -o EmployeeRegister main.cpp Display.cpp Employee.cpp EmployeeRegister.cpp
Running the executable:
./EmployeeRegister

Sample Output for an add operation in the following menu is given below.



I have uploaded the entire project at the following link: Vector Operations Case Study. You can download the "Employee Register.zip" from this link.

Vector Operations in C++ -- Part 2

Have some information about vectors and want to implement the vector operations?? You have come to the correct place.
In the last post, we have discussed about the operations in vectors and the functions that can be used in vectors. If you do not know about vectors in C++, then refer this page. In this post, we will see the practical implementation of vectors by using a case study.

We are building an employee register in which an employee has two attributes -- a unique Employee ID and the Employee Name. If you want more attributes such as Date of Birth, Work experience, Designations etc., you can add them. But I thought of keeping it simple. So, I have included only two attributes for an employee. I am not linking this case study to any database programming. But I am sure this case study would be very useful in many of the applications based on CRUD and can be used in applications which include database programming. The following actions are done in this case study:
  1. Add a new employee
  2. Display the existing employees
  3. Delete an employee details
  4. Update an employee details
We will have a total of three classes:
  1. Employee class  -- To define the basic attributes of Employee
  2. EmployeeRegister class  -- To do the vector operations on Employee class
  3. EmployeeDisplay class --  Created for User Interface programming

To ensure data abstraction, the header files are separated from the actual implementation. In the header files, the declarations of the variables, classes and methods will be present, while the implementation is covered in the corresponding CPP source files.

First let us see what Employee class has. The following piece of code is Employee.h which declares the variables and the member functions of Employee class.



The implementation of the Employee class are given below. The getters and setters are defined so as to access the member variables in the Employee class.



The implementation of EmployeeRegister class has the following operations:
  1. Adding, Deleting and Updating an employee.
  2. Displaying the current employee details.
  3. Searching for an element in the vector.
  4. Checking whether the vector for Employee class is empty.
We can check if the vector is empty by using the boolean function "empty()" used in vector STL.



Searching is an important operation and will be used by almost all other functions. Imagine you have large number of employee records and you need to search for a record which is the last in the vector. By using linear search, the search process will consume more time. For binary search to be implemented, we need to have the elements in the vector in an increasing order which is not the case here. They can be any possible order. Thus, due to all these factors, the STL algorithm "find" is used to find the position of an element in the vector. For this function to be used, we need to create another vector of integers "emp_id_records". This vector should have all the employee ID's exactly in the same order as that of the emp_records. So, if we can get the position of an element in one vector, we can the index of the other too. 



Friday, June 29, 2012

Vector Operations in C++ -- Part 1

At the end of this series of posts, you will understand the working of vectors, the functions used in vectors. In addition to this, you will be able to do the basic CRUD operations on the vectors.

Vector is a type of sequence container in C++ and is implemented like a dynamic array. Vectors are very similar to arrays except that the arrays have a constant size while vectors can be extended or contracted as needed. However, the size of vector can also be fixed and resized. Once the storage of a vector is exhausted, it grows automatically in size. The storage space they have been allocated can be either equal or greater than the actual size. The extra amount of storage allocated is not used, but is reserved for the vector to be used in case it grows. Thus, the vector does not have to re-allocate storage on each occasion it grows or a new element is inserted.

Similar to arrays, we can access a given element in a vector easily with the index. The elements of vectors are stored in contiguous memory locations just like in the case of arrays. We can iterate over a vector by using its index or by using iterators used in vectors.

Adding or removing the elements from the vector at the ends is done very well as far as the time complexity is concerned. However, the performance of insertion/deletion at positions other than the ends is worse than that of the other sequence containers in C++ (deques and lists).

Constructors for the vector class are as follows:


Constructor Name Syntax Example Description
Default Constructor vector<T> vector_name; vector<int> a; Creates a vector with no elements
Repetitive sequence Constructor vector<T> vector_name(No. of Elements to be repeated, Value to be assigned); vector<int> b(4,100); Creates a vector of 4 elements with value 100.
Iterator constructor vector<T> vector_name (Beginning of another vector, End of another vector); vector<int> c (a.begin(),a.end()); Creates a vector with elements from vector “a”. Can also create a vector by copying only a part of the vector “a”.
Copy constructor vector<T> vector_name(another_vector); vector<int> d(c); Creates a vector by copying the vector “c” into vector “d”.


Accessing an element in a vector:
An element in a Vector can be accessed in two ways:
1. vector_name[index]     (or)
2. vector_name.at(index)

Iterating over a vector using an iterator:
We can iterate over a vector using a normal for loop with the initialization variable from 0 to the vector's maximum size (vector.size( )). But here is another method to explore all the elements in a vector:

vector<int> vector_name;
vector::iterator it;
for(it=vector_name.begin();it < vector_name.end(); it++)
      cout<<*it;

This piece of code will print all the elements one by one in the vector. Similar to iterator, there is also reverse iterator, which iterates over the vector from right to left.

Functions in Vector:

Function Syntax Description
push_back vector.push_back(element) Inserts the element at the end of the vector
pop_back vector.pop_back() Deletes the last element in the vector and returns the same
erase vector.erase(position) Deletes the element from the vector in the specified position/index
insert vector.insert(element,position) Inserts an element (1st argument) into the position (2nd argument)

In the next post, we will apply this concept of vectors in our case study, which involves the basic CRUD operations.
Related Posts Plugin for WordPress, Blogger...