Server Side Programming Articles - Page 1871 of 2646

Swapping of subranges from different containers in C++

Ayush Gupta
Updated on 02-Mar-2020 11:05:38

112 Views

In this tutorial, we will be discussing a program to understand swapping of subranges of different containers in C++.For this we will be provided with vectors and lists, and we need to swap some of their elements.Example Live Demo#include #include #include #include using namespace std; int main(){    vector v = { -10, -15, -30, 20, 500 };    list lt = { 10, 50, 30, 100, 50 };    swap_ranges(v.begin(), v.begin() + 3, lt.begin());    for (int n : v)       cout

How to sum two integers without using arithmetic operators in C/C++ Program?

Ayush Gupta
Updated on 02-Mar-2020 11:02:56

634 Views

In this tutorial, we will be discussing a program to understand how to sum two integers without using arithmetic operators in C/C++.For adding two integers without using arithmetic operators, we can do this with either using pointers or using bitwise operators.ExampleUsing pointers#include using namespace std; int sum(int a, int b){    int *p = &a;    return (int)&p[b]; } int main() {    int add = sum(2,3);    cout

How to store Data Triplet in a Vector in C++?

Ayush Gupta
Updated on 02-Mar-2020 10:56:12

683 Views

In this tutorial, we will be discussing a program to understand how to store a Data triplet in a vector in C++.To store three elements in a single cell of a vector we will creating a user defined structure and then make a vector from that user defined structure.Example Live Demo#include using namespace std; struct Test{    int x, y, z; }; int main(){    //creating a vector of user defined structure    vector myvec;    //inserting values    myvec.push_back({2, 31, 102});    myvec.push_back({5, 23, 114});    myvec.push_back({9, 10, 158});    int s = myvec.size();    for (int i=0;i

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

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

678 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

920 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

277 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

264 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

365 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

332 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

291 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

Advertisements