-
Notifications
You must be signed in to change notification settings - Fork 1
/
c++ stl
63 lines (44 loc) · 2.92 KB
/
c++ stl
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#MULTIMAP :
Multimap is similar to map with an addition that multiple elements can have same keys. Also, it is NOT required that the key value and
mapped value pair has to be unique in this case .
// insert elements in random order
gquiz1.insert(pair <int, int> (1, 40)); or insert({1,2}) ;
gquiz1.insert(pair <int, int> (2, 30));
gquiz1.insert(pair <int, int> (2, 60));
// printing multimap gquiz1
multimap <int, int> :: iterator itr;
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// assigning the elements from gquiz1 to gquiz2
multimap <int, int> gquiz2(gquiz1.begin(),gquiz1.end());
// remove all elements with key = 4
int num;
num = gquiz2.erase(4);
// find() function finds the position at which 3 is ........ The elements from position 3 in multimap are : \n";
for (auto itr = mp.find(3); itr != mp.end(); itr++)
cout << itr->first << '\t' << itr->second << '\n';
"multimap mp has number of elements"; ----------> mp.size() ;
/////
multimap_name.cbegin() : is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container.
#QUEUE
Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
empty() – Returns whether the queue is empty.
size() – Returns the size of the queue.
queue::swap() in C++ STL: Exchange the contents of two queues but the queues must be of same type, although sizes may differ.
queue::emplace() in C++ STL: Insert a new element into the queue container, the new element is added to the end of the queue.
queue::front() and queue::back() in C++ STL– front() function returns a reference to the first element of the queue. back() function returns a reference to the last element of the queue.
push(g) and pop() – push() function adds the element ‘g’ at the end of the queue. pop() function deletes the first element of the queue
queue <int> gquiz; gquiz.front();
gquiz.back() : gquiz.back();
#Priority Queue in C++
Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements
are in non increasing order(hence we can see that each element of the queue has a priority{fixed order}).
Priority queue of pairs in C++ (Ordered by first) ..............................IMPORTANT...............................
----> Max Heap == priority Queue <---------
And syntax for the priority queue ans queue is sameeeeeeeeeeeee . Max heap = priority_queue <int> q ;
----> min Heap <---------
// Syntax to create a min heap for priority queue
priority_queue <int, vector<int>, greater<int>> g = gq;