Found 7197 Articles for C++

How to sort a Vector in descending order using STL in C++?

Ayush Gupta
Updated on 21-Feb-2025 16:27:08

613 Views

The problem is to sort a vector in descending order using C++'s Standard Template Library(STL). Sorting in descending order means rearranging the elements of the vector so that the largest elements come first, followed by smaller elements, all the way down to the smallest element at the end of the vector. Let's say we have the following vector of integers: vector v = {4, 2, 9, 1, 7}; We want to sort this vector so that the elements are arranged in descending order: v = {9, 7, 4, 2, 1}; In this article, we will show you how ... Read More

How to reverse a Vector using STL in C++?

Nishu Kumari
Updated on 30-Jan-2025 14:41:48

818 Views

Reversing a vector's elements is a common task in many programs, whether for sorting, displaying, or other reasons. C++ provides several ways to reverse a vector. Following is an example of creating a vector using C++. For the vector: V = {1, 2, 3, 4, 5} Reversed Output: {5, 4, 3, 2, 1} For the vector: V = {22, 23, 5, 6, 34} Reversed Output: {34, 6, 5, 23, 22} Approaches to Reverse a Vector We'll cover the following methods for reversing the element in the Vector. Using std::reverse Using a Simple Loop Using std::reverse_copy Using a ... Read More

How to restrict dynamic allocation of objects in C++?

Ayush Gupta
Updated on 02-Mar-2020 10:47:03

253 Views

In this tutorial, we will be discussing a program to understand how to restrict dynamic allocation of objects in C++.For this we will be keeping the new operator function private so that objects cannot be created using it dynamically.Example Live Demo#include using namespace std; class Test{    //making new operator private    void* operator new(size_t size);    int x;    public:    Test() { x = 9; cout

How to quickly swap two arrays of the same size in C++?

Ayush Gupta
Updated on 02-Mar-2020 10:41:17

240 Views

In this tutorial, we will be discussing a program to understand how to quickly swap two arrays of same size in C++.For this we will be using a quick method called std::swap() for swapping the elements of the two given arrays.Example Live Demo#include #include using namespace std;    int main (){    int a[] = {1, 2, 3, 4};    int b[] = {5, 6, 7, 8};    int n = sizeof(a)/sizeof(a[0]);    swap(a, b);    cout

How to print a semicolon(;) without using semicolon in C/C++?

Ayush Gupta
Updated on 02-Mar-2020 10:38:18

330 Views

In this tutorial, we will be discussing a program to understand how to print a semicolon(;) without using a semicolon in /C++.This can be done in two possible ways, either by using the ascii value of semicolon or using user-defined macros for the same.Example Live DemoUsing putchar() method#include int main(){    //ASCII value of semicolon is equal to 59    if (putchar(59)){    }    return 0; }Output;Example Live DemoUsing Macros :#include #define POINT printf("%c",59) int main(){    if (POINT) {    } }Output;

How to join two Vectors using STL in C++?

Ayush Gupta
Updated on 02-Mar-2020 10:27:32

284 Views

In this tutorial, we will be discussing a program to understand how to join two given vectors using STL library in C++.To join two given vectors we would be using the set_union() method from the STL library.Example Live Demo#include using namespace std; int main(){    //collecting the vectors    vector vector1 = { 1, 45, 54, 71, 76, 12 };    vector vector2 = { 1, 7, 5, 4, 6, 12 };    sort(vector1.begin(), vector1.end());    sort(vector2.begin(), vector2.end());    cout

deque::at() and deque::swap() in C++ STL

Sunidhi Bansal
Updated on 02-Mar-2020 09:33:32

252 Views

In this article we are going to discuss the deque::at() and deque::swap() function in C++ STL function syntax, working and its return values.What is deque::at() and deque::swap() function in STL?Deque or Double ended queues are as name suggests, sequence containers which can be expanded or contracted at both the ends. The user can easily insert data from any of the ends and similarly delete data from any of the ends. They are similar to vectors, but the only difference is that unlike vectors, contiguous storage allocation may not be guaranteed. Still Deque is more efficient in case of insertion and ... Read More

list pop_back() function in C++ STL

Sunidhi Bansal
Updated on 02-Mar-2020 09:29:35

2K+ Views

In this article we will be discussing the working, syntax and examples of list::pop_back() function in C++.What is a List in STL?List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.What is list::pop_back()?list::pop_back() ... Read More

list merge() function in C++ STL

Sunidhi Bansal
Updated on 02-Mar-2020 09:25:51

3K+ Views

In this article we will be discussing the working, syntax and examples of list::merge() function in C++.What is a List in STL?List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.What is list::merge()?list::merge() ... Read More

list erase() function in C++ STL

Sunidhi Bansal
Updated on 02-Mar-2020 09:16:59

1K+ Views

In this article we will be discussing the working, syntax and examples of list::erase() function in C++.What is a List in STL?List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.What is list::erase()?list::erase() ... Read More

Advertisements