Server Side Programming Articles - Page 2008 of 2650

How to insert elements in C++ STL List?

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

538 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

399 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

639 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

How to delete last element from a List in C++ STL

Arnab Chakraborty
Updated on 17-Dec-2019 12:52:36

601 Views

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

How to delete an element from the Set by passing its value in C++

Arnab Chakraborty
Updated on 21-Feb-2025 16:31:34

1K+ Views

In this article, we will explain how to delete an element from a set in C++ by passing its value. A set stores elements in sorted order, where each element is unique and cannot be modified once added. While the values cannot be changed, we can add or remove elements using the erase() function from the C++ Standard Library (STL). For example, if we have a set of integers: {1, 2, 3, 4, 5} and want to remove the element 3, we can use the erase() function. After the operation, the set will be {1, 2, 4, 5}, ... Read More

Check if a key is present in a C++ map or unordered_map

Arnab Chakraborty
Updated on 17-Dec-2019 12:46:43

488 Views

In C++ the Maps and unordered maps are hash tables. They use some keys and their respective key values. Here we will see how to check whether a given key is present in the hash table or not. The code will be like below −Example Live Demo#include #include using namespace std; string isPresent(map m, string key) {    if (m.find(key) == m.end())    return "Not Present";    return "Present"; } int main() {    map my_map;    my_map["first"] = 4;    my_map["second"] = 6;    my_map["third"] = 6;    string check1 = "fifth", check2 = "third";    cout

What is Array Decay in C++? How can it be prevented?

Arnab Chakraborty
Updated on 17-Dec-2019 12:43:45

208 Views

Here we will see what is Array Decay. The loss of type and dimensions of an array is called array decay. It occurs when we pass the array into a function by pointer or value. The first address is sent to the array which is a pointer. That is why, the size of array is not the original one.Let us see one example of array decay using C++ code,Example Live Demo#include using namespace std; void DisplayValue(int *p) {    cout

Advertisements