Wednesday, November 15, 2023

Handling large data sets with C++ STL vectors

 Vectors in C++ standard templat library is an dynamic container which dynamicaly allocate memory as size is increased.When  we deal with a larger data set, freaquent dynamic allocations can reduce the performance of our program.

By using the reserve() function judiciously, we can preallocate memory, reducing the need for frequent reallocations as elements are added. This not only minimizes the overhead associated with dynamic resizing but also enhances contiguous memory allocation, which can have positive effects on cache locality and iteration performance.

Let's consider an example where we have a vector that will be populated with a significant number of elements, but we also need fast random access to these elements. In such a scenario, we might strike a balance by allocating a slightly larger initial capacity than the expected dataset size using reserve(). This reduces the frequency of reallocations, optimizing memory usage.

However, it's crucial to be aware that this approach comes with a trade-off. Allocating more memory than strictly necessary increases the initial memory footprint, and if memory is a critical constraint, we might need to explore alternative solutions. 

In summary, the key is to carefully assess the specific requirements of the application and strike a balance between minimizing memory usage and optimizing for access patterns based on the characteristics of the dataset.

Basic C++ interview questions for beginners


Check if you can answer following basic questions.

1. Explain the difference between deep copy and shallow copy in C++.

2. What is a constructor, and how does it differ from a destructor in C++?

3. What is the purpose of the `virtual` keyword in C++?

4. Explain the difference between `new` and `malloc()` in C++.

5. What is a smart pointer in C++? How does it differ from a raw pointer?

6. Explain the concept of operator overloading in C++

Friday, July 23, 2021

Split a string by a given delimiter

 I was really amazed to find out that C++ string class has no split method. There are plenty of accasions I needed to split a string by a given delimiter. Lets see how to split a string and put substrings in a to vector. 

If you think incrementing varable start is ugly, you can use function find_first_not_of for it

Wednesday, June 30, 2021

modify files using sed in linux

 sed is a powerfull tool and you can find all details from here

delete a line containing a substring ("pattern")

sed -i '/pattern/d' file
replace a string
sed -e s/pattern//g -i file
using i.backup will keep a backup file of original and it is safer.  

sed -e "s/pattern//g" -i.backup file




Get parent directory name of shell script

 pwd | rev | cut -d/ -f1 | rev


lets see arguments for cut used with above command

spliting delimeter is given to -d, in this case \

required argument is given to -f, in this case 1


rev is used to reverse the string because parent directory name is the first argumet from reverse of pwd.

Get substring of a string in shell

 I wanted to get a only file names from the output of  command find . -maxdepth 1 -type d

This returns a strings contains "./" at the begining.

./1-case

./2-case

./3-case

./4-case

./5-case

./6-case


to remove first two characters i used command cut. as follows

echo "./1-case" | cut -c3-


-c means return characters

3- means from 3rd character to end


other examples

-c3 will return only 3rd character

-c3-4 will return characters form 3rd to 4th


 



Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...