Found 7401 Articles for C++

C++ Program to Implement Sorting containers in STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

115 Views

In this C++ program, we implement Sorting containers in STL.Functions and descriptions:Functions used here:    l.push_back() = It is used to push elements into a list from the front.    l.sort() = Sorts the elements of the list.    Where l is a list object.Example Code#include #include #include #include using namespace std; int main() {    list l;    list::iterator it;    int c, i;    while (1) {       cout

C++ Program to Implement Set_Union in STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

471 Views

The union of two sets is created by the elements that are present in either one of the sets, or in both. Elements from the second set that has the same element in the first set are not copied to the resulting set.Common set operations are −Set UnionSet IntersectionSymmetric Set Difference or Exclusive-ORSet Difference or SubtractionAlgorithmBegin    Declare set vector v and iterator st.    Initialize st= set_union (set1, set1 + n, set2, set2 +n, v.begin()))    Print the elements. End.Example Code Live Demo#include #include #include using namespace std; int main () {    int set1[] = {5, ... Read More

C++ Program to Implement Set_Symmetric_difference in STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

180 Views

This is a c++ program to implemet set_symmetric_difference. The symmetric difference of two sets is built by the elements that are present in one of the sets, but not in the other.Common set operations are −Set UnionSet IntersectionSymmetric Set Difference or Exclusive-ORSet Difference or SubtractionAlgorithmBegin    Declare set vector v and iterator st.   Initialize st = set_symmetric_difference (set1, set1 + n, set2, set2 +n, v.begin()))    Print the elements obtained after symmetric_difference of two sets End.Example Code#include #include #include using namespace std; int main () {    int set1[] = {5, 6, 7, 8, 9, 10};   ... Read More

C++ Program to Implement Set_Intersection in STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

435 Views

The intersection of two sets is formed only by the elements those are common in both sets. The elements copied by the function come always from the first set in the same order. The elements in the both the sets shall already be ordered.Common set operations are −Set UnionSet IntersectionSymmetric Set Difference or Exclusive-ORSet Difference or SubtractionAlgorithmBegin    Declare set vector v and iterator st.    Initialize st = set_intersection (set1, set1 + n, set2, set2 +n, v.begin()))    Print the intersection between two sets. End.Example Code#include #include #include using namespace std; int main () {   ... Read More

C++ Program to Implement Set_Difference in STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

703 Views

The difference of two sets is formed only by the elements that are present in the first set, not in the second set. The elements copied by the function come always from the first set in the same order. The elements in both the sets shall already be ordered.Common set operations are −Set UnionSet IntersectionSymmetric Set Difference or Exclusive-ORSet Difference or SubtractionAlgorithmBegin    Declare set vector v and iterator st.    Initialize st = set_difference (set1, set1 + n, set2, set2 +n, v.begin()))    Print the number of elements different between two sets. End.Example Code#include #include #include using ... Read More

C++ Program to Implement Set in STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

439 Views

Set is abstract data type in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, but it is possible to remove and add the modified value of that element.Functions and descriptions:Functions used here:    st.size() = Returns the size of set.    st.insert() = It is used to insert elements to the set.    st.erase() = To delete the element from the set.    st.find() = Returns an iterator to the search element in the set if found, else ... Read More

C++ Program to Implement Queue in STL

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

571 Views

A queue is a linear structure which follows First In First Out (FIFO) order in which the operations are performed on the elements of the queue.AlgorithmFunctions used here:    q.size() = Returns the size of queue.    q.push() = It is used to insert elements to the queue.    q.pop() = To pop out the value from the queue.    q.front() = Returns the front element of the array.    q.back() = Returns the back element of the array.Example Code#include #include #include #include using namespace std; int main() {    queue q;    int c, i;    while (1) {       cout

C++ Program to Implement Priority_queue in STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

83 Views

Priority queues are a type of container adapters, in which the first element of the queue is the greatest of all elements in the queue. Elements are in also non decreasing order in the priority queue. An element with high priority is served before an element with low priority, in a priority queue.Functions and descriptions:Functions used here:    pq.size() = Returns the size of priority queue.    pq.insert) = It is used to insert elements to the priority queue.    pq.delete() = Deletes the value from the priority queue.    pq.top() = Returns a reference to the top most element ... Read More

C++ Program to Implement Prev_Permutataion in STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

74 Views

Prev_permutation in STL is used to rearrange the elements in the range [first, last] into the previous lexicographically smaller permutation. A permutation is each one of the N! possible arrangements the elements can take. Here is a C++ program to implement Prev_permutation in STL.AlgorithmBegin    Define one integer array variable elements[].    Get the number of data e from the user.    Initialize the array elements[] with e number of data from the keyboard.    Sort all the array elements.    Reverse the array elements.    Do       show(elements) //to display the current content of the array   ... Read More

C++ Program to Implement Pairs in STL

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

244 Views

Pair is a simple container which consists of two data object:‘first’ = The first element is referenced as ‘first’ ‘second’ = the second element and the order is fixed (first, second).Pair can be assigned, compared and copied. It is used to combine together two values which may be different in type.Syntax is: pairvariable name(datavalue1, datavalue2).AlgorithmBegin Write pairvariable name(datavalue1,datavalue2) Print the pairs EndExample Code#include using namespace std; int main() { pair value('a',7); pair fruit ("grapes",2.30); pair food ("pulao",200); cout

Advertisements