The array operator provides the direct access to array elements using their index. What is Array Operator in C++? The arrow operator in C++ is also known as the member access operator, which is used to access a member of a class, structure, or union with the help of a pointer to an object. The arrow operator allows you to directly access the member, unlike the dot operator, which first dereferences the pointer and then uses the dot operator to access it. So instead of using (*pointer).member, you can directly use pointer->member. Syntax Here is the syntax to access array ... Read More
A class in C++ has the following access modifiers: public, private, and protected, which contain the corresponding class members. The protected members in a class are similar to private members as they cannot be accessed from outside the class, but they can be accessed by derived classes or child classes, while private members cannot. In this article, we will see various examples of how to access protected members in C++ and how it is different from private members. Accessing Protected Variable in C++ In this example, we have initialized a protected variable value in the parent class. We ... Read More
An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0th index element. So *(arr+i) means the element is at i distance from the first element of the array. So, array index starts from 0 as initially i is 0 which means the first element of the array. In this article, we will see an example of C and C++ code to understand the reason why an array starts from index 0. Why Array Index Starts from Zero? The array index starts from zero as it provides better efficiency and ... Read More
In C++, both structures (struct) and classes (class) are user-defined data types, where they both give access to group different data elements (variables) and functions together. However, they still possess a few differences between them. In this article, we will see and go through its differences. Structure (struct) The struct is a user-defined data type, which allows the grouping of variables of different data types, with the members being public by default. This is commonly used to represent simple data structures, where encapsulation is not necessary. A struct can contain data members and member functions, but its primary use is ... Read More
When we are dealing with large directories, managing files and folders is often required in listing them in a specific order. One common approach to list out the files/folders is sorting items by their names, which makes navigation and processing much easier. Python simplifies this task with built-in modules such as os and pathlib, which allow developers to fetch and organize directory contents with just a few lines of code. In this article, we will go through the different methods to retrieve and sort directory listings alphabetically with clean and flexible solutions for our file-handling needs. Using os.listdir() with sorted() ... Read More
In few scenarios, we need to change the extension of a file programmatically such as renaming a .txt file to .md or converting .csv to .json. Python provides different ways to do this using the os and pathlib modules. In this article, we’ll explore how to change a file’s extension using both approaches. Using os.path.splitext() The os.path.splitext() method of the os module in Python is used to split the file name into the name and extension. We can use this method to strip off the old extension and add the new extension. Example In this example, we are using the os.path.splitext() ... Read More
Here, we are going to learn how we can calculate the range of the different C++ data types such as signed data types (int, char, float, etc.) and unsigned data types (unsigned char, unsigned int, unsigned float, etc.). Calculating Range of Signed Data Types In C++, signed data types are used to represent both positive and negative integer values. So, to display their range, we use the following method − Calculate the total number of bits, multiply the sizeof bytes by 8. Calculate -2^(n-1) for minimum range ... Read More
In Python, when we are working with directory structures, it is necessary to check whether a given directory contains any other directories within it or not. This process of checking is useful when performing batch operations, cleaning up folders, or traversing file systems. Python provides several built-in ways to perform this check effectively. In this article, we will go through different methods using both os and pathlib modules to determine if a directory contains any subdirectories. Using os.listdir() and os.path.isdir() The os.listdir() method is the straightforward method to detect subdirectories to list directory contents, and when it is combined with ... Read More
Both vectors and arrays are used to store collections of elements, but they differ significantly in how they manage their memory and flexibility. C++ std::vector A vector is a dynamic array that can be resized automatically when elements are added or removed. It is a part of the C++ STL and provides more flexibility than a static array. Example In the following example, we will demonstrate the usage of the vector in C++ − #include #include using namespace std; int main() { vector > v { { 4, 5, 3}, {2, 7, 6}, {3, 2, 1, 10} }; cout
In this article, we will see why we should avoid the std::endl while printing lines into the console or a file. We use std::endl to create a new line after the current line. For a few lines of I/O operations, it is not causing any problems. However, a large number of I/O tasks decreases performance. Why We Avoid Using std::endl There are the following reasons to avoid endl: The endl is used to create new lines, but it does not send to the new line only; after sending the cursor to the next line, it ... Read More