├── README.md
├── algorithm.cpp
├── array.cpp
├── deque.cpp
├── list.cpp
├── map.cpp
├── priority_queue.cpp
├── queue.cpp
├── set.cpp
├── stacks.cpp
├── unordered_map.cpp
├── unordered_set.cpp
└── vector.cpp
/README.md:
--------------------------------------------------------------------------------
1 | # Important CPP(C++) STL functions for competitive programming and other questions
2 |
3 | ### Most of the STL functions are in the [Algorithm](https://github.com/geeky01adarsh/CPP_STL/blob/main/algorithm.cpp) file as it could be applied almost every STL containers.
4 |
5 | ### Find complete hand-written notes with time complexities from [here](https://drive.google.com/file/d/1aDsx8ZYymjhLZm5JwtSmTsaojvucdXzr/view?usp=sharing).
6 |
7 |
C++ STL
8 |
9 | - Algorithm
10 |
11 | - Binary Search
12 | - Upper/Lower Bound
13 | - min/max
14 | - Reverse/Rotate
15 | - Sort/Swap etc.
16 |
17 | - Containers
18 |
19 | - Sequence Container
20 |
27 | - Container Adaptors
28 |
33 | - Associative Containers
34 |
35 | - Set
36 | - Map
37 | - MultiSet
38 | - MultiMap
39 |
40 | - Unordered Associative
41 |
47 |
48 |
49 |
50 |
51 | ### Time Complexity and Space complexites for these containers -- [Hand written notes(pdf)](https://drive.google.com/file/d/1aBsuM59ileYqExvXOmK7IAcvjUT7DPre/view?usp=sharing) -- [Digital Copy(pdf)](https://drive.google.com/file/d/1DkKDrURUyOl9OO2N0hHHfZkPxso7Fh9Q/view?usp=sharing)
52 |
53 | #### Thank You for visiting this repo. Please provide support by staring it.
54 | ##### Feel free to contact me
55 | [
56 | ](adarsh91094@gmail.com) [
57 | ](https://www.linkedin.com/in/adarsh-navneet-sinha-34a36419a/) [
58 | ](https://twitter.com/geeky01adarsh)
59 |
--------------------------------------------------------------------------------
/algorithm.cpp:
--------------------------------------------------------------------------------
1 | // These algoritms may be applied to most of the stl containers
2 | // we will implement them in vector here
3 | #include
4 | #include
5 | #include
6 |
7 | using namespace std;
8 |
9 | int main()
10 | {
11 | vector v;
12 | v.push_back(2);
13 | // v.push_back(5);
14 | v.push_back(4);
15 | v.push_back(1);
16 | v.push_back(6);
17 | v.push_back(3);
18 | v.push_back(7);
19 |
20 | // binary search - to search if an element is present
21 | cout << "Check if 6 is present in vector- ";
22 | cout << binary_search(v.begin(), v.end(), 6) << endl;
23 |
24 | // lower bound:- The element just greater than given element
25 | cout << "The lower bound for 5 is ";
26 | cout << lower_bound(v.begin(), v.end(), 5) - v.begin() << endl;
27 |
28 | // upper bound:- The element just smaller than the given element
29 | cout << "The upper bound for 5 is ";
30 | cout << upper_bound(v.begin(), v.end(), 6) - v.begin() << endl;
31 |
32 | // maximum of two number
33 | int a = 8, b = 10;
34 | cout << "Maximum of " << a << " and " << b << " is " << max(a, b) << endl;
35 |
36 | // minimum of two no.
37 | cout << "Minimum of " << a << " and " << b << " is " << min(a, b) << endl;
38 |
39 | // swap function to replace the value among themselves
40 | cout << "Earlier a=" << a << " and b=" << b << endl;
41 | swap(a, b);
42 | cout << "Now a=" << a << " and b=" << b << endl;
43 |
44 | // reverse the container
45 | cout << "Before reversing vector : ";
46 | for (auto i : v)
47 | cout << i << " ";
48 | cout << endl;
49 | // calling the reverse function
50 | reverse(v.begin(), v.end());
51 | cout << "After reversing vector : ";
52 | for (auto i : v)
53 | cout << i << " ";
54 | cout << endl;
55 |
56 | // rotate - it will rotate by provided position
57 | cout << "Before rotating vector : ";
58 | for (auto i : v)
59 | cout << i << " ";
60 | cout << endl;
61 | // calling the rotate function
62 | rotate(v.begin(), v.begin() + 1, v.end());
63 | cout << "After rotating vector by 1 : ";
64 | for (auto i : v)
65 | cout << i << " ";
66 | cout << endl;
67 |
68 | // sorting the container
69 | // it is based on intro sort(quick sort + heap sort + insertion sort)
70 | cout << "Before sorting vector : ";
71 | for (auto i : v)
72 | cout << i << " ";
73 | cout << endl;
74 | // calling the rotate function
75 | sort(v.begin(), v.end());
76 | cout << "After sorting vector : ";
77 | for (auto i : v)
78 | cout << i << " ";
79 | cout << endl;
80 |
81 | return 0;
82 | }
83 |
--------------------------------------------------------------------------------
/array.cpp:
--------------------------------------------------------------------------------
1 | // array using stl function can do much more task and have a good library support
2 |
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 |
11 | // declaration
12 | array a;
13 | // assigning values
14 | for (int i = 0; i < 4; i++)
15 | {
16 | cin >> a[i];
17 | }
18 | // declaration and assigning value
19 | array arr = {1, 2, 3, 4};
20 |
21 | // printing the array
22 | for (int i = 0; i < 4; i++)
23 | {
24 | cout << a[i] << " ";
25 | }
26 | cout << endl;
27 | for (int i = 0; i < 4; i++)
28 | {
29 | cout << arr[i] << " ";
30 | }
31 | cout << endl;
32 |
33 | // finding the size of the array
34 | cout << a.size() << " " << arr.size() << endl;
35 |
36 | // accessing any element at given potion
37 | cout << a[3] << " " << a.at(3) << endl;
38 |
39 | // checking if the array is empty
40 | cout << "Is array a empty ? " << a.empty() << endl;
41 |
42 | // excessing first and last element
43 | cout << arr.front() << " " << arr.back() << endl;
44 |
45 | return 0;
46 | }
--------------------------------------------------------------------------------
/deque.cpp:
--------------------------------------------------------------------------------
1 | // deque is also know as doubly ended queue
2 |
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | // declaration
11 | deque dq;
12 |
13 | // assigning value
14 | // we can enter value in doubly ended queue both from front and back
15 | dq.push_back(1);
16 | dq.push_back(2);
17 | dq.push_back(3);
18 | dq.push_back(4);
19 | dq.push_back(5);
20 | dq.push_front(0);
21 |
22 | // printing the deque
23 | for (auto i : dq)
24 | cout << i << " ";
25 | cout << endl<
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | // declaration
9 | list ls;
10 |
11 | // assigning values
12 | ls.push_back(1);
13 | ls.push_back(3);
14 | ls.push_back(2);
15 | ls.push_back(4);
16 | ls.push_front(0);
17 | // printing a list
18 | for (auto i : ls)
19 | cout << i << " ";
20 | cout << endl;
21 |
22 | // declaration with given dingle value
23 | list l(5, 10);
24 | cout << "this list will print like this ";
25 | for (auto i : l)
26 | cout << i << " ";
27 | cout << endl;
28 |
29 | // deleting the first element
30 | ls.erase(ls.begin());
31 | cout << "After deleting first element : ";
32 | for (auto i : ls)
33 | cout << i << " ";
34 | cout << endl;
35 |
36 | // deleting the first or the last element
37 | ls.pop_back();
38 | ls.pop_front();
39 | cout << "After deleting front and back element : ";
40 | for (auto i : ls)
41 | cout << i << " ";
42 | cout << endl;
43 |
44 | // size of the list
45 | cout << "The size of the list is " << ls.size() << endl;
46 |
47 | // iterators
48 | cout << "front and back iterators are " << *ls.begin() << " ls.end()" << endl;
49 |
50 | return 0;
51 | }
--------------------------------------------------------------------------------
/map.cpp:
--------------------------------------------------------------------------------
1 | // stores in key-value pair
2 | // all kyes are unique
3 | // stored in sorted order
4 |
5 | #include
6 | #include