Material de Apoyo Concurso de Porgramadores
-
Upload
alex-madrigal -
Category
Documents
-
view
221 -
download
0
Transcript of Material de Apoyo Concurso de Porgramadores
-
7/29/2019 Material de Apoyo Concurso de Porgramadores
1/13
class template
std::stack
LIFO 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 encapsulatedobject of a specific container class as its underlying container, providing a specific set of memberfunctions to access its elements. Elements arepushed/poppedfrom the "back"of the specificcontainer, which is known as the top of the stack.
The underlying container may be any of the standard container class templates or some otherspecifically designed container class. The only requirement is that it supports the followingoperations:
back() push_back() pop_back()
Therefore, the standard container class templatesvector,dequeandlistcan be used. By default,if no container class is specified for a particular stack class, the standard container class
templatedequeis used.
In their implementation in the C++ Standard Template Library, stacks take two templateparameters:template < class T, class Container = deque > 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 thetemplate 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)
http://www.cplusplus.com/vectorhttp://www.cplusplus.com/vectorhttp://www.cplusplus.com/vectorhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/reference/stl/stack/stack/http://www.cplusplus.com/reference/stl/stack/stack/http://www.cplusplus.com/reference/stl/stack/empty/http://www.cplusplus.com/reference/stl/stack/empty/http://www.cplusplus.com/reference/stl/stack/size/http://www.cplusplus.com/reference/stl/stack/size/http://www.cplusplus.com/reference/stl/stack/top/http://www.cplusplus.com/reference/stl/stack/top/http://www.cplusplus.com/reference/stl/stack/top/http://www.cplusplus.com/reference/stl/stack/size/http://www.cplusplus.com/reference/stl/stack/empty/http://www.cplusplus.com/reference/stl/stack/stack/http://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/vector -
7/29/2019 Material de Apoyo Concurso de Porgramadores
2/13
push
Add element (public member function)
pop
Remove element (public member function)
class template
std::queue
FIFO queue
queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from theother.
queues are implemented as containers adaptors, which are classes that use an encapsulatedobject of a specific container class as its underlying container, providing a specific set of memberfunctions to access its elements. Elements arepushedinto the "back"of the specific containerandpoppedfrom 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 followingoperations:
front() back() push_back() pop_front()
Therefore, the standard container class templatesdequeandlistcan be used. By default, if nocontainer class is specified for a particular queue class, the standard container class
templatedequeis used.
In their implementation in the C++ Standard Template Library, queues take two templateparameters:template < class T, class Container = deque > 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 thetemplate parameters.
http://www.cplusplus.com/reference/stl/stack/push/http://www.cplusplus.com/reference/stl/stack/push/http://www.cplusplus.com/reference/stl/stack/pop/http://www.cplusplus.com/reference/stl/stack/pop/http://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/reference/stl/stack/pop/http://www.cplusplus.com/reference/stl/stack/push/ -
7/29/2019 Material de Apoyo Concurso de Porgramadores
3/13
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
Vectors are a kind of sequence container. As such, their elements are ordered following a strictlinear sequence.
Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containershave 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 expandedand 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 havethe 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 forfuture growth).
Compared to the other base standard sequence containers (deques andlists), vectors aregenerally the most efficient in time for accessing elements and to add or remove elements fromthe end of the sequence. For operations that involve inserting or removing elements at positionsother than the end, they perform worse thandeques andlists, and have less consistent iteratorsand references thanlists.
Internally, vectors -like most containers- have asize, which represents the amount of elements
http://www.cplusplus.com/reference/stl/queue/queue/http://www.cplusplus.com/reference/stl/queue/queue/http://www.cplusplus.com/reference/stl/queue/empty/http://www.cplusplus.com/reference/stl/queue/empty/http://www.cplusplus.com/reference/stl/queue/size/http://www.cplusplus.com/reference/stl/queue/size/http://www.cplusplus.com/reference/stl/queue/front/http://www.cplusplus.com/reference/stl/queue/front/http://www.cplusplus.com/reference/stl/queue/back/http://www.cplusplus.com/reference/stl/queue/back/http://www.cplusplus.com/reference/stl/queue/push/http://www.cplusplus.com/reference/stl/queue/push/http://www.cplusplus.com/reference/stl/queue/pop/http://www.cplusplus.com/reference/stl/queue/pop/http://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/listhttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/listhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/reference/stl/queue/pop/http://www.cplusplus.com/reference/stl/queue/push/http://www.cplusplus.com/reference/stl/queue/back/http://www.cplusplus.com/reference/stl/queue/front/http://www.cplusplus.com/reference/stl/queue/size/http://www.cplusplus.com/reference/stl/queue/empty/http://www.cplusplus.com/reference/stl/queue/queue/ -
7/29/2019 Material de Apoyo Concurso de Porgramadores
4/13
contained in the vector. But vectors, also have acapacity, which determines the amount ofstorage space they have allocated, and which can be either equal or greater than the actualsize.The extra amount of storage allocated is not used, but is reserved for the vector to be used inthe case it grows. This way, the vector does not have to reallocate storage on each occasion itgrows, but only when this extra space is exhausted and a new element is inserted (which shouldonly happen in logarithmic frequence in relation with its size).
Reallocations may be a costly operation in terms of performance, since they generally involvethe entire storage space used by the vector to be copied to a new location. You can use memberfunctionvector::reserveto indicate beforehand acapacityfor the vector. This can help optimizestorage space and reduce the number of reallocations when many enlargements are planned.
In their implementation in the C++ Standard Template Library vectors take two templateparameters:template < class T, class Allocator = allocator > 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 simplestmemory allocation model and is value-independent.
In the reference for the vector member functions, these same names are assumed for thetemplate 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)
http://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::reservehttp://www.cplusplus.com/vector::reservehttp://www.cplusplus.com/vector::reservehttp://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/reference/stl/vector/vector/http://www.cplusplus.com/reference/stl/vector/vector/http://www.cplusplus.com/reference/stl/vector/~vector/http://www.cplusplus.com/reference/stl/vector/~vector/http://www.cplusplus.com/reference/stl/vector/operator=/http://www.cplusplus.com/reference/stl/vector/operator=/http://www.cplusplus.com/reference/stl/vector/begin/http://www.cplusplus.com/reference/stl/vector/begin/http://www.cplusplus.com/reference/stl/vector/end/http://www.cplusplus.com/reference/stl/vector/end/http://www.cplusplus.com/reference/stl/vector/rbegin/http://www.cplusplus.com/reference/stl/vector/rbegin/http://www.cplusplus.com/reference/stl/vector/rend/http://www.cplusplus.com/reference/stl/vector/rend/http://www.cplusplus.com/reference/stl/vector/size/http://www.cplusplus.com/reference/stl/vector/size/http://www.cplusplus.com/reference/stl/vector/size/http://www.cplusplus.com/reference/stl/vector/rend/http://www.cplusplus.com/reference/stl/vector/rbegin/http://www.cplusplus.com/reference/stl/vector/end/http://www.cplusplus.com/reference/stl/vector/begin/http://www.cplusplus.com/reference/stl/vector/operator=/http://www.cplusplus.com/reference/stl/vector/~vector/http://www.cplusplus.com/reference/stl/vector/vector/http://www.cplusplus.com/vector::capacityhttp://www.cplusplus.com/vector::reservehttp://www.cplusplus.com/vector::sizehttp://www.cplusplus.com/vector::capacity -
7/29/2019 Material de Apoyo Concurso de Porgramadores
5/13
max_size
Return maximum size (public member function)
resize
Change size (public member function)
capacity
Return size of allocated storage capacity (public member function)
emptyTest whether vector is empty (public member function)
reserve
Request a change in capacity (public member function)
Element access:
operator[]
Access element (public member function)
at
Access element (public member function)
front
Access first element (public member function)back
Access last element (public member function)
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)
Member typesoftemplate class vector;
member type definition
reference Allocator::reference
const_reference Allocator::const_reference
http://www.cplusplus.com/reference/stl/vector/max_size/http://www.cplusplus.com/reference/stl/vector/max_size/http://www.cplusplus.com/reference/stl/vector/resize/http://www.cplusplus.com/reference/stl/vector/resize/http://www.cplusplus.com/reference/stl/vector/capacity/http://www.cplusplus.com/reference/stl/vector/capacity/http://www.cplusplus.com/reference/stl/vector/empty/http://www.cplusplus.com/reference/stl/vector/empty/http://www.cplusplus.com/reference/stl/vector/reserve/http://www.cplusplus.com/reference/stl/vector/reserve/http://www.cplusplus.com/reference/stl/vector/operator%5b%5d/http://www.cplusplus.com/reference/stl/vector/operator%5b%5d/http://www.cplusplus.com/reference/stl/vector/at/http://www.cplusplus.com/reference/stl/vector/at/http://www.cplusplus.com/reference/stl/vector/front/http://www.cplusplus.com/reference/stl/vector/front/http://www.cplusplus.com/reference/stl/vector/back/http://www.cplusplus.com/reference/stl/vector/back/http://www.cplusplus.com/reference/stl/vector/assign/http://www.cplusplus.com/reference/stl/vector/assign/http://www.cplusplus.com/reference/stl/vector/push_back/http://www.cplusplus.com/reference/stl/vector/push_back/http://www.cplusplus.com/reference/stl/vector/pop_back/http://www.cplusplus.com/reference/stl/vector/pop_back/http://www.cplusplus.com/reference/stl/vector/insert/http://www.cplusplus.com/reference/stl/vector/insert/http://www.cplusplus.com/reference/stl/vector/erase/http://www.cplusplus.com/reference/stl/vector/erase/http://www.cplusplus.com/reference/stl/vector/swap/http://www.cplusplus.com/reference/stl/vector/swap/http://www.cplusplus.com/reference/stl/vector/clear/http://www.cplusplus.com/reference/stl/vector/clear/http://www.cplusplus.com/reference/stl/vector/get_allocator/http://www.cplusplus.com/reference/stl/vector/get_allocator/http://www.cplusplus.com/reference/stl/vector/get_allocator/http://www.cplusplus.com/reference/stl/vector/clear/http://www.cplusplus.com/reference/stl/vector/swap/http://www.cplusplus.com/reference/stl/vector/erase/http://www.cplusplus.com/reference/stl/vector/insert/http://www.cplusplus.com/reference/stl/vector/pop_back/http://www.cplusplus.com/reference/stl/vector/push_back/http://www.cplusplus.com/reference/stl/vector/assign/http://www.cplusplus.com/reference/stl/vector/back/http://www.cplusplus.com/reference/stl/vector/front/http://www.cplusplus.com/reference/stl/vector/at/http://www.cplusplus.com/reference/stl/vector/operator%5b%5d/http://www.cplusplus.com/reference/stl/vector/reserve/http://www.cplusplus.com/reference/stl/vector/empty/http://www.cplusplus.com/reference/stl/vector/capacity/http://www.cplusplus.com/reference/stl/vector/resize/http://www.cplusplus.com/reference/stl/vector/max_size/ -
7/29/2019 Material de Apoyo Concurso de Porgramadores
6/13
iterator Random access iterator
const_iterator Constant random access iterator
size_type Unsigned integral type (usually same assize_t)
difference_type Signed integral type (usually same asptrdiff_t)
value_type T
allocator_type Allocator
pointer Allocator::pointer
const_pointer Allocator::const_pointer
reverse_iterator reverse_iterator
const_reverse_iteratorreverse_iterator
Vector specialization: vector
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 inC++: 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 class specialization as:
123456
78910
class vector::reference {friendclass vector;reference(); // no public constructor
public:~reference();operatorbool () const; // convert to bool
reference& operator= ( constbool x ); // assign from boolreference& operator= ( const reference& x ); // assign from bitvoid flip(); // flip bit value.
}
For a similar container class to contain bits, but with a fixed size, seebitset.
class template
std::listList
Lists are a kind of sequence container. As such, their elements are ordered following a linearsequence.
List containers are implemented as doubly-linked lists; Doubly linked lists can store each of theelements 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 elementfollowing it.
http://www.cplusplus.com/size_thttp://www.cplusplus.com/size_thttp://www.cplusplus.com/size_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/bitsethttp://www.cplusplus.com/bitsethttp://www.cplusplus.com/bitsethttp://www.cplusplus.com/bitsethttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/size_t -
7/29/2019 Material de Apoyo Concurso de Porgramadores
7/13
This provides the following advantages to list containers:
Efficient insertion and removal of elements before any other specific element in thecontainer (constant time).
Efficient moving elements and block of elements within the container or even betweendifferent containers (constant time).
Iterating over the elements in forward or reverse order (linear time).
Compared to other base standard sequence containers (vectors anddeques), lists performgenerally better in inserting, extracting and moving elements in any position within the containerfor which we already have an iterator, and therefore also in algorithms that make intensive useof these, like sorting algorithms.
The main drawback oflists 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 factorfor 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 templateparameters:template < class T, class Allocator = allocator > 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 simplestmemory 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
http://www.cplusplus.com/vectorhttp://www.cplusplus.com/vectorhttp://www.cplusplus.com/vectorhttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/dequehttp://www.cplusplus.com/reference/stl/list/list/http://www.cplusplus.com/reference/stl/list/list/http://www.cplusplus.com/reference/stl/list/~list/http://www.cplusplus.com/reference/stl/list/~list/http://www.cplusplus.com/reference/stl/list/operator=/http://www.cplusplus.com/reference/stl/list/operator=/http://www.cplusplus.com/reference/stl/list/begin/http://www.cplusplus.com/reference/stl/list/begin/http://www.cplusplus.com/reference/stl/list/begin/http://www.cplusplus.com/reference/stl/list/operator=/http://www.cplusplus.com/reference/stl/list/~list/http://www.cplusplus.com/reference/stl/list/list/http://www.cplusplus.com/dequehttp://www.cplusplus.com/vector -
7/29/2019 Material de Apoyo Concurso de Porgramadores
8/13
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
http://www.cplusplus.com/reference/stl/list/end/http://www.cplusplus.com/reference/stl/list/end/http://www.cplusplus.com/reference/stl/list/rbegin/http://www.cplusplus.com/reference/stl/list/rbegin/http://www.cplusplus.com/reference/stl/list/rend/http://www.cplusplus.com/reference/stl/list/rend/http://www.cplusplus.com/reference/stl/list/empty/http://www.cplusplus.com/reference/stl/list/empty/http://www.cplusplus.com/reference/stl/list/size/http://www.cplusplus.com/reference/stl/list/size/http://www.cplusplus.com/reference/stl/list/max_size/http://www.cplusplus.com/reference/stl/list/max_size/http://www.cplusplus.com/reference/stl/list/resize/http://www.cplusplus.com/reference/stl/list/resize/http://www.cplusplus.com/reference/stl/list/front/http://www.cplusplus.com/reference/stl/list/front/http://www.cplusplus.com/reference/stl/list/back/http://www.cplusplus.com/reference/stl/list/back/http://www.cplusplus.com/reference/stl/list/assign/http://www.cplusplus.com/reference/stl/list/assign/http://www.cplusplus.com/reference/stl/list/push_front/http://www.cplusplus.com/reference/stl/list/push_front/http://www.cplusplus.com/reference/stl/list/pop_front/http://www.cplusplus.com/reference/stl/list/pop_front/http://www.cplusplus.com/reference/stl/list/push_back/http://www.cplusplus.com/reference/stl/list/push_back/http://www.cplusplus.com/reference/stl/list/pop_back/http://www.cplusplus.com/reference/stl/list/pop_back/http://www.cplusplus.com/reference/stl/list/insert/http://www.cplusplus.com/reference/stl/list/insert/http://www.cplusplus.com/reference/stl/list/erase/http://www.cplusplus.com/reference/stl/list/erase/http://www.cplusplus.com/reference/stl/list/swap/http://www.cplusplus.com/reference/stl/list/swap/http://www.cplusplus.com/reference/stl/list/clear/http://www.cplusplus.com/reference/stl/list/clear/http://www.cplusplus.com/reference/stl/list/splice/http://www.cplusplus.com/reference/stl/list/splice/http://www.cplusplus.com/reference/stl/list/remove/http://www.cplusplus.com/reference/stl/list/remove/http://www.cplusplus.com/reference/stl/list/remove/http://www.cplusplus.com/reference/stl/list/splice/http://www.cplusplus.com/reference/stl/list/clear/http://www.cplusplus.com/reference/stl/list/swap/http://www.cplusplus.com/reference/stl/list/erase/http://www.cplusplus.com/reference/stl/list/insert/http://www.cplusplus.com/reference/stl/list/pop_back/http://www.cplusplus.com/reference/stl/list/push_back/http://www.cplusplus.com/reference/stl/list/pop_front/http://www.cplusplus.com/reference/stl/list/push_front/http://www.cplusplus.com/reference/stl/list/assign/http://www.cplusplus.com/reference/stl/list/back/http://www.cplusplus.com/reference/stl/list/front/http://www.cplusplus.com/reference/stl/list/resize/http://www.cplusplus.com/reference/stl/list/max_size/http://www.cplusplus.com/reference/stl/list/size/http://www.cplusplus.com/reference/stl/list/empty/http://www.cplusplus.com/reference/stl/list/rend/http://www.cplusplus.com/reference/stl/list/rbegin/http://www.cplusplus.com/reference/stl/list/end/ -
7/29/2019 Material de Apoyo Concurso de Porgramadores
9/13
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 typesoftemplate 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 assize_t)
difference_type Signed integral type (usually same asptrdiff_t)
value_type T
allocator_type Allocatorpointer Allocator::pointer
const_pointer Allocator::const_pointer
reverse_iterator reverse_iterator
const_reverse_iteratorreverse_iterator
class template
std::priority_queue
Priority queue
Priority queues are a type of container adaptors, specifically designed such that its first elementis always the greatest of the elements it contains, according to some strict weak orderingcondition.
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
http://www.cplusplus.com/reference/stl/list/remove_if/http://www.cplusplus.com/reference/stl/list/remove_if/http://www.cplusplus.com/reference/stl/list/unique/http://www.cplusplus.com/reference/stl/list/unique/http://www.cplusplus.com/reference/stl/list/merge/http://www.cplusplus.com/reference/stl/list/merge/http://www.cplusplus.com/reference/stl/list/sort/http://www.cplusplus.com/reference/stl/list/sort/http://www.cplusplus.com/reference/stl/list/reverse/http://www.cplusplus.com/reference/stl/list/reverse/http://www.cplusplus.com/reference/stl/list/get_allocator/http://www.cplusplus.com/reference/stl/list/get_allocator/http://www.cplusplus.com/size_thttp://www.cplusplus.com/size_thttp://www.cplusplus.com/size_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/ptrdiff_thttp://www.cplusplus.com/size_thttp://www.cplusplus.com/reference/stl/list/get_allocator/http://www.cplusplus.com/reference/stl/list/reverse/http://www.cplusplus.com/reference/stl/list/sort/http://www.cplusplus.com/reference/stl/list/merge/http://www.cplusplus.com/reference/stl/list/unique/http://www.cplusplus.com/reference/stl/list/remove_if/ -
7/29/2019 Material de Apoyo Concurso de Porgramadores
10/13
encapsulated object of a specific container class as its underlying container, providing a specificset of member functions to access its elements. Elements arepoppedfrom the "back"of thespecific 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 otherspecifically designed container class. The only requirement is that it must be accessible throughrandom access iterators and it must support the following operations:
front() push_back() pop_back()
Therefore, the standard container class templatesvectoranddequecan be used. By default, ifno container class is specified for a particular priority_queue class, the standard container
class templatevectoris 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 thealgorithmsmake_heap,push_heapandpop_heapwhen appropriate.
In their implementation in the C++ Standard Template Library, priority queues take threetemplate parameters:12template < class T, class Container = vector,
class Compare = less > classpriority_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 ifa is to be placed earlier than b in a strict weak ordering operation. Thiscan either be a class implementing a function call operatoror a pointer to a function.This defaults to less, which returns the same as applying the less-than
operator(a
-
7/29/2019 Material de Apoyo Concurso de Porgramadores
11/13
empty
Test whether container is empty (public member function)
size
Return size (public member function)
top
Access top element (public member function)
pushInsert element (public member function)
pop
Remove top element (public member function)
_________________ALGORITHMS________________
function template
std::binary_search
template bool binary_search ( ForwardIterator first, ForwardIterator last,
const T& value );
template 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
-
7/29/2019 Material de Apoyo Concurso de Porgramadores
12/13
Parametersfirst, 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 firstand 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 valuetrue if an element in value is found, and false otherwise.
Example
123
45678910111213141516
17181920212223242526
// binary_search example#include #include
#include usingnamespace std;
bool myfunction (int i,int j) { return (i
-
7/29/2019 Material de Apoyo Concurso de Porgramadores
13/13
2728 return 0;
}
Output:looking for a 3... found!looking for a 6... not found.
ComplexityAt 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