Found 7197 Articles for C++

When is copy constructor called in C++?

Revathi Satya Kondra
Updated on 26-May-2025 16:41:09

1K+ Views

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to Initialize one object from another of the same type. Copy an object to pass it as an argument to a function. Copy an object to return it from a function. If a copy constructor is not defined in a class, the compiler itself defines one. If the class has pointer variables and has some dynamic ... Read More

Functors in C++

Revathi Satya Kondra
Updated on 26-May-2025 16:50:30

376 Views

The functors are the function objects in C++. The functor allows an instance object of some class to be called as if it were an ordinary function. Let us consider a function that takes one argument. We can use this function as function object to do some task on a set of data. The Functors are widely used in STL algorithms like transform(), sort(), etc. Functor vs Regular Function: Need of Functor? Imagine we have a function that takes only one argument, like this: int increment(int x) { return x + 1; } Now, what ... Read More

Return from void functions in C++

Revathi Satya Kondra
Updated on 10-Jun-2025 13:16:07

18K+ Views

The void functions are called void because they do not return anything. "A void function cannot return anything" this statement is not always true. From a void function, we cannot return any values, but we can return something other than values. Some of them are like below. A void function can return A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated.ExampleThe following example demonstrates a void function with the return statement: #include using namespace std; void my_func() { cout

What should we assign to a C++ pointer: A Null or 0?

Revathi Satya Kondra
Updated on 12-Jun-2025 14:25:46

1K+ Views

In C++, a pointer stores the address of another variable, which means that the pointer itself does not contain a value of its own. However, you can assign a null value or a 0 to a pointer, in which case the pointer will not point to the address of any other variable. NULL: It is special constant that indicates the pointer does not point to any valid memory location i.e., an Empty Pointer. 0: It is an older way to represent a null pointer to indicate that the pointer points to nothing. nullptr: It is introduced in C++11, it ... Read More

What is the size of a pointer in C/C++?

Revathi Satya Kondra
Updated on 27-May-2025 17:14:33

10K+ Views

The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor, for example for a 32 bit computer the pointer size can be 4 bytes and for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed. It is common to all data types like int *, float * etc. Depending on the system architecture, pointer size may vary. The following table shows the pointer size based on the ... Read More

What is the easiest way to initialize a std::vector with hardcoded elements in C++?

Revathi Satya Kondra
Updated on 12-Jun-2025 13:45:58

661 Views

In C++, a std::vector is a container that is a part of the STL (Standard Template Library). It enables dynamic arrays which can adjust their size automatically. Depending on the requirements and version of C++ being used, there are many ways to initialize a std::vector with hardcoded elements. Initializing a std::vector with Hardcoded Elements Some of the approaches to initialize a std::vector with hardcoded elements in C++: Using Initializer List Using assign() Function Using accumulate() function Initializing Vector Using Initializer List An initializer list is a ... Read More

Ways to copy a vector in C++

Nishu Kumari
Updated on 30-Jan-2025 14:45:41

13K+ Views

In C++, vectors are commonly used to store dynamic collections of data. Sometimes, we need to copy a vector to manipulate data without affecting the original. In this article, we'll show you the easiest ways to copy a vector in C++, along with examples to show you how it works. Ways to copy a vector in C++ There are different ways to copy a vector in C++, and each method has its own use. We'll cover the following approaches: Using std::copy Using assign() Method By assignment "=" ... Read More

Sorting a vector in C++

Nishu Kumari
Updated on 30-Jan-2025 14:46:25

19K+ Views

Sorting a vector in C++ means arranging its elements in a specific order, like ascending or descending. This is a common task when you need to organize data efficiently. C++ provides different ways to sort a vector. In this article, we will look at different ways to sort a vector in C++. Let's look at this example to better understand it: For the vector: V = {5, 3, 8, 1, 2} Sorted Output: {1, 2, 3, 5, 8} For the vector: V = {22, 23, 5, 6, 34} Sorted Output: {5, 6, 22, 23, 34} Approaches to ... Read More

Passing a vector to constructor in C++

Revathi Satya Kondra
Updated on 26-May-2025 17:24:41

1K+ Views

In C++, you can pass a std::vector to a class constructor to create a list of values when the object is created. So that the object can store or work with a list of values right from the beginning. Why do You Pass a Vector to a Constructor? Passing a Vector to a Constructor allows the object to be initialized with data at the time of creation. When you pass a vector to a function, it simplifies the code by eliminating the need to initialize the function parameters, as the vector ... Read More

How to sum up elements of a C++ vector?

Vishesh Raina
Updated on 01-Aug-2024 12:27:52

27K+ Views

In this article, we will understand how to sum the elements present inside a vector in C++. A vector is a dynamically allocated array with variable size. The sum of elements of a vector can be calculated in a number of ways, and we will discuss two such ways. Problem Statement Given a non-empty vector of integers, calculate the sum of all elements of the vector using multiple approaches in C++. Examples The following examples give the input and output of some test cases : Input vector vec={1, 3, 4, 5, 2, 3} Output ... Read More

Advertisements