Tuesday, November 29, 2011

associative container example

Using associative container:
The iterator loop in the previous example could be used for any  container. You only have to adjust the iterator type. Now you can print elements of associative containers. The following are some examples of the use of associative containers.
  1. Examples of Using Sets and Multisets
  2. Examples of Using Maps and Multimaps
  3. Maps as Associative Arrays
See Also:

Monday, November 28, 2011

stl map

Examples of Using Maps and Multimaps / stl map c++

The elements of maps and multimaps are key/value pairs. Thus, the  declaration, the insertion, and the access to elements are a bit different. Here is an example of using a multimap:

Example:
 
The program may have the following output:

this is a multimap of tagged strings However, because "this" and  "is" have the same key, their order might be the other way around.

When you compare this example with the set example presented earlier, you can see the following two differences:
  1. The elements are key/value pairs, so you must create such a pair to insert it into the collection. The auxiliary function make_pair() is provided for this purpose.  
  2. The iterators refer to key/value pairs. Therefore, you can't just print them as a whole. Instead, you must access the members of the pair structure, which are called first and second. Thus, the expression "pos->second" yields the second part of the key/value pair, which is the value of the multimap element. As with ordinary pointers, the expression is defined as an abbreviation for
Note: In some older environments, operator -> might not work yet  or iterators. In this case, you must use the second version. (*pos) .second 

Similarly, the expression

pos->first

yields the first part of the key/value pair, which is the key of  the multimap element.

Usage:
Multimaps can also be used as dictionaries.

See Also:

Sunday, November 27, 2011

map associative array


stl multimap example


stl multiset example


stl set example

using set in c++

The first example shows how to insert elements into a set and to use iterators to print them

Example:

As usual, the include directive

#include <set>

defines all necessary types and operations of sets.

The type of the container is used in several places, so first a shorter type name gets defined:

typedef set<int> IntSet;

This statement defines type IntSet as a set for elements of type  int. This type uses the default sorting criterion, which sorts  the elements by using operator <. This means the elements are sorted in ascending order. To sort in descending order or use a  completely different sorting criterion, you can pass it as a  second template parameter. For example, the following statement defines a set type that sorts the elements in descending order

Note that you have to put a space between the two ">" characters.  >>" would be parsed as shift operator, which would result in a syntax error.

typedef set<int, greater<int> > IntSet;

greater<> is a predefined function object. For a sorting criterion that uses only a part of the data of an object (such as he ID).

All associative containers provide an insert() member function to  nsert a new element:

coll.insert(3);
coll.insert(1);
...

The new element receives the correct position automatically  according to the sorting criterion. You can't use the push_back() or push_front() functions provided for sequence containers. They make no sense here because you can't specify the position of the  new element.

After all values are inserted in any order, the state of the  container is as shown in Figure below. The elements are sorted  into the internal tree structure of the container so that the  value of the left child of an element is always less (with  respect to the actual sorting criterion) and the value of the  right child of an element is always greater. Duplicates are not  allowed in a set, so the container contains the value 1 only  once.

Figure . A Set that Has Six Elements

To print the elements of the container, you use the same loop as  in the previous list example. An iterator iterates over all  elements and prints them:

IntSet::const_iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
   cout << *pos << ' ';
}

Again, because the iterator is defined by the container, it does  the right thing, even if the internal structure of the container  is more complicated. For example, if the iterator refers to the  third element, operator ++ moves to the fourth element at the  top. After the next call of operator ++ the iterator refers to the fifth element at the bottom (Figure below).

Figure. Iterator pos Iterating over Elements of a Set

The output of the program is as follows:

1 2 3 4 5 6

If you want to use a multiset rather than a set, you need only  change the type of the container (the header file remains the  same):

typedef multiset<int> IntSet;

A multiset allows duplicates, so it would contain two elements  that have value 1. Thus, the output of the program would change  to the following:

1 1 2 3 4 5 6

See Also:

Saturday, November 26, 2011

stl set example


Wednesday, November 23, 2011

shell script to get linux version

############################################################
#  Author      : Saurabh Gupta                             #
#  Created     : 9:30 AM 23/11/2011                        #
#  shell script to get Linux Version Running               #
#  get linux version  shell script                         #
############################################################

#!/bin/bash
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "XXXXXXX          Retrieving Linux Version Information       XXXXXXX"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

CHECK_OS_VERSION=`uname -r | sed -e 's/\./ /g' | awk '{print $4}'`

echo "Your Linux OS Version is = $CHECK_OS_VERSION"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
############################################################
#
# OUTPUT
#
############################################################
[sgupta@ivog-ks-200 scripts]$ . ./getOSVersion.sh
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXX          Retrieving Linux Version Information       XXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Your Linux OS Version is = el5
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[sgupta@ivog-ks-200 scripts]$
 
 #

See Also:

Sunday, November 20, 2011

iterator example



See Also:

iterator stl

iterator c++

An iterator is an object that can "iterate" (navigate) over  elements. These elements may be all or part of the elements of a  STL container. An iterator represents a certain position in a  container. The following fundamental operations define the behavior of an iterator:
  • Operator * : Returns the element of the actual position. If the elements have members, you can use operator -> to access those members directly from the iterator. ( In some older environments, operator -> might not work yet for iterators.) 
  • Operator ++ : Lets the iterator step forward to the next element. Most iterators also allow stepping backward by using operator --. 
  • Operators == and != : Return whether two iterators represent the same position. 
  • Operator = Assigns an iterator (the position of the element to which it refers).
These operations are exactly the interface of ordinary pointers  in C and C++ when they are used to iterate over the elements of an array. The difference is that iterators may be smart pointers — pointers that iterate over more complicated data structures of  containers. The internal behavior of iterators depends on the data structure over which they iterate. Hence, each container type supplies its own kind of iterator. In fact, each container  class defines its iterator type as a nested class. As a result, iterators share the same interface but have different types. This leads directly to the concept of generic programming: Operations  use the same interface but different types, so you can use templates to formulate generic operations that work with  arbitrary types that satisfy the interface.

All container classes provide the same basic member functions  that enable them to use iterators to navigate over their  elements. The most important of these functions are as follows:  
  • begin() : Returns an iterator that represents the beginning of the elements in the container. The beginning is the position of the first element (if any). 
  • end() : Returns an iterator that represents the end of the elements in the container. The end is the position behind the last element. Such an iterator is also called a past-the-end iterator.
Thus, begin() and end() define a half-open range that includes  the first element but excludes the last (Figure 5.3). A half-open range has two advantages:

Figure. begin() and end() for Containers
  1. You have a simple end criterion for loops that iterate over the elements: They simply continue as long as end() is not reached. 
  2. It avoids special handling for empty ranges. For empty ranges, begin() is equal to end().
Here is an example demonstrating the use of iterators. It prints  all elements of a list container (it is the promised enhanced  version of the first list example).

Example:

After the list is created and filled with the characters 'a' through 'z', all elements are printed within a for loop:

list<char>::const_iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
   cout << *pos << ' ';
}

The iterator pos is declared just before the loop. Its type is the iterator type for constant element access of its container class:

list<char>::const_iterator pos;

Every container defines two iterator types:
  1. container::iterator - is provided to iterate over elements in read/write mode. 
  2. container: : const_iterator - is provided to iterate over elements in read-only mode.
For example, in class list the definitions might look like the following:

namespace std {
   template <class T>
   class list {
      public:
         typedef ... iterator;
         typedef ... const_iterator;
         ...
   };
}

The exact type of iterator and const_iterator is implementation  defined.

Inside the for loop, the iterator pos first gets initialized with  he position of the first element:

pos = coll.begin()

The loop continues as long as pos has not reached the end of the  container elements:

pos != coll.end()

Here, pos is compared with the past-the-end iterator. While the  loop runs the increment operator, ++pos navigates the iterator pos to the next element.

All in all, pos iterates from the first element, element- by-element, until it reaches the end (Figure below). If the  container has no elements, the loop does not run because  coll.begin() would equal coll.end().

Figure . Iterator pos Iterating Over Elements of a List

In the body of the loop, the expression *pos represents the  actual element. In this example, it is written followed by a  space character. You can't modify the elements because a const_iterator is used. Thus, from the iterator's point of view  the elements are constant.

However, if you use a nonconstant iterator and the type of the  elements is nonconstant, you can change the values. For example:  
//make all characters in the list uppercase
list<char>::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
   *pos = toupper(*pos);
}

Note that the preincrement operator (prefix ++) is used here.  This is because it might have better performance than the  postincrement operator. The latter involves a temporary object  because it must return the old position of the iterator. For this  eason, it generally is best to prefer ++pos over pos++. Thus, you should avoid the following version:

for (pos = coll.begin(); pos != coll.end(); pos++) {
   ^^^^^ // OK, but slower
  ...
}

For this reason, I recommend using the preincrement and pre-decrement operators in general.

See Also:

Saturday, November 19, 2011

container adaptors stl

container adaptors c++

In addition to the fundamental container classes, the C++ standard library provides special predefined container adapters that meet special needs. These are implemented by using the fundamental containers classes. The predefined container adapters are as follows:
  • Stacks : The name says it all. A stack is a container that manages its elements by the LIFO (lastin- first-out) policy. 
  • Queues : A queue is a container that manages its elements by the FIFO (first-in-first-out) policy. That is, it is an ordinary buffer. 
  • Priority Queues : A priority queue is a container in which the elements may have different priorities. The priority is based on a sorting criterion that the programmer may provide (by default, operator < is used). A priority queue is, in effect, a buffer in which the next element is always the element that has the highest priority inside the queue. If more than one element has the highest priority, the order of these elements is undefined.
Container adapters are historically part of the STL. However, from  programmer's view point, they are just special containers that use the general framework of the containers, iterators, and algorithms provided by the STL.

See Also: