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.

Sunday, June 5, 2011

UNIX Command History

All the commands typed by the user are stored in a file named ".bash_history" (for bash) in the Home directory of the User. It is a hidden file as its name is preceded by a dot (.) and therefore you can view this  in the File manager only when you hit the Ctrl-H combination. To view this file in "bash" the option -a has to be used with the ls command, i.e., to view this file in the command line interface, the command ls -a has to be used.

This file contains the list of all the commands typed since the system formatting. The operations on the command history such as retrieval or reuse of the commands depends on the contents of this file.

Type history at the command prompt and you will get the last 500 commands used. If you only want to look at the last 10 commands, you could type the command history 10. Commands are remembered so you can reuse them.



You can reuse any command in the history list by typing its number (as shown by the history command), preceded by an exclamation mark (!). If you want a view a file listing of the Documents folder, and ls Documents is command #31 in the list, you could type !31.




Say you’ve just typed a long file-name copy (cp) command, and want to use it again. Typing !cp will find the last line in your history list that begins with cp and reuse it.



Typing two bangs (!!) will cause the last command you typed to be reused. Finally, typing !? and then part of a recently-typed command will cause bash to auto-complete using the nearest match it can find in the history.


Related Posts Plugin for WordPress, Blogger...