Found 7197 Articles for C++

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

726 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

158 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

255 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

798 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

448 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

Input Iterators in C++

Ayush Gupta
Updated on 01-Apr-2020 06:16:13

373 Views

In this tutorial, we will be discussing a program to understand input iterators in C++.Input iterators are one of the five iterators in STL being the weakest and simplest of all. They are mostly used in serial input operations where each value is read one and then the iterator moves to the next one.Example Live Demo#include #include using namespace std; int main(){    vector v1 = { 1, 2, 3, 4, 5 };    //declaring iterator    vector::iterator i1;    for (i1 = v1.begin(); i1 != v1.end(); ++i1) {       //looping over elements via iterator       cout

Print Immutable Linked List in Reverse in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:19:35

464 Views

Suppose we have an immutable linked list, we have to print out all values of each node in reverse with the help of the following interface −ImmutableListNode − This is an interface of an immutable linked list, we are given the head of the list.We have to use the following functions to access the linked list −ImmutableListNode.printValue() − This will print value of the current node.ImmutableListNode.getNext() −This will return the next node.So if the list is like: [0, -4, -1, 3, -5], then the output will be [-5, 3, -1, -4, 0]To solve this, we will follow these steps −Define ... Read More

Get Equal Substrings Within Budget in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:16:56

197 Views

Suppose we have given two strings s and t of the same length. We want to change s to t. Changing the i-th character of s to i-th character of t will assign cost as |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters. We have also given an integer maxCost. We have to find the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost.So if the input is like s = “abcd” ... Read More

Advertisements