Found 7347 Articles for C++

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

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

2K+ Views

Here we will see how to sort an array using STL functions in C++. So if the array is like A = [52, 14, 85, 63, 99, 54, 21], then the output will be [14 21 52 54 63 85 99]. To sort we have one function called sort() present in the header file . The code is like below −Example Live Demo#include #include using namespace std; int main() {    int arr[] = {52, 14, 85, 63, 99, 54, 21};    int n = sizeof(arr) / sizeof(arr[0]);    cout

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

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

780 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

419 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

273 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

1K+ 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 17-Dec-2019 13:07:46

3K+ Views

Here we will see how to find the maximum element. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then max element is 96. We can use the max_element() function present in algorithm.h header file to get the maximum element.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 delete last element from a set in C++

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

785 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

470 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

461 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 17-Dec-2019 12:50:06

382 Views

Here we will see how to delete one element from set by passing the value as argument. So if the set is like {10, 20, 30, 50, 60, 80, 90, 100, 120, 200, 500}, and we want to delete 90, it will be: {10, 20, 30, 50, 60, 80, 100, 120, 200, 500}In set each element can occur only once and they are arranged. The value of the element cannot be modified when it is added, so this is immutable. Though we can add or remove elements from it.We can use the erase() method to do this task.Example Live Demo#include #include ... Read More

Advertisements