andersch.dev

<2024-11-01 Fri>

C++

C++ is a systems programming language that extends C with object-oriented features.

Standard Template Library (STL)

The C++ STL offers a collection of algorithms and data structures using templates.

Containers:

  • Sequential: vector, deque, list
  • Associative: set, map, multiset, multimap
  • Unordered: unordered_set, unordered_map, unordered_multiset, unordered_multimap
  • Wrappers: stack, queue, priority_queue

Iterators: <iterator>

  • Input/Output: std::begin, std::rbegin, std::end, std::rend
  • Advance: std::next, std::prev, std::advance

Algorithms: <algorithm>

  • Sorting: sort, stable_sort
  • Searching: find, binary_search
  • Transformation: copy, transform, reverse
  • Set Operations: set_union, set_intersection

Misc.:

  • Input/Output: <iostream>
  • Strings: std::string, std::string_view
  • Time: <chrono>
  • Memory Management: <memory>, std::unique_ptr, std::shared_ptr
  • Threading: <thread>
  • File System: <filesystem>
  • Function Objects (Functors): operator(), std::function
  • Exception Handling: <exception>, std::exception
  • Allocators: std::allocator, custom allocators
  • Random: <random>

Templates

Key tools:

  • template <typename T>: Declare a template
  • decltype: Get type at compile-time
  • auto: Infer type
  • constexpr: Function can be evaluated at compile-time
  • <type_traits>: std::is_same, std::is_integral, std::enable_if, std::conditional
  • template <typename... Args>: Variadic template accepts variable parameters
  • Template Specialization: Define template for a specific type

Simple Example

#include <iostream>

template <typename T> // typename or class
void swap(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10;
    int y = 20;

    std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
    swap(x, y);
    std::cout << "After swap: x = " << x << ", y = " << y << std::endl;

    return 0;
}

Common Code Interview Questions

Two Sum Problem

Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
#include <iostream>
#include <vector>
#include <unordered_map>

std::vector<int> twoSum(const std::vector<int>& nums, int target) {
    std::unordered_map<int, int> num_map; // To store the number and its index
    for (int i = 0; i < nums.size(); ++i) {
        int complement = target - nums[i]; // Calculate the complement
        if (num_map.find(complement) != num_map.end()) {
            return {num_map[complement], i}; // Return the indices
        }
        num_map[nums[i]] = i; // Store the number and its index
    }
    return {}; // Return an empty vector if no solution is found
}

int main() {
    std::vector<int> nums = {2, 7, 11, 15};
    int target = 9;
    std::vector<int> result = twoSum(nums, target);

    std::cout << "Indices: " << result[0] << ", " << result[1] << std::endl; // Outputs: Indices: 0, 1
    return 0;
}