Relearning C++

Some notes about C++ and Leetcode.

Lately, I’ve been using LeetCode to prepare for an internship and was surprised by how much the C++ standard has evolved since I studied it as an undergraduate. It’s fascinating to revisit C++ and explore the new features and improvements.

Data Structures #

Vector #

vector is a sequence container in the C++ Standard Library that provides dynamic array functionality.

#include <vector>
using namespace std;
vector<int> numbers = {1, 2, 3};

// as function parameter
vector<int> twoSum(vector<int>& nums, int target) {};

// access element
cout << "The first element of the vector is:" << numbers[0] << endl;

// add element
numbers.push_back(1);
numbers.push_back("hello");

// remove element 
numbers.pop_back();

// used in loops
for (int num : numbers) {};

// size
int size = numbers.size();

// check if it's empty
bool isEmpty = numbers.empty();

// sum
int totalSum = accumulate(numbers.begin(), numbers.end(), 0);

String #

#include <string>
using namespace std;
string str = "I love engineering."

// length
int len = str.length();

// find substr
bool isSubstr = str.find("love")

// extract a substring
string substr = str.substr(pos, len)

// check if it's empty
bool isEmpty = str.empty();

Unordered_map #

unordered_map is a type of associative container in the C++ Standard Library used to store key-value pairs.

#include <unordered_map>
using namespace std;
unordered_map<char, int> myMap = {
            {'I', 1},
            {'V', 5}
        };

// Checking if a Key Exists:
if (myMap.find(3) != myMap.end()) {
    // Key 3 exists
}