Server Side Programming Articles - Page 1861 of 2650

Counting Inversions using Set in C++ STL

Ayush Gupta
Updated on 16-Mar-2020 10:02:31

248 Views

In this tutorial, we will be discussing a program to count inversions using set in C++ STL.Inversion count is a measure of how near the array is to be completely sorted. If the array is already sorted, the inversion count will be 0.Example Live Demo#include using namespace std; //returning inversion count int get_Icount(int arr[],int n){    multiset set1;    set1.insert(arr[0]);    int invcount = 0; //initializing result    multiset::iterator itset1;    for (int i=1; i

Count the number of 1’s and 0’s in a binary array using STL in C++

Ayush Gupta
Updated on 16-Mar-2020 10:00:27

402 Views

In this tutorial, we will be discussing a program to count the number of 1’s and 0’s in a binary array using STL in C++.For this we will be provided with an array. Our task is to count the number of 0’s and 1’s present in the array.Example Live Demo#include using namespace std; // checking if element is 1 or not bool isOne(int i){    if (i == 1)       return true;    else       return false; } int main(){    int a[] = { 1, 0, 0, 1, 0, 0, 1 };    int n = sizeof(a) / sizeof(a[0]);    int count_of_one = count_if(a, a + n, isOne);    cout

Count smaller elements on right side using Set in C++ STL

Ayush Gupta
Updated on 16-Mar-2020 09:58:45

294 Views

In this tutorial, we will be discussing a program to count smaller elements on right side using set in C++ STL.For this we will be provided with an array. Our task is to construct a new array and add the number of smaller elements on the right side of the current element at its position.Example Live Demo#include using namespace std; void count_Rsmall(int A[], int len){    set s;    int countSmaller[len];    for (int i = len - 1; i >= 0; i--) {       s.insert(A[i]);       auto it = s.lower_bound(A[i]);       countSmaller[i] = ... Read More

Count smaller elements in sorted array in C++

Ayush Gupta
Updated on 16-Mar-2020 09:55:50

294 Views

In this tutorial, we will be discussing a program to count smaller elements in sorted array in C++.In this we will be given a number and our task is to count all the elements present in the sorted array which are smaller than the given number.Example Live Demo#include using namespace std; int countSmaller(int arr[], int n, int x){    return upper_bound(arr, arr+n, x) - arr; } int main(){    int arr[] = { 10, 20, 30, 40, 50 };    int n = sizeof(arr)/sizeof(arr[0]);    cout

Core Dump (Segmentation fault) in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:54:10

5K+ Views

In this tutorial, we will be discussing a program to understand core dump (segmentation fault) in C/C++.It happens due to reasons like when code tries to write on read only memory or tries to access corrupt memory location.ExampleModifying a string literalint main(){    char *str;    str = "GfG";    *(str+1) = 'n';    return 0; }Accessing out of array index bounds#include using namespace std; int main(){    int arr[2];    arr[3] = 10;    return 0; }Accessing an address which is freed#include #include int main(void){    int* p = malloc(8);    *p = 100;    free(p);    *p = 110;    return 0; }OutputAbnormal termination of program

Converting Strings to Numbers in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:52:05

284 Views

In this tutorial, we will be discussing a program to understand how to convert strings into numbers in C/C++.C/C++ provides two ways to convert strings into numbers.Example Live DemoUsing sscanf()#include int main(){    const char *str = "12345";    int x;    sscanf(str, "%d", &x);    printf("The value of x : %d", x);    return 0; }OutputThe value of x : 12345Using stoi() Live Demo#include #include using namespace std; int main(){    string str1 = "45";    string str2 = "3.14159";    string str3 = "31337 geek";    int myint1 = stoi(str1);    int myint2 = stoi(str2);    int myint3 = stoi(str3);    cout

Convert a String to Integer Array in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:50:25

3K+ Views

In this tutorial, we will be discussing a program to understand how to convert a string into integer array in C/C++.For this we will create a new array. Traverse through the given string, if the character is a comma “, ”, we move on to the next character else add it to the new array.Example Live Demo#include using namespace std; //converting string to integer array void convert_array(string str){    int str_length = str.length();    int arr[str_length] = { 0 };    int j = 0, i, sum = 0;    //traversing the string    for (i = 0; str[i] != ... Read More

Conversion of whole String to uppercase or lowercase using STL in C++

Ayush Gupta
Updated on 16-Mar-2020 09:47:51

5K+ Views

In this tutorial, we will be discussing a program to understand conversion of whole string to uppercase or lowercase using STL in C++.To perform this transformation, C++ STL provides toupper() and tolower() functions to convert to uppercase and lowercase respectively.Example Live Demo#include using namespace std; int main(){    string su = "Tutorials point";    transform(su.begin(), su.end(), su.begin(), ::toupper);    cout

Virtual Function in C++

Ayush Gupta
Updated on 12-Mar-2020 06:40:10

2K+ Views

In this tutorial, we will be discussing a program to understand virtual functions in C++.Virtual function is the member function defined in the base class and can further be defined in the child class as well. While calling the derived class, the overwritten function will be called.Example Live Demo#include using namespace std; class base {    public:    virtual void print(){       cout

Virtual destruction using shared_ptr in C++

Ayush Gupta
Updated on 12-Mar-2020 06:36:51

196 Views

In this tutorial, we will be discussing a program to understand virtual destruction using shared_ptr in C++.To delete the instances of a class, we define the destructor of the base class to be virtual. So it deletes the various object instances inherited in the reverse order in which they were created.Example Live Demo#include #include using namespace std; class Base {    public:    Base(){       cout

Advertisements