Skip to main content

Posts

rule of 3 5 6 in C++

  Rule of 3 (C++98/03) In C++98/03, the Rule of 3 states that if a class defines one of the following special member functions, it should likely define all three: Destructor : Cleans up any dynamically allocated memory or other resources. Copy Constructor : Creates a new object as a copy of an existing object. Copy Assignment Operator : Assigns the contents of one object to another. Rule of 5 (C++11 and beyond) With the introduction of C++11, the Rule of 5 builds upon the Rule of 3 and adds two more special member functions: Move Constructor : Transfers resources from one object to another without copying. Move Assignment Operator : Transfers resources from one object to another using move semantics rather than copying. Rule of 6 (Extended rule, but not part of the C++ standard) In some discussions, you may encounter references to the Rule of 6 , which is an extension of the Rule of 5. It specifically addresses the issue of defining default constructor alongside the ot...

what happens when unordered_map keeps increasing in size?

 When a std::unordered_map keeps increasing in size, it dynamically manages its internal storage by resizing and rehashing the underlying hash table. This process helps to maintain efficient average-case performance for operations like insertions , lookups , and deletions . Here's what happens in more detail as the container grows: 1. Load Factor and Rehashing The load factor is a key concept when it comes to how an unordered_map handles growth. It is defined as the ratio of the number of elements ( n ) in the container to the number of buckets ( b ) in the hash table: bash Copy load factor = n / b As the unordered_map grows, the load factor increases. If it exceeds a certain threshold (typically around 1.0 ), the map will rehash to maintain efficient performance. This threshold is dependent on the implementation, but most implementations will rehash when the load factor exceeds 1.0 (i.e., when the number of elements exceeds the number of buckets). 2. Resizing and Rehash...

std::unordered_map problems

 The std::unordered_map in C++ is a powerful container that provides fast access to key-value pairs with average constant-time complexity ( O(1) ) for lookups, inserts, and deletions. However, like any data structure, it has its potential issues, trade-offs, and limitations. Here are some common problems and limitations associated with std::unordered_map : 1. Unpredictable Iteration Order Problem: The order of elements in an unordered_map is not guaranteed. It depends on the internal hash table implementation and the distribution of hash values. Impact: This makes std::unordered_map unsuitable for use cases where the order of elements matters, such as when you need to traverse the elements in a specific order. Example: cpp Copy std::unordered_map< int , std::string> map; map[ 1 ] = "one" ; map[ 2 ] = "two" ; map[ 3 ] = "three" ; // The order in which elements are iterated is not guaranteed for ( auto & entry : map) { std::cout ...

C++11 faster than C++03

  C++11 faster than C++03 : Why and How C++11 introduced several features and improvements that can make code written in C++11 faster than code written in C++03. Here are some key reasons: 1. Move Semantics C++11 introduced move semantics, which allow the resources of temporary objects to be "moved" rather than copied. This reduces unnecessary deep copies of objects, especially for classes that manage resources like dynamic memory, file handles, or sockets. This can lead to significant performance improvements in applications that create and destroy many temporary objects. 2. Rvalue References Move semantics are enabled by rvalue references (denoted by &&), which allow developers to distinguish between objects that can be moved (temporaries) and those that cannot. This optimization minimizes the overhead of copying large objects. 3. Improved Concurrency Support C++11 introduced a standardized memory model and threading library (<thread>, <mutex>, <fut...

my bashrc

  #Source global defs if [ -f /etc/bashrc ]; then . /etc/bashrc fi if [ -e “$HOME”/.bash_aliases ]; then source “$HOME”/.bash_aliases fi #User specific env if ! [[ “$PATH” =~”$HOME/.local/bin:$HOME/bin:” ]] then PATH=“$HOME/.local/bin:$HOME/bin:$PATH” fi export PATH ORACLE_HOME=/opt/oracle export ORACLE_HOME ORACLE_SID=OTP01 export ORACLE_SID JAVA_HOME=/usr/lib/jvm/java-1.8.0 export JAVA_HOME alias ll=‘ls -l | more’ alias lt=‘ls -t | more’ alias lsa=‘ls -a | more’ alias lla=‘ls -la | more’ alias lR=‘ls -R | more’ alias ltr=‘ls -ltr | more’ alias ldir=‘ls -l | grep ^d | more’ alias c=‘clear’ alias h=‘history 200’ alias find=‘find 2>/dev/null’ #what does 2>/dev/null does? alias sq=‘sqlplus username/password@schemaname’ alias srcrc=‘source ~/.bashrc’ alias myps=‘ps -aef | grep dagaa | grep -v vscode | more’ #displays processes for dagaa except for vscode alias myps=‘ps -aef | grep dagaa | grep vscode | more’ #displays vscode processes for dagaa   alias gs=‘git status’...

google test for a code that has random number generator

 Writing tests for code that uses a random number generator (RNG) can be tricky, as random behavior introduces non-determinism into your tests. However, you can make testing easier by controlling or "mocking" the randomness. There are several strategies you can use to test code that involves random numbers, particularly when using a testing framework like Google Test . Here are the steps to test code that uses a random number generator: 1. Refactor to Allow Injecting the RNG If possible, refactor your code to allow the random number generator to be injected (dependency injection). This allows you to substitute the random number generator with a mock or predictable RNG during testing. Example Code to Refactor: cpp Copy # include <iostream> # include <random> class RandomGenerator { public : virtual int generate () = 0 ; }; class RealRandomGenerator : public RandomGenerator { public : int generate () override { std::random_de...