You are on page 1of 13

class template std:: <stack> LIFO stack

stack

Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from the end of the container. stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack. The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it supports the following operations:

back() push_back() pop_back()

Therefore, the standard container class templates vector, deque and list can be used. By default, if no container class is specified for a particular stack class, the standard container class template deque is used. In their implementation in the C++ Standard Template Library, stacks take two template parameters:

template < class T, class Container = deque<T> > class stack;


Where the template parameters have the following meanings:

T: Type of the elements. Container: Type of the underlying container object used to store and access the
elements.

In the reference for the stack member functions, these same names are assumed for the template parameters.

Member functions
(constructor) Construct stack (public member function) empty Test whether container is empty (public member function) size Return size (public member function) top Access next element (public member function)

push Add element (public member function) pop Remove element (public member function)

class template std:: <queue> FIFO queue

queue

queues are a type of container adaptor, specifically designed to operate in a FIFO context (firstin first-out), where elements are inserted into one end of the container and extracted from the other. queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front". The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:

front() back() push_back() pop_front()

Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used. In their implementation in the C++ Standard Template Library, queues take two template parameters:

template < class T, class Container = deque<T> > class queue;


Where the template parameters have the following meanings:

T: Type of the elements. Container: Type of the underlying container object used to store and access the
elements.

In the reference for the queue member functions, these same names are assumed for the template parameters.

Member functions
(constructor) Construct queue (public member function) empty Test whether container is empty (public member function) size Return size (public member function) front Access next element (public member function) back Access last element (public member function) push Insert element (public member function) pop Delete next element (public member function)

class template std:: <vector> Vector

vector

Vectors are a kind of sequence container. As such, their elements are ordered following a strict linear sequence. Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements. But unlike regular arrays, storage in vectors is handled automatically, allowing it to be expanded and contracted as needed. Vectors are good at:

Accessing individual elements by their position index (constant time). Iterating over the elements in any order (linear time). Add and remove elements from its end (constant amortized time).

Compared to arrays, they provide almost the same performance for these tasks, plus they have the ability to be easily resized. Although, they usually consume more memory than arrays when their capacity is handled automatically (this is in order to accommodate extra storage space for future growth). Compared to the other base standard sequence containers (deques and lists), vectors are generally the most efficient in time for accessing elements and to add or remove elements from the end of the sequence. For operations that involve inserting or removing elements at positions other than the end, they perform worse thandeques and lists, and have less consistent iterators and references than lists. Internally, vectors -like most containers- have a size, which represents the amount of elements

contained in the vector. But vectors, also have a capacity, which determines the amount of storage space they have allocated, and which 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 the case it grows. This way, the vector does not have to reallocate storage on each occasion it grows, but only when this extra space is exhausted and a new element is inserted (which should only happen in logarithmic frequence in relation with its size). Reallocations may be a costly operation in terms of performance, since they generally involve the entire storage space used by the vector to be copied to a new location. You can use member function vector::reserve to indicate beforehand a capacity for the vector. This can help optimize storage space and reduce the number of reallocations when many enlargements are planned. In their implementation in the C++ Standard Template Library vectors take two template parameters:

template < class T, class Allocator = allocator<T> > class vector;


Where the template parameters have the following meanings:

T: Type of the elements. Allocator: Type of the allocator object used to define the storage allocation model. By default, theallocator class template for type T is used, which defines the simplest
memory allocation model and is value-independent.

In the reference for the vector member functions, these same names are assumed for the template parameters.

Member functions
(constructor) Construct vector (public member function) (destructor) Vector destructor (public member function) operator= Copy vector content (public member function) Iterators: begin Return iterator to beginning (public member function) end Return iterator to end (public member function) rbegin Return reverse iterator to reverse beginning (public member function) rend Return reverse iterator to reverse end (public member function) Capacity: size Return size (public member function)

max_size Return maximum size (public member function) resize Change size (public member function) capacity Return size of allocated storage capacity (public member function) empty Test whether vector is empty (public member function) reserve Request a change in capacity (public member function) Element access: operator[] Access at Access front Access back Access Modifiers: assign Assign vector content (public member function) push_back Add element at the end (public member function) pop_back Delete last element (public member function) insert Insert elements (public member function) erase Erase elements (public member function) swap Swap content (public member function) clear Clear content (public member function) Allocator: get_allocator Get allocator (public member function)

element (public member function) element (public member function) first element (public member function) last element (public member function)

Member types
of template <class T, class Allocator=allocator<T> > class vector; member type definition

reference const_reference

Allocator::reference Allocator::const_reference

iterator Random access iterator const_iterator Constant random access iterator size_type Unsigned integral type (usually same as size_t) difference_type Signed integral type (usually same as ptrdiff_t) value_type T allocator_type Allocator pointer Allocator::pointer const_pointer Allocator::const_pointer reverse_iterator reverse_iterator<iterator> const_reverse_iterator reverse_iterator<const_iterator>

Vector specialization:

vector<bool>

The vector class template has a special template specialization for the bool type. This specialization is provided to optimize for space allocation: In this template specialization, each element occupies only one bit (which is eight times less than the smallest type in C++: char). The references to elements of a bool vector returned by the vector members are not references to bool objects, but a special member type which is a reference to a single bit, defined inside the vector<bool> class specialization as:

1 class vector<bool>::reference { 2 friend class vector; 3 reference(); // no public constructor 4 public: 5 ~reference(); 6 operator bool () const; // convert to bool 7 reference& operator= ( const bool x ); // assign from bool 8 reference& operator= ( const reference& x ); // assign from bit 9 void flip(); // flip bit value. 10 }

For a similar container class to contain bits, but with a fixed size, see bitset.

class template std:: <list> List

list

Lists are a kind of sequence container. As such, their elements are ordered following a linear sequence. List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the element preceding it and a link to the element following it.

This provides the following advantages to list containers:

Efficient insertion and removal of elements before any other specific element in the container (constant time). Efficient moving elements and block of elements within the container or even between different containers (constant time). Iterating over the elements in forward or reverse order (linear time).

Compared to other base standard sequence containers (vectors and deques), lists perform generally better in inserting, extracting and moving elements in any position within the container for which we already have an iterator, and therefore also in algorithms that make intensive use of these, like sorting algorithms. The main drawback of lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a list one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements). Storage is handled automatically by the list object, allowing it to be expanded and contracted automatically as needed. In their implementation in the C++ Standard Template Library lists take two template parameters:

template < class T, class Allocator = allocator<T> > class list;


Where the template parameters have the following meanings:

T: Type of the elements. Allocator: Type of the allocator object used to define the storage allocation model. By default, theallocator class template for type T is used, which defines the simplest
memory allocation model and is value-independent.

In the reference for the list member functions, these same names are assumed for the template parameters.

Member functions
(constructor) Construct list (public member function) (destructor) List destructor (public member function) operator= Copy container content (public member function) Iterators: begin

Return iterator to beginning (public member function) end Return iterator to end (public member function) rbegin Return reverse iterator to reverse beginning (public member function) rend Return reverse iterator to reverse end (public member function) Capacity: empty Test whether container is empty (public member function) size Return size (public member function) max_size Return maximum size (public member function) resize Change size (public member function) Element access: front Access first element (public member function) back Access last element (public member function) Modifiers: assign Assign new content to container (public member function) push_front Insert element at beginning (public member function) pop_front Delete first element (public member function) push_back Add element at the end (public member function) pop_back Delete last element (public member function) insert Insert elements (public member function) erase Erase elements (public member function) swap Swap content (public member function) clear Clear content (public member function) Operations: splice Move elements from list to list (public member function) remove

Remove elements with specific value (public member function) remove_if Remove elements fulfilling condition (public member function template) unique Remove duplicate values (member function) merge Merge sorted lists (public member function) sort Sort elements in container (public member function) reverse Reverse the order of elements (public member function) Allocator: get_allocator Get allocator (public member function)

Member types
of template <class T, class Allocator=allocator<T> > class list; member type definition

reference Allocator::reference const_reference Allocator::const_reference iterator Bidirectional iterator const_iterator Constant bidirectional iterator size_type Unsigned integral type (usually same as size_t) difference_type Signed integral type (usually same as ptrdiff_t) value_type T allocator_type Allocator pointer Allocator::pointer const_pointer Allocator::const_pointer reverse_iterator reverse_iterator<iterator> const_reverse_iterator reverse_iterator<const_iterator>

class template std:: <queue> Priority queue

priority_queue

Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. This context is similar to a heap where only the max heap element can be retrieved (the one at the top in thepriority queue) and elements can be inserted indefinitely. Priority queues are implemented as container adaptors, which are classes that use an

encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue. The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it must be accessible through random access iterators and it must support the following operations:

front() push_back() pop_back()

Therefore, the standard container class templates vector and deque can be used. By default, if no container class is specified for a particular priority_queue class, the standard container class template vector is used. Support for random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by calling the algorithms make_heap, push_heap and pop_heap when appropriate. In their implementation in the C++ Standard Template Library, priority queues take three template parameters:

1 template < class T, class Container = vector<T>, 2 class Compare = less<typename Container::value_type> > class priority_queue;
Where the template parameters have the following meanings:

T: Type of the elements. Container: Type of the underlying container object used to store and access the
elements. Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less<T>, which returns the same as applying the less-than operator (a<b). The priority_queue object uses this expression when an element is inserted or removed from it (using pushor pop, respectively) to grant that the element popped is always the greatest in the priority queue.

In the reference for the priority_queue member functions, these same names (T, Container and Compare) are assumed for the template parameters.

Member functions
(constructor) Construct priority queue (public member function)

empty Test whether container is empty (public member function) size Return size (public member function) top Access top element (public member function) push Insert element (public member function) pop Remove top element (public member function)

_________________ALGORITHMS________________
function template std:: <algorithm>

binary_search

template <class ForwardIterator, class T> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
Test if value exists in sorted array Returns true if an element in the range [first,last) is equivalent to value, and false otherwise. The comparison is performed using either operator< for the first version, or comp for the second: A value, a, is considered equivalent to another, b, when (!(a<b) && !(b<a)) or (!comp(a,b) && !comp(b,a)) For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp). The behavior of this function template is equivalent to:

1 template <class ForwardIterator, class T> 2 bool binary_search ( ForwardIterator first, ForwardIterator last, 3 const T& value ) 4{ 5 first = lower_bound(first,last,value); 6 return (first!=last && !(value<*first)); }

Parameters
first, last Forward iterators to the initial and final positions of the sequence to be searched. The range used is[first,last), which contains all the elements between first and last, including the element pointed by firstbut not the element pointed by last. value Element value to search for. comp Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.

Return value
true if an element in value is found, and false otherwise.

Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // binary_search example #include <iostream> #include <algorithm> #include <vector> using namespace std; bool myfunction (int i,int j) { return (i<j); } int main () { int myints[] = {1,2,3,4,5,4,3,2,1}; vector<int> v(myints,myints+9); 4 3 2 1 // using default comparison: sort (v.begin(), v.end()); cout << "looking for a 3... "; if (binary_search (v.begin(), v.end(), 3)) cout << "found!\n"; else cout << "not found.\n"; // using myfunction as comp: sort (v.begin(), v.end(), myfunction); cout << "looking for a 6... "; if (binary_search (v.begin(), v.end(), 6, myfunction)) cout << "found!\n"; else cout << "not found.\n";

// 1 2 3 4 5

27 28 }

return 0;

Output:

looking for a 3... found! looking for a 6... not found.

Complexity
At most, logarithmic number of comparisons and linear number of steps in the length of [first,last) +2. If used with random-access iterators, number of steps is reduced to logarithmic.

quick_sort
void qsort( int list[], int left ,int size){ int i, j, pivot; i=left; j= size; pivot = list[left]; do{ while(list[i] < pivot)i++;while(pivot < list[j])j--; if(i<=j){ int aux = list[i]; list[i] = list[j] ; list[j] = aux; i++; j--;} }while(i<=j); if(left< j)qsort(list,left,j); if(i<size)qsort(list,i,size); }

You might also like