Repository

Cpp-STL-Quick-Help

It contains C++ STLs usage and quick help with easy to understand comments and examples

README

C++ STL Quick Help

It contains C++ STLs usage and quick help with easy to understand comments and examples (copy+paste to use). I learned these while solving different kinds of Leetcode Questions. I will be using "int, string etc" for ease and not complex entities like pairs, structs etc 😉. You can replace it with any data structure If you are confused with the syntax or description, see the example. I am sure that will clear things BECAUSE I have specifically chosen mag_right: "EASY + IMPORTANT + MOST USED" examples.
Last but not least, I have added Leetcode Qns also which can be easily solved using STLs

:memo:Different ways of using priority_queue (i.e. heap) :mount_fuji:

  • Default declarations

    priority_queue pq;                            //creates max-heap
    priority_queue pq;               //creates max-heap
  • writing comparator function for priority_queue

  1. Using in-built comparator provided by C++ :

priority_queue pq; //creates min-heap
priority_queue< pair, vector, greater > pq; //min_heap of pairs
priority_queue< pair, vector, greater > pq; //min_heap of pairs

```c++
2. Using user defined comparator as a structure

struct comp {
    bool operator()(int &a, int &b) {
        return ab; //min-heap
    }
};

priority_queue pq;  //usage
3. Using user defined comparator as a function

static bool comp(int &a, int &b) {
    return ab; //min-heap
}

priority_queue pq(comp);   //usage
4. Using lambda function

auto comp = [](int &a, int &b) {
    return ab; //min-heap 
};

priority_queue pq(comp);   //usage

NOTE :
You can receive parameters inside [] as well i.e. auto comp = [some_parameters]
Ex : You want to access a map inside this lambda function
unordered_map mp;

auto comp = [&mp](int &a, int &b) {
    return mp[a] < mp[b]; //etc.
};

:memo: When and why to use std::move() :arrow_left:


/*
    To efficiently transfer the resources from source to target.
    By efficient, I mean no usage of extra space and time for creating copy.
*/
Examples :
    string source = "MIK";
    string target = "";
    target = std::move(source);
    cout