Found 7197 Articles for C++

partition_point in C++

Ayush Gupta
Updated on 14-Apr-2020 12:13:33

128 Views

In this tutorial, we will be discussing a program to understand partition_point in C++.Partition point is a method which returns an iterator pointing to the first value in a given range. The range is a partitioned one in which the predicate is not true.Example Live Demo#include #include #include bool IsOdd(int i) { return (i % 2) == 1; } int main(){    std::vector data{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };    std::vector odd, even;    std::stable_partition(data.begin(), data.end(), IsOdd);    auto it = std::partition_point(data.begin(), data.end(),    IsOdd);    odd.assign(data.begin(), it);    even.assign(it, data.end());    std::cout

Pair in C++ Standard Template Library (STL)

Ayush Gupta
Updated on 14-Apr-2020 12:11:55

505 Views

In this tutorial, we will be discussing a program to understand pair in C++ Standard Template Library.Pair is a container defined in the utility header that contains two values. It is used to combine two values and associate them even if they are of different types.Example Live Demo#include #include using namespace std; int main(){    //initializing a pair    pair PAIR1 ;    PAIR1.first = 100;    PAIR1.second = 'G' ;    cout

Override Keyword in C++ programming

Ayush Gupta
Updated on 14-Apr-2020 12:10:44

236 Views

In this tutorial, we will be discussing a program to understand override keyword in C++.Override keyword is used to override the function in a base class and define a separate function with the same signature in the child class.Example Live Demo#include using namespace std; class Base {    public:    //function to be override    virtual void func() {       cout

Output Iterators in C++ programming

Ayush Gupta
Updated on 14-Apr-2020 12:09:22

150 Views

In this tutorial, we will be discussing a program to understand output iterators in C++.Output iterators are a part of the major five iterators. They function opposite of the input iterators in a way that they can be assigned values but can’t be accessed to fetch values.Example Live Demo#include #include using namespace std; int main(){    vectorv1 = {1, 2, 3, 4, 5};    //declaring iterator    vector::iterator i1;    for (i1=v1.begin();i1!=v1.end();++i1){       *i1 = 1;    }    return 0; }OutputNo output, because we cannot access values of output operators

Ordered Set and GNU C++ PBDS

Ayush Gupta
Updated on 14-Apr-2020 12:07:48

2K+ Views

In this tutorial, we will be discussing a program to understand ordered set and GNU C++ PBDS.Ordered set is a policy based structure other than those in the STL library. The ordered set keeps all the elements in a sorted order and doesn’t allow duplicate values.Example Live Demo#include using namespace std; #include #include using namespace __gnu_pbds; #define ordered_set tree int main(){    //declaring ordered set    ordered_set o_set;    o_set.insert(5);    o_set.insert(1);    o_set.insert(2);    cout

Order of Constructor/ Destructor Call in C++

Ayush Gupta
Updated on 14-Apr-2020 12:04:57

535 Views

In this tutorial, we will be discussing a program to understand the order of constructor/ destructor in C++.Order of constructor/destructor refers to the pattern in which the constructors of various classes are called during inheritance of classes.Example Live Demo#include using namespace std; //parent class class Parent{    public:    Parent(){       cout

Operator overloading in C++ to print contents of vector, map, pair ..

Ayush Gupta
Updated on 14-Apr-2020 12:02:26

599 Views

In this tutorial, we will be discussing a program to understand operator overloading in C++ to print contents of vector, map and pair.Operator overloading is the function of operators which gives them the ability to act on User defined objects and work accordingly in a similar fashion.ExampleVector Live Demo#include #include using namespace std; template ostream& operator

Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), inplace_merge

Ayush Gupta
Updated on 14-Apr-2020 11:58:44

152 Views

In this tutorial, we will be discussing a program to understand the various merge operations using STL in C++.The merge() function is used to merge two sorted containers in a way that the new container is also sorted. Further includes() is used to check if the elements from first container are present in the second one.Example Live Demo#include #include #include using namespace std; int main(){    vector v1 = {1, 3, 4, 5, 20, 30};    vector v2 = {1, 5, 6, 7, 25, 30};    //initializing resultant vector    vector v3(12);    merge(v1.begin(), v1.end(), v2.begin(),    v2.end(), v3.begin());    cout

Remove Zero Sum Consecutive Nodes from Linked List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:51:36

2K+ Views

Suppose we have given the head of a linked list; we have to repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. So after doing so, we have to return the head of the final linked list. So if the list is like [1, 2, -3, 3, 1], then the result will be [3, 1].To solve this, we will follow these steps −Create a node called dummy, and store 0 into it, set next of dummy := headcreate one map m, store dummy for the key 0 into m, set sum = 0while ... Read More

Swap For Longest Repeated Character Substring in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:46:05

562 Views

Suppose we have a string text, so we are allowed to swap two of the characters in the string. We have to find the length of the longest substring with repeated characters. So if the input is like “ababa”, then the result will be 3, as if we swap first b with last a, or the last b with first a, then the longest repeated character will be “aaa”, so the length is 3.To solve this, we will follow these steps −Define a map cnt, set ret := 1, j := 0, n := size of text, v := 0, ... Read More

Advertisements