├── queue.cpp ├── stacks.cpp ├── array.cpp ├── map.cpp ├── unordered_set.cpp ├── unordered_map.cpp ├── set.cpp ├── list.cpp ├── priority_queue.cpp ├── vector.cpp ├── deque.cpp ├── algorithm.cpp └── README.md /queue.cpp: -------------------------------------------------------------------------------- 1 | // It works on the FIFO methodology 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | // declaration 10 | queue q; 11 | 12 | // assigning values 13 | q.push(1); 14 | q.push(2); 15 | q.push(3); 16 | q.push(4); 17 | q.push(5); 18 | 19 | // size of queue 20 | cout<<"The size of queue is "< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | // declaration 10 | stack st; 11 | 12 | // assigning value 13 | st.push("Adarsh"); 14 | st.push("Navneet"); 15 | st.push("Sinha"); 16 | 17 | // printing our stack 18 | cout<<"top element is "< 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 | } -------------------------------------------------------------------------------- /map.cpp: -------------------------------------------------------------------------------- 1 | // stores in key-value pair 2 | // all kyes are unique 3 | // stored in sorted order 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(){ 11 | // declaration 12 | map mp; 13 | 14 | // assigning values 15 | // m[key]=value 16 | mp[1]="Adarsh"; 17 | mp[2]="Navneet"; 18 | mp[30]="Sinha"; 19 | 20 | // inserting elements using insert function 21 | mp.insert({5,"geeky01adarsh"}); 22 | 23 | // printing the map 24 | for(auto i:mp){ 25 | cout<<"key = "< 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(){ 11 | // declaration 12 | unordered_map um; 13 | 14 | // assigning values 15 | // m[key]=value 16 | um[1]="Adarsh"; 17 | um[2]="Navneet"; 18 | um[30]="Sinha"; 19 | 20 | // inserting elements using insert function 21 | um.insert({5,"geeky01adarsh"}); 22 | 23 | // printing the map 24 | for(auto i:um){ 25 | cout<<"key = "< 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 | } -------------------------------------------------------------------------------- /priority_queue.cpp: -------------------------------------------------------------------------------- 1 | // it works on max heap and min heap 2 | // i.e. it stores values in sorted order either increasing or decreasing 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | // decreasing order implementation 12 | priority_queue maxi; 13 | 14 | // increasing order implementaion 15 | priority_queue, greater> mini; 16 | 17 | // putting the same values in both the queues 18 | // pushing values to maxi(max heap) 19 | maxi.push(5); 20 | maxi.push(7); 21 | maxi.push(1); 22 | maxi.push(2); 23 | maxi.push(9); 24 | // pushing value to mini 25 | mini.push(5); 26 | mini.push(7); 27 | mini.push(1); 28 | mini.push(2); 29 | mini.push(9); 30 | 31 | // empty function 32 | cout << "Is maxi empty? " << maxi.empty() << endl; 33 | 34 | // size fuction 35 | cout << "The size of maxi is " << maxi.size() << endl; 36 | cout << "The size of mini is " << mini.size() << endl; 37 | 38 | // printing the maxi(max heap) 39 | cout << "Printing the maxi "; 40 | int n = maxi.size(); 41 | for (int i = 0; i < n; i++) 42 | { 43 | cout << maxi.top() << " "; 44 | maxi.pop(); 45 | } 46 | cout << endl; 47 | cout << "Is maxi empty now? " << mini.empty() << endl; 48 | 49 | // printing the mini(min heap) 50 | cout << "Printing the mini "; 51 | n = mini.size(); 52 | for (int i = 0; i < n; i++) 53 | { 54 | cout << mini.top() << " "; 55 | mini.pop(); 56 | } 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // declaration 8 | vector v; 9 | 10 | // assigning values 11 | for (int i = 0; i < 5; i++) 12 | { 13 | int temp; 14 | cin >> temp; 15 | // adding the input to our vector 16 | v.push_back(temp); 17 | } 18 | 19 | // printing the vector 20 | for (auto i : v) 21 | cout << i << " "; 22 | cout << endl; 23 | 24 | // with same given values 25 | vector a(5, 1); 26 | // checking our vector 27 | for (auto i : a) 28 | cout << i << " "; 29 | cout << endl; 30 | 31 | // coping one vector in different one 32 | vector last(a); 33 | // checking our vector 34 | for (auto i : last) 35 | cout << i << " "; 36 | cout << endl; 37 | 38 | // size of the vector 39 | cout << v.size() << endl; 40 | 41 | // capacity of the vector 42 | // how much place in vector is currently empty(not filled) 43 | cout << v.capacity() << endl; 44 | 45 | // excessing certain element at any index 46 | cout << v[1] << " " << v.at(1) << endl; 47 | 48 | // rempving the last element from the vector 49 | v.pop_back(); 50 | // checking our fuction 51 | for (auto i : v) 52 | cout << i << " "; 53 | cout << endl; 54 | 55 | // front and back elements 56 | cout << v.front() << " " << v.back() << endl; 57 | 58 | // iterators(pointers for first and last+1 element) 59 | cout << *v.begin() << " " << *(v.end() - 1) << endl; 60 | 61 | // deleting all elements of vector 62 | v.clear(); 63 | // size changes to 0 but the capacity remains the same 64 | cout << "SIZE : " << v.size() << endl 65 | << "CAPACITY : " << v.capacity() << endl; 66 | 67 | // to check if our vector is empty or not 68 | cout << "Is the vector-a empty? " << a.empty() << endl; 69 | cout << "Is the vector-v empty? " << v.empty() << endl; 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /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< 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 | -------------------------------------------------------------------------------- /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 |
    1. Binary Search
    2. 12 |
    3. Upper/Lower Bound
    4. 13 |
    5. min/max
    6. 14 |
    7. Reverse/Rotate
    8. 15 |
    9. Sort/Swap etc.
    10. 16 |
    17 |
  • Containers
  • 18 |
      19 |
    1. Sequence Container
    2. 20 | 27 |
    3. Container Adaptors
    4. 28 | 33 |
    5. Associative Containers
    6. 34 |
        35 |
      • Set
      • 36 |
      • Map
      • 37 |
      • MultiSet
      • 38 |
      • MultiMap
      • 39 |
      40 |
    7. Unordered Associative
    8. 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 | [![image](https://user-images.githubusercontent.com/74068552/128004471-32cc18b7-4ce6-4faa-9536-410607433b08.png) 56 | ](adarsh91094@gmail.com) [![image](https://user-images.githubusercontent.com/74068552/128004564-e85e171a-0869-4c1f-9451-eb49254e8ea9.png) 57 | ](https://www.linkedin.com/in/adarsh-navneet-sinha-34a36419a/) [![image](https://user-images.githubusercontent.com/74068552/128004703-5babc26d-679a-43a0-8d6b-9e4146d8f65a.png) 58 | ](https://twitter.com/geeky01adarsh) 59 | --------------------------------------------------------------------------------