Posts

Showing posts from August, 2023

Book review : Complete Guide to Standard C++ Algorithms

Book under review: A Complete Guide to Standard C++ Algorithms by Simon Toth History of standard C++ algorithms The history of standard C++ algorithms is characterized by a few notable milestones. The C++98 standard introduced most of the algorithms, but it was the C++11 standard that made algorithms more useful with the introduction of lambdas. Before lambdas, the time investment of writing a custom function object made the usefulness of algorithms dubious. The C++11 standard also introduced move semantics, which made algorithms more efficient by allowing them to move objects instead of copying them. The C++14 standard added a few new algorithms and made some existing ones more efficient. The C++17 standard added several new algorithms, including ones for generating random numbers and for finding adjacent elements that satisfy a predicate. Finally, the C++20 standard added several new algorithms, including ones for sorting and searching ranges, and for transforming and filtering ra

Methods available with vector in C++

  The vector class in C++ provides various methods to perform different operations on vectors. Here are some commonly used methods: push_back() : Inserts a new element at the end of the vector. pop_back() : Removes the last element from the vector. size() : Returns the number of elements in the vector. empty() : Returns true if the vector is empty. clear() : Removes all elements from the vector. begin() : Returns an iterator pointing to the first element of the vector. end() : Returns an iterator pointing to the past-the-end element of the vector. insert() : Inserts an element at a specified position in the vector. erase() : Removes an element from a specified position in the vector. resize() : Resizes the vector to a specified size. swap() : Swaps the contents of two vectors. at() : Returns a reference to the element at a specified position in the vector. front() : Returns a reference to the first element in the vector. back() : Returns a reference to the last element in the vector. d

Kadane's algorithm for finding the maximum subarray sum in an array

int best = 0 , sum = 0 ; for ( int k = 0 ; k < n ; k + + ) { sum = max ( array [ k ] , sum + array [ k ] ) ; best = max ( best , sum ) ; } cout << best << "\n" ; The program implements Kadane's algorithm, which is an efficient algorithm for finding the maximum subarray sum in an array. The algorithm works by iterating through the array and keeping track of the maximum sum encountered so far. Here's a step-by-step explanation of how the algorithm works: Initialize two variables, "best" and "sum", to 0: The variable "best" will store the maximum subarray sum. The variable "sum" will store the current sum of the subarray being considered. Iterate through the array from index 0 to n-1: The loop variable "k" represents the current index being considered. For each element at index k, calculate the new sum: The new sum is calculated by taking the maximum of two values: The current element a

Differences between references and pointers in C++ with use cases

In C++, both references and pointers provide a way to access data indirectly, but they have different use cases and properties. Pointers  are variables that hold memory addresses. They are more flexible and powerful than references, but also more prone to error. Pointers can be reassigned to point to different variables during their lifetime. Pointers can be incremented or decremented (pointer arithmetic), allowing us to iterate through an array of data. Pointers can be set to  NULL  or  nullptr , representing a pointer that doesn't point to anything. Pointers can have multiple levels of indirection, such as pointer to pointer ( int **p ). Here's an example of using pointers: int a = 5 ; int * p = & a ; // p points to a * p = 10 ; // a is now 10 p = nullptr ; // p does not point to anything now References , on the other hand, are like "alias" for another variable. They are safer and easier to use than pointers, but less flexible. References must be