C++ Toolbox

Vector

  1. Init 2D vectors: https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector

     std::vector<std::vector<int> > v (A_NUMBER, std::vector <int> (OTHER_NUMBER, DEFAULT_VALUE))
  2. Find an element in an array

    Simple version:

     vector<int> vc = {2,10};
     if(find(vc.begin(), vc.end(), 10) != vc.end())
         cout << "Found 10!";

    Generic version:

     template < typename T>
    std::pair<bool, int > findInVector(const std::vector<T>  & vecOfElements, const T  & element)
    {
     std::pair<bool, int > result;
    
     // Find given element in vector
     auto it = std::find(vecOfElements.begin(), vecOfElements.end(), element);
    
     if (it != vecOfElements.end())
     {
         result.second = distance(vecOfElements.begin(), it);
         result.first = true;
     }
     else
     {
         result.first = false;
         result.second = -1;
     }
    
     return result;

Sort

  1. Sort vector of vector: https://stackoverflow.com/questions/14419520/sort-vector-of-vectors

     std::sort(vec.begin(), vec.end(),
               [](const std::vector<int>& a, const std::vector<int>& b) {
       return a[2] < b[2];
     });
  2. Sum of vector elements: https://stackoverflow.com/questions/3221812/how-to-sum-up-elements-of-a-c-vector

     sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);

String

  1. pop_back

  2. Tokenize https://www.geeksforgeeks.org/tokenizing-a-string-cpp/

     // Tokenize w.r.t. space ' '
     string s = "I love leetcode, leetcode loves me."
     vector<string> tokens;
     stringstream ss(s); 
     string tok; 
    
     while(getline(ss, tok, ' ')) { 
         if(tok == " " || tok == "") continue;
         tokens.push_back(tok); 
     }

    Another way: https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string

  3. Convert int to char

    • In C++17, use std::to_chars as:

    std::array<char, 10> str;
    std::to_chars(str.data(), str.data() + str.size(), 42);
    • In C++11, use std::to_string as:

    std::string s = std::to_string(number);
    char const *pchar = s.c_str();  //use char const* as target type

Priority_queue

  1. Create a min-heap:

     std::priority_queue<int, std::vector<int>, std::greater<int> > my_min_heap;
  2. Store my own data structure:https://stackoverflow.com/questions/16111337/declaring-a-priority-queue-in-c-with-a-custom-comparator/48587737

     class rate_pair
     {
     public:
         int index;
         int rate;
    
         rate_pair(int id, int r) {
             index = id;
             rate = r;
         }
     };
    
     class Compare
     {
     public:
         bool operator() (rate_pair rate_pair_1, rate_pair rate_pair_2)
         {
             return rate_pair_1.rate > rate_pair_2.rate;
         }
     };
    
     class Solution {
     public:
         int candy(vector<int>& ratings) {
             // Corner case
             if(ratings.size() < 2) return ratings.size();
    
             // Use a pq to hold ratings 
             priority_queue<rate_pair, std::vector<rate_pair>, Compare> pq;
             for(int i=0; i<ratings.size(); i++) {
                 rate_pair r(i, ratings[i]);
                 pq.push(r);
             }
    
             while(!pq.empty()) {
                 cout << pq.top().index << " " << pq.top().rate << endl;
                 pq.pop();
             }
    
             vector<int> candy(ratings.size(), 1);
    
             return accumulate(candy.begin(), candy.end(), 0);
         }
     };

Set

Last updated