Found 7197 Articles for C++

Code valid in both C and C++ but produce different output

Arnab Chakraborty
Updated on 17-Dec-2019 13:26:14

123 Views

Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.Example Live Demo#include int main() {    printf("The character: %c, size(%d)", 'a', sizeof('a')); }OutputThe character: a, size(4)Example#include int main() {    printf("The ... Read More

Chrono in C++

Arnab Chakraborty
Updated on 17-Dec-2019 13:23:17

399 Views

In this section we will see what is the Chrono library in C++. This Chrono library is used for date and time. Timers and clocks are different in different systems. So if we want to improve time over precision we can use this library.In this library, it provides precision-neutral concept, by separating the durations and point of time.The duration objects are used to express time span by means of a count like minute, two hours or ten minutes. For example, 30 seconds is represented by a duration consisting of 30 ticks of unit of 1 seconds.Example Live Demo#include #include ... Read More

How to use getline() in C++ when there are blank lines in input?

Arnab Chakraborty
Updated on 17-Dec-2019 13:20:55

961 Views

In C++, we use the getline() function to read lines from stream. It takes input until the enter button is pressed, or user given delimiter is given. Here we will see how to take the new line character as input using the getline() function. Let us see the following implementation to get the idea.Example Live Demo#include using namespace std; int main() {    string str;    int term = 4;    while (term--) {       getline(cin, str);       while (str.length()==0 )       getline(cin, str);       cout

How to traverse a C++ set in reverse direction?

Arnab Chakraborty
Updated on 17-Dec-2019 13:19:02

1K+ Views

Suppose we have a set, then we have to traverse the set in reverse direction. So if the set is like S = [10, 15, 26, 30, 35, 40, 48, 87, 98], then the output will be: 98 87 48 40 35 30 26 15 10.To traverse in reverse order, we can use the reverse_iterator. Here we will use the rbegin() and rend() function to get the begin and end of the reverse iterator.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {10, 15, 26, 30, 35, 40, 48, 87, 98};    set my_set(arr, arr + sizeof(arr) / sizeof(arr[0]));    set::iterator it;    cout

How to sort an Array using STL in C++?

Arnab Chakraborty
Updated on 21-Feb-2025 16:25:55

2K+ Views

The problem is to sort an array using C++'s Standard Template Library (STL). Sorting an array means rearranging its elements in a specific order. In this case, we aim to sort the elements in both ascending and descending order, using the built-in functionalities of the STL. Let's say we have the following array of integers: int arr[] = {4, 2, 9, 1, 7}; The goal is to sort this array so that the elements are ordered from smallest to largest (ascending) and largest to smallest (descending): Ascending: arr = {1, 2, 4, 7, 9} Descending: arr = {9, 7, ... Read More

How to reverse an Array using STL in C++?

Arnab Chakraborty
Updated on 17-Dec-2019 13:15:32

976 Views

Here we will see how to reverse an array using STL functions in C++. So if the array is like A = [10, 20, 30, 40, 50, 60], then the output will be B = [60, 50, 40, 30, 20, 10]. To reverse we have one function called reverse() present in the header file . The code is like below −Example Live Demo#include #include using namespace std; int main() {    int arr[] = {10, 20, 30, 40, 50, 60};    int n = sizeof(arr) / sizeof(arr[0]);    cout

How to insert elements in C++ STL List?

Arnab Chakraborty
Updated on 17-Dec-2019 13:13:46

532 Views

Suppose we have one STL list in C++. There are few elements. We have to insert a new element into the list. We can insert at the end, or beginning or at any position. Let us see one code to get better understanding. To insert at beginning we will use push_front(), To insert at end, we will use push_end() and to insert at any position, we have to use some operations. we have to initialize one iterator, then move that iterator to correct position, then insert into that place using insert() method.Example Live Demo#include #include using namespace std; void display(list my_list){ ... Read More

How to find the sum of elements of an Array using STL in C++?

Arnab Chakraborty
Updated on 17-Dec-2019 13:11:17

384 Views

Here we will see how to find the sum of all element of an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then sum will be: 405. So here we have to use the accumulate() function to solve this problem. This function description is present inside header file.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};    int n = sizeof(arr) / sizeof(arr[0]);    cout

How to find the minimum and maximum element of an Array using STL in C++?

Arnab Chakraborty
Updated on 17-Dec-2019 13:09:59

2K+ Views

Here we will see how to find the maximum and minimum element from an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then max element is 96, and min element is 12. We can use the max_element() function and min_element() function, present in algorithm.h header file to get the maximum and minimum elements respectively.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};    int n = sizeof(arr) / sizeof(arr[0]);    cout

How to find the maximum element of an Array using STL in C++?

Arnab Chakraborty
Updated on 14-Feb-2025 18:04:33

4K+ Views

In C++, one common task is to find the largest number in an array. An array is just a collection of values, and sometimes we need to find the highest value in that collection. For example, consider the array: int arr[] = {11, 13, 21, 45, 8}; In this case, the largest element is 45. Similarly, for the array: int arr[] = {1, 9, 2, 5, 7}; The largest number is 9. In this article, we will show you how to find the largest number in an array using different methods in C++, including some built-in features ... Read More

Advertisements