Server Side Programming Articles - Page 1872 of 2650

Thread functions in C/C++

Ayush Gupta
Updated on 02-Mar-2020 11:16:59

2K+ Views

In this tutorial, we will be discussing a program to understand thread functions in C/C++.Thread functions allow users to implement concurrent functions at the same time, which can either be dependent on each other for execution or independent.Example#include #include #include void* func(void* arg){    //detaching the current thread    pthread_detach(pthread_self());    printf("Inside the thread");    pthread_exit(NULL); } void fun(){    pthread_t ptid;    //creating a new thread    pthread_create(&ptid, NULL, &func, NULL);    printf("This line may be printed before thread terminates");    if(pthread_equal(ptid, pthread_self())       printf("Threads are equal");    else       printf("Threads are ... Read More

Templates and Static variables in C++

Ayush Gupta
Updated on 02-Mar-2020 11:13:22

604 Views

In this tutorial, we will be discussing a program to understand templates and static variables in C++.In case of function and class templates, each instance of the templates has its own local copy of the variables.Example Live Demo#include using namespace std; template void fun(const T& x){    static int i = 10;    cout

Template Specialization in C++ Program?

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

184 Views

In this tutorial, we will be discussing a program to understand Template specialization in C++.Standard functions like sort() can be used with any data types and they behave the same with each of them. But if you want to set a special behaviour of the function for a particular data type (even user defined), we can use template specialization.Example Live Demo#include using namespace std; template void fun(T a) {    cout

Swapping of subranges from different containers in C++

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

99 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

618 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

651 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

634 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

852 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

262 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

251 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

Advertisements