Found 26504 Articles for Server Side Programming

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

977 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

385 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

How to delete last element from a set in C++

Arnab Chakraborty
Updated on 17-Dec-2019 13:05:10

1K+ Views

Suppose we have one STL set in C++. There are few elements. We have to delete the last element from that set. So if the elements are like [10, 41, 54, 20, 23, 69, 84, 75], then the set will be like [10 20 23 41 54 69 75 84], and last element is 84. We will see the C++ code to delete last element from set.Example Live Demo#include #include using namespace std; void display(set my_set){    for (auto it = my_set.begin(); it != my_set.end(); ++it)    cout

How to delete last element from a map in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:55:24

624 Views

Here we will see how to delete the last element from C++ STL map. The map is the hash table based data type, it has key and value. We can use the prev() method to get last element, and erase() function to delete as follows.Example Live Demo#include #include using namespace std; int main() {    map my_map;    my_map["first"] = 10;    my_map["second"] = 20;    my_map["third"] = 30;    cout

Advertisements