Found 7346 Articles for C++

Structure vs class in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

158 Views

In C++ the structure and class are basically the same. But there are some minor differences. These differences are like below.The class members are private by default, but members of structures are public. Let us see these two codes to see the differences.Example Code Live Demo#include using namespace std; class my_class {    int x = 10; }; int main() {    my_class my_ob;    cout

Functors in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:25

267 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.Example Code Live Demo#include #include using namespace std; int square(int x) {    return x*x; //return square of x } int main() {    int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};    transform(data, data+10, data, square);    for (int i = 0; i

Return from void functions in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:25

17K+ 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 returnA void function cannot return any values. But we can use the return statement. It indicates that the function is terminated. It increases the readability of code.Example Code Live Demo#include using namespace std; void my_func() {    cout

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

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

In C++, Null is defined as 0. Null or 0 is an integer.In case of a pointer, we can assign a pointer p as −Float* p = NULL; Float* p = 0; Float* p = nullptr;3 of them will produce the same result. null ptr is a keyword introduced in C++11 as a replacement to NULL.

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

Samual Sam
Updated on 30-Jul-2019 22:30:25

8K+ 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 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.

Double Pointer (Pointer to Pointer) in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

10K+ Views

A pointer is used to store the address of variables. So, when we define a pointer to pointer, the first pointer is used to store the address of the second pointer. Thus it is known as double pointers.AlgorithmBegin    Declare v of the integer datatype.       Initialize v = 76.    Declare a pointer p1 of the integer datatype.    Declare another double pointer p2 of the integer datatype.    Initialize p1 as the pointer to variable v.    Initialize p2 as the pointer to variable p1.    Print “Value of v”.       Print the value ... Read More

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

Samual Sam
Updated on 30-Jul-2019 22:30:25

523 Views

In modern C++ [11,14,…] a vector is initialized in the following waystd::vector vec = {1,2,3};AlgorithmBegin    Initialize the vector v.    Using accumulate, sum up all the elements of the vector v is done.    Print the result. End.Here is a simple example of sum up the elements of a vector:Example Live Demo#include #include #include using namespace std; int main() {    vector v = {2,7,6,10};    cout

Ways to copy a vector in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

10K+ Views

There are different ways to copy a vector in C++.1) std::copystd:: copy is inbuilt to copy the elements from one vector to another.Syntaxstd::copy(first_iterator_o, last_iterator_o, back_inserter()): first_iteratot_0 = First iterator of first vector. last_iteratot_0 = Last iterator of first vector. back_inserter() = To insert values from back.AlgorithmBegin    Declare v1 of vector type.       Initialize some values into v1 vector in array pattern.    Declare v2 of vector type.    Call copy(v1.begin(), v1.end(), back_inserter(v2)) to copy all    elements of v1 to v2.    Print “v1 vector elements are :”.    for (int i=0;iRead More

Sorting a vector in descending order in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

17K+ Views

Sorting a vector in C++ can be done by using std::sort(). It is defined in header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintain the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement.Sorting a vector in descending order can be done by using std::greater ().AlgorithmBegin    Declare v of vector type.       Initialize some values into v in array pattern.    Print “Elements before sorting”.    for (const auto &i: v)       print all the values of variable i.    Print “Elements after ... Read More

Sorting a vector in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

16K+ Views

Sorting a vector in C++ can be done by using std::sort(). It is defined in header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement.AlgorithmBegin    Decalre v of vector type.       Initialize some values into v in array pattern.    Print “Elements before sorting”.    for (const auto &i: v)       print all the values of variable i.    Print “Elements after sorting”.    Call sort(v.begin(), v.end()) function to sort all the elements of ... Read More

Advertisements