Sie sind auf Seite 1von 34

Container Classes in C++.

By Kunal Kumar (CDAC Noida) Mca

Standard Template Library (STL)


The Standard Template Library defines powerful, templatebased, reusable components
- That implements common data structures and algorithms

STL extensively uses generic programming based on templates Divided into three components:
- Containers: data structures that store objects of any type - Iterators: used to manipulate container elements - Algorithms: searching, sorting and many others

Containers
Two types of containers
- Sequence containers: - linear data structures such as vectors and linked lists - Associative containers: - non-linear containers such as hash tables

Sequence and associative containers are also called firstclass containers

Iterators
Iterators are pointers to elements of first-class containers
Type const_iterator defines an iterator to a container element that cannot be modified Type iterator defines an iterator to a container element that can be modified

All first-class containers provide the members functions begin() and end()
return iterators pointing to the first and last element of the container

Iterators (cont.)
If the iterator it points to a particular element, then
- it++ (or ++it) points to the next element and - *it refers to the value of the element pointed to by it

The iterator resulting from end() can only be used to detect whether the iterator has reached the end of the container

Sequence Containers
All objects of the same type Store objects in strictly linear order Data organized as an array or linked list Container vector, deque and list is varying-length sequence of elements of type T.

Sequence Containers
STL provides three sequence containers
- vector: based on arrays - deque (double-ended queue): based on arrays - list: based on linked lists

Sequence Containers: vector


The implementation of a vector is based on arrays

Vectors allow direct access to any element via indexes


Insertion at the end is normally efficient. The vector simply grows Insertion and deletion in the middle is expensive An entire portion of the vector needs to be moved

Sequence Containers: vector (cont.)


When the vector capacity is reached then
- A larger vector is allocated, - The elements of the previous vector are copied and - The old vector is deallocated

To use vectors, we need to include the header <vector> Some functions of the class vector include
- size, capacity, insert

Example of using the class vector


#include <iostream> #include <vector> //vector class-template // display the content of v vector<int>::iterator it;

using namespace std;


int main() { vector<int> v;

for (it = v.begin(); it != v.end(); it++) { cout << *it << \n; } return 0;

// add integers at the end of the vector v.push_back( 2 ); v.push_back( 3 ); v.push_back( 4 );


cout << "\nThe size of v is: " << v.size() << "\nThe capacity of v is: " << v.capacity();

Vector

Sequence Containers: list


list is implemented using a doubly-linked list Insertions and deletions are very efficient at any point of the list But you have to have access to an element in the middle of the list first bidirectional iterators are used to traverse the container in both directions Include header <list> when using lists

Sequence Containers: deque


deque stands for double-ended queue

deque combines the benefits of vector and list


It provides indexed access using indexes (which is not possible using lists) It also provides efficient insertion and deletion in the front (which is not efficient using vectors) and the end

deque (cont.)
Additional storage for a deque is allocated using blocks of memory
- that are maintained as an array of pointers to those blocks

Same basic functions as vector, in addition to that


- deque supports push_front and pop_front for insertion and deletion at beginning of deque

Deque Example.
#include < deque.h > int main () { deque< int > d; d.push_back (4); // Add after end. d.push_back (9); d.push_back (16); d.push_front (1); // Insert at beginning. for (int i = 0; i < d.size (); i++) cout << "d[" << i << "] = " << d[i] << endl; cout << endl; d.pop_front (); // Erase first element. d[2] = 25; // Replace last element. for (i = 0; i < d.size (); i++) cout << "d[" << i << "] = " << d[i] << endl; return 0; }
output:

d[0] = 1 d[1] = 4 d[2] = 9 d[3] = 16 d[0] = 4 d[1] = 9 d[2] = 25

Associative Containers
Associative containers use keys to store and retrieve elements There are four types: multiset, set, multimap and map
- all associative containers maintain keys in sorted order
- set does not allow duplicate keys

- multiset and multimap allow duplicate keys

Associative Containers: multiset


Multisets are implemented using a red-black binary search tree for fast storage and retrieval of keys Multisets allow duplicate keys.

The ordering of the keys is determined by the STL comparator function object less<T> Keys sorted with less<T> must support comparison using the < operator

Example of using a multiset


#include <iostream> #include <set> using std; int main() { multiset< int, less< int > > ms; ms.insert( 10 ); // insert 10 ms.insert( 35 ); // insert 35 ms.insert( 10 ); // insert 10 again (allowed) multiset < int, less< int > >::iterator it; // creates an iterator it = ms.find(10);

if ( it != ms.end() ) // iterator not at end cout << \n10 was found";


return 0; }

Associative Containers: set


Sets are identical to multisets except that they do not allow duplicate keys To use sets, we need to include the header file <set>

Associative Containers: multimap


Multimaps associate keys to values They are implemented using red-black binary search trees for fast storage and retrieval of keys and values Insertion is done using objects of the class pair (with a key and value) Multimaps allow duplicate keys (many values can map to a single key)

The ordering of the keys is determined by the STL comparator function object less<T>

Example of using a multimap


#include <iostream> #include <map> using std;

typedef multimap< int, double, less< int > > mp_type; // creates a mutlimap type
int main() { mp_type mp;

// value_type is overloaded in multimap to create objects of the class pair


mp.insert( mp_type( 10, 14.5 ) ); // use iterator to go through mp for (mp_type::iterator it = mp.begin(); it != mp.end(); it ++) cout << it->first << '\t' << it->second << '\n'; return 0; }

Associative Containers: map


They are implemented using red-black binary search trees just like multimaps Unlike multimaps, they allow storage and retrieval of unique key/value pairs. They do not allow duplicates of keys

Example of using a map


The following code fragment shows how to use indexes with an object of the class map
map<int, double, less<int>> map_obj;

Thank you..

Das könnte Ihnen auch gefallen