C++ Articles

Page 292 of 597

Shuffle vs random_shuffle in C++

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 622 Views

Here we will see the Shuffle and random_shuffle in C++. Let us see the random_shuffle first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example#include using namespace std; int myRandomGenerator(int j) {    return rand() % j; } main() {    srand(unsigned(time(0)));    vector arr;    for (int j ...

Read More

Bind function and placeholders in C++

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 2K+ Views

Here we will see the Bind function and the placeholders in C++. Sometimes we need to manipulate the operation of some functions as we need. We can use some default parameters to get some essence of manipulating.In C++11, one new feature is introduced, called the bind function. This helps us to do such manipulating in some easier fashion. To use these features, we have to use header file.Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs.Placeholders are namespaces which detect the position of a ...

Read More

C++ Program to Perform Searching Based on Locality of Reference

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 197 Views

Searching based on locality of reference, depends upon the memory access pattern data elements are reallocated.Here linear searching method is used to search an element.AlgorithmBegin    int find(int *intarray, int n, int item)    intialize comparisons = 0    for i = 0 to n-1       Increase comparisons    if(item == intarray[i])       Print element with its index    break    if(i == n-1)       Print element not found    return -1    Print Total comparisons    For j = i till i>0       intarray[j] = intarray[j-1]       intarray[0] = ...

Read More

Removing an element from C++ std::vector<> by index?

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 502 Views

Remove an element from C++ std::vector by index can be done by following way −Example#include #include using namespace std; int main() {    vector v; //declare vector    //insert elements into vector    v.push_back(-10);    v.push_back(7);    v.push_back(6);    // Deletes the first element (v[0])    v.erase(v.begin() );    for (int i = 0; i < v.size(); i++)       cout

Read More

RAII and smart pointers in C++

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 2K+ Views

RAII in C++RAII (Resource Acquisition Is Initialization) is C++ technique which control the life cycle of resource. It is a class variant and is tied to object life time.It encapsulates several resources into class where resource allocation is done by constructor during object creation and resource deallocation is done by destructor during object destruction.Resource is guaranteed to be held till the object is alive.Examplevoid file_write {    Static mutex m; //mutex to protect file access    lock_guard lock(m); //lock mutex before accessing file    ofstream file("a.txt");    if (!file.is_open()) //if file is not open    throw runtime_error("unable to open file"); ...

Read More

Pointers, smart pointers and shared pointers in C++

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 6K+ Views

PointersPointers are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer = variable name;Functionspointers are used to store address of variable.pointers can have a null value assigned.pointer can be referenced by pass by reference.a pointer has its own memory address and size on the stack.Example#include using namespace std; int main() {    // A normal integer variable    int a = 7;    // A pointer variable that holds address of a.    int *p = &a;    // Value stored is value of variable "a"    cout

Read More

Why do we pass a Pointer by Reference in C++?

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 877 Views

If we need to modify a pointer rather than the object that the pointer is pointing to, we pass a pointer by reference.Here is an example of how to pass a pointer by reference −Example#include using namespace std; void Decrement( int*& d ) {    --d; } int main( void ) {    int a = 26;    int* ptr = &a; // pointer to pass    // print before decrement    cout

Read More

C++ Program to Find the maximum subarray sum using Binary Search approach

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 392 Views

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the ...

Read More

C++ Program to Find Closest Pair of Points in an Array

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 913 Views

This is the program to find closest pair of points in an array.AlgorithmsFor Distance between Closest pointBegin    Declare function Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) to the double datatype.    Declare Minimum to the double datatype.       Initialize Minimum = dist.    for (int i = 0; i < s; ++i)       for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j)          if (Distance(stp[i], stp[j]) < Minimum) then          Minimum = Distance(stp[i], stp[j]).             ...

Read More

Print prime numbers in a given range using C++ STL

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 680 Views

It is the program to print prime numbers in a given range.AlgorithmsBegin    Declare a user define datatype stl.    Declare a vector number to the stl datatype.    Declare variable a to the stl datatype.    Declare vector Prime_Number to the Boolean datatype.       Prime_Number[0] = false.       Prime_Number[1] = false.    Declare b to the integer datatype.       Initialize b = sqrt(a).    for (stl pr=2; pr

Read More
Showing 2911–2920 of 5,962 articles
« Prev 1 290 291 292 293 294 597 Next »
Advertisements