Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

C++ Tips and Tricks πŸ˜€

General βš™οΈ


Faster operations ⏩⏩


Bit magic / Bit mask 0️⃣1️⃣


std::string 🧡


std::vector πŸ”Ό


std::set 🍁


std::map πŸ—ΊοΈ


Other tips 🍹

  1. We can use dec,hex,oct to print numbers in different bases.

     // bitset<bitcnt>(num)
     cout << bitset<12>(n) << "\n"; // Binary
     cout << oct << n << "\n"; // Octal
     cout << hex << n << "\n"; // Hexadecimal
     cout << dec << n << "\n"; // Decimal-normal
    
  2. std::minmax_element == returns pair{minelement, maxelement}

     vector<int> x = {1, 2, 3, 4};
     auto it = minmax_element(all(x));
     cout << *it.first << ' ' << *it.second << endl;
    
     output:
     1 4
    
  3. is_palindrome function using std::equal.

     bool is_palindrome(string s) {
         return equal(all(s) - s.size() / 2, s.rbegin());
     }
    

©️ prepared by chetan6780