Server Side Programming Articles - Page 1845 of 2650

MakeFile in C++ and its applications

Ayush Gupta
Updated on 01-Apr-2020 06:46:17

270 Views

In this tutorial, we will be discussing a program to understand MakeFile in C++ and its applications.The task is to break the entire program with MakeFile. It is usually done by making .cpp files and .h files with all the classes/functionalities and link them together.Examplemain.cpp#include #include "function.h" using namespace std; //main execution program int main(){    int num1 = 1;    int num2 = 2;    cout

Kruskal’s Minimum Spanning Tree using STL in C++

Ayush Gupta
Updated on 01-Apr-2020 06:43:15

509 Views

In this tutorial, we will be discussing a program to understand Kruskal’s minimum spanning tree using STL in C++.For this, we will be provided with a connected, undirected and weighted graph. Our task is to calculate the Minimum spanning tree for the given graph.Example Live Demo#include using namespace std; typedef pair iPair; //structure for graph struct Graph{    int V, E;    vector< pair > edges;    Graph(int V, int E){       this->V = V;       this->E = E;    }    void addEdge(int u, int v, int w){       edges.push_back({w, {u, v}});    } ... Read More

Lower bound in C++

Ayush Gupta
Updated on 01-Apr-2020 06:41:03

327 Views

In this tutorial, we will be discussing a program to understand the lower bound in C++.lower_bound() method in C++ is used to return the very first number in the container object which is not less than the given value.Example Live Demo#include int main(){    std::vector v{ 10, 20, 30, 40, 50 };    std::cout

Iseek() in C/C++ to read the alternate nth byte and write it in another file

Ayush Gupta
Updated on 01-Apr-2020 06:38:41

749 Views

In this tutorial, we will be discussing a program to understand how to read the alternate nth byte and write it in another file.For this, we will be provided with two .txt files. Our task is to write the contents from one file to another file using Iseek() which is used to change the pointer of the file descriptor.Example#include #include #include #include void func(char arr[], int n){    int f_write = open("start.txt", O_RDONLY);    int f_read = open("end.txt", O_WRONLY);    int count = 0;    while (read(f_write, arr, 1)){       if (count < n) ... Read More

Iterator Invalidation in C++ program

Ayush Gupta
Updated on 01-Apr-2020 06:47:34

164 Views

In this tutorial, we will be discussing a program to understand iterator invalidation in C++.While iterating over the elements of a container object, sometimes it can become invalidated if we don’t apply bound checks. This mainly occurs due to the change in the shape and size of the container object.Example Live Demo#include using namespace std; int main() {    //declaring a vector    vector v{1, 5, 10, 15, 20};    //changing vector during execution    //which will cause bound invalidation    for (auto it=v.begin();it!=v.end();it++)       if ((*it) == 5)          v.push_back(-1);    for (auto ... Read More

Internal details of std::sort() in C++

Ayush Gupta
Updated on 01-Apr-2020 06:31:34

261 Views

In this tutorial, we will be discussing a program to understand the internal details of std::sort() in C++.std::sort() function is used to sort down an array using a comparison of the elements. If we look at the in-depth functionality of std::sort() it uses IntroSort algorithm to sort the elements of a container object.Example Live Demo#include using namespace std; int main(){    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};    int n = sizeof(arr)/sizeof(arr[0]);    sort(arr, arr+n);    cout

Integer literal in C/C++ (Prefixes and Suffixes)

Ayush Gupta
Updated on 01-Apr-2020 06:29:25

2K+ Views

In this tutorial, we will be discussing a program to understand integer literal in C/C++ (Prefixes and suffixes).Integer literals are literals for integer values directly represented in the source code. Further, they are of two types −Prefixes − Prefixes denotes the base of the value. For example, 0x10 indicates hexadecimal value with 0x.Suffixes − Suffixes denotes the type of the value. For example, 8465484156155LL denotes a long long integer.Example Live Demo#include using namespace std; int main(){    //prefixes    cout

INT_MAX and INT_MIN in C/C++ and Applications

Ayush Gupta
Updated on 01-Apr-2020 06:30:53

3K+ Views

In this tutorial, we will be discussing a program to understand INT_MAX and INT_MIN in C/C++.INT_MIN and INT_MAX are macros that are defined to set the minimum and maximum value for a variable/element.Example Live Demo#include int main(){    printf("%d", INT_MAX);    printf("%d", INT_MIN);    return 0; }Output2147483647 -2147483648ApplicationCalculating MIN value in an arrayExample Live Demo#include //calculating minimum element in an array int compute_min(int arr[], int n){    int MIN = INT_MAX;    for (int i = 0; i < n; i++)    MIN = std::min(MIN, arr[i]);    std::cout

Insertion sort using C++ STL

Ayush Gupta
Updated on 01-Apr-2020 06:20:53

812 Views

In this tutorial, we will be discussing a program to understand the insertion sort using C++ STL.In this we use std::upper_bound to find the element at the wrong position, then rotate the unsorted part of the array to make it sorted.Example Live Demo#include //function to perform insertion sort void insertionSort(std::vector &vec){    for (auto it = vec.begin(); it != vec.end(); it++){       auto const insertion_point =       std::upper_bound(vec.begin(), it, *it);       std::rotate(insertion_point, it, it+1);    } } //printing the array void print(std::vector vec){    for( int x : vec)    std::cout

Insertion and Deletion in STL Set C++ program

Ayush Gupta
Updated on 01-Apr-2020 06:18:48

459 Views

In this tutorial, we will be discussing a program to understand the insertion and deletion in STL set in C++.The set is a container element. The properties that make it unique is that it can only contain unique elements and they can be looped over in a sorted manner.ExampleInsertion Live Demo#include #include using namespace std; int main(){    set st;    //declaring iterators    set::iterator it = st.begin();    set::iterator it1, it2;    pair< set::iterator,bool> ptr;    //inserting a single element    ptr = st.insert(20);    if (ptr.second)       cout

Advertisements