C++ STL functions

C++

Posted by PZ on March 4, 2020

Map

// empty map container 
    map<int, int> gquiz1; 
  
    // insert elements in random order 
    gquiz1.insert(pair<int, int>(1, 40));
          gquiz1.insert({1,40});
	
	// printing map gquiz1 
	    map<int, int>::iterator itr; 
	
	for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) { 
	        cout << '\t' << itr->first 
	             << '\t' << itr->second << '\n'; 
	    } 
	
	// assigning the elements from gquiz1 to gquiz2 
	    map<int, int> gquiz2(gquiz1.begin(), gquiz1.end()); 
	
	
	// remove all elements up to 
	    // element with key=3 in gquiz2 
	    cout << "\ngquiz2 after removal of"
	            " elements less than key=3 : \n"; 
	    cout << "\tKEY\tELEMENT\n"; 
	    gquiz2.erase(gquiz2.begin(), gquiz2.find(3)); 
	
	// remove all elements with key = 4 
	    int num; 
	    num = gquiz2.erase(4); 
	
	
	// lower bound and upper bound for map gquiz1 key = 5 
	    cout << "gquiz1.lower_bound(5) : "
	         << "\tKEY = "; 
	    cout << gquiz1.lower_bound(5)->first << '\t'; 
	    cout << "\tELEMENT = "
	         << gquiz1.lower_bound(5)->second << endl; 
	    cout << "gquiz1.upper_bound(5) : "
	         << "\tKEY = "; 
	    cout << gquiz1.upper_bound(5)->first << '\t'; 
	    cout << "\tELEMENT = "
	         << gquiz1.upper_bound(5)->second << endl; 
	
	
	// sort map by its member
	sort(v.begin(),v.end(),comparator)
	
	bool comparator(mem i1, mem i2)
	{
	       return (i1.first < i2.first)}
	
	
	// initialize container; copy constructor
	    map<int, int> mp, copymp; 
	  
	    // insert elements in random order 
	    mp.insert({ 2, 30 }); 
	    mp.insert({ 1, 40 }); 
	    mp.insert({ 4, 50 }); 
	         h[{y, x}].insert(root->val);
	  
	    // = operator is used to copy map 
    copymp = mp; 

Set

#include <iostream> 
#include <set> 
#include <iterator> 
  
using namespace std; 
  
int main() 
{ 
    // empty set container 
    set <int, greater <int> > gquiz1;         
  
    // insert elements in random order 
    gquiz1.insert(40); 
    gquiz1.insert(30); 
    gquiz1.insert(60); 
    gquiz1.insert(20); 
    gquiz1.insert(50); 
    gquiz1.insert(50); // only one 50 will be added to the set 
    gquiz1.insert(10); 
  
    // printing set gquiz1 
    set <int, greater <int> > :: iterator itr; 
    cout << "\nThe set gquiz1 is : "; 
    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) 
    { 
        cout << '\t' << *itr; 
    } 
    cout << endl; 
  
    // assigning the elements from gquiz1 to gquiz2 
    set <int> gquiz2(gquiz1.begin(), gquiz1.end()); 
  
    // print all elements of the set gquiz2 
    cout << "\nThe set gquiz2 after assign from gquiz1 is : "; 
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) 
    { 
        cout << '\t' << *itr; 
    } 
    cout << endl; 
  
    // remove all elements up to 30 in gquiz2 
    cout << "\ngquiz2 after removal of elements less than 30 : "; 
    gquiz2.erase(gquiz2.begin(), gquiz2.find(30)); 
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) 
    { 
        cout << '\t' << *itr; 
    } 
  
    // remove element with value 50 in gquiz2 
    int num; 
    num = gquiz2.erase (50); 
    cout << "\ngquiz2.erase(50) : "; 
    cout << num << " removed \t" ; 
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) 
    { 
        cout << '\t' << *itr; 
    } 
  
    cout << endl; 
  
    //lower bound and upper bound for set gquiz1 
    cout << "gquiz1.lower_bound(40) : "
         << *gquiz1.lower_bound(40) << endl; 
    cout << "gquiz1.upper_bound(40) : "
         << *gquiz1.upper_bound(40) << endl; 
  
    //lower bound and upper bound for set gquiz2 
    cout << "gquiz2.lower_bound(40) : "
         << *gquiz2.lower_bound(40) << endl; 
    cout << "gquiz2.upper_bound(40) : "
         << *gquiz2.upper_bound(40) << endl; 
  
    return 0; 
  
} 

Erase Function

set.erase(position)
set.erase(startingposition, endingposition)
erase(iterator, iterator)
erase(element) --> return its index;

Stack

// CPP program to demonstrate working of STL stack 
#include <iostream> 
#include <stack> 
using namespace std; 
  
void showstack(stack <int> s) 
{ 
    while (!s.empty()) 
    { 
        cout << '\t' << s.top(); 
        s.pop(); 
    } 
    cout << '\n'; 
} 
  
int main () 
{ 
    stack <int> s; 
    s.push(10); 
    s.push(30); 
    s.push(20); 
    s.push(5); 
    s.push(1); 
  
    cout << "The stack is : "; 
    showstack(s); 
  
    cout << "\ns.size() : " << s.size(); 
    cout << "\ns.top() : " << s.top(); 
  
  
    cout << "\ns.pop() : "; 
    s.pop(); 
    showstack(s); 
  
    return 0; 
} 

Deque

#include <iostream> 
#include <deque> 
  
using namespace std; 
  
void showdq(deque <int> g) 
{ 
    deque <int> :: iterator it; 
    for (it = g.begin(); it != g.end(); ++it) 
        cout << '\t' << *it; 
    cout << '\n'; 
} 
  
int main() 
{ 
    deque <int> gquiz; 
    gquiz.push_back(10); 
    gquiz.push_front(20); 
    gquiz.push_back(30); 
    gquiz.push_front(15); 
    cout << "The deque gquiz is : "; 
    showdq(gquiz); 
  
    cout << "\ngquiz.size() : " << gquiz.size(); 
    cout << "\ngquiz.max_size() : " << gquiz.max_size(); 
  
    cout << "\ngquiz.at(2) : " << gquiz.at(2); 
    cout << "\ngquiz.front() : " << gquiz.front(); 
    cout << "\ngquiz.back() : " << gquiz.back(); 
  
    cout << "\ngquiz.pop_front() : "; 
    gquiz.pop_front(); 
    showdq(gquiz); 
  
    cout << "\ngquiz.pop_back() : "; 
    gquiz.pop_back(); 
    showdq(gquiz); 
  
    return 0; 
} 

Queue

// CPP code to illustrate  
// Queue in Standard Template Library (STL) 
#include <iostream> 
#include <queue> 
  
using namespace std; 
  
void showq(queue <int> gq) 
{ 
    queue <int> g = gq; 
    while (!g.empty()) 
    { 
        cout << '\t' << g.front(); 
        g.pop(); 
    } 
    cout << '\n'; 
} 
  
int main() 
{ 
    queue <int> gquiz; 
    gquiz.push(10); 
    gquiz.push(20); 
    gquiz.push(30); 
  
    cout << "The queue gquiz is : "; 
    showq(gquiz); 
  
    cout << "\ngquiz.size() : " << gquiz.size(); 
    cout << "\ngquiz.front() : " << gquiz.front(); 
    cout << "\ngquiz.back() : " << gquiz.back(); 
  
    cout << "\ngquiz.pop() : "; 
    gquiz.pop(); 
    showq(gquiz); 
  
    return 0; 
} 

Prioity_Queue

// this is an strucure which implements the 
// operator overlading 
struct CompareHeight { 
    bool operator()(Person const& p1, Person const& p2) 
    { 
        // return "true" if "p1" is ordered  
        // before "p2", for example: back is top!!!
        return p1.height < p2.height; 
    } 
}; 
  
int main() 
{ 
    priority_queue<Person, vector<Person>, CompareHeight> Q; 
  
    // When we use priority_queue with  structure 
    // then we need this kind of syntax where 
    // CompareAge is the functor or comparision function 
    float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },  
                    { 20, 6 }, { 33, 6.1 }, { 23, 5.6 } }; 
  
    for (int i = 0; i < ROW; ++i) { 
  
        Q.push(Person(arr[i][0], arr[i][1])); 
  
        // insert an object in priority_queue by using 
        // the Person strucure constructor 
    } 
  
    while (!Q.empty()) { 
        Person p = Q.top(); 
        Q.pop(); 
        cout << p.age << " " << p.height << "\n"; 
    } 
    return 0; 
} 

// C++ program to create a priority queue of pairs. 
// By default a max heap is created ordered 
// by first element of pair. 
#include <bits/stdc++.h> 
  
using namespace std; 
  
// Driver program to test methods of graph class 
int main() 
{ 
    // By default a max heap is created ordered 
    // by first element of pair. 
    priority_queue<pair<int, int> > pq; 
  
    pq.push(make_pair(10, 200)); 
    pq.push(make_pair(20, 100)); 
    pq.push(make_pair(15, 400)); 
  
    pair<int, int> top = pq.top(); 
    cout << top.first << " " << top.second; 
    return 0; 
} 

// C++ program to create a priority queue of pairs. 
// We can create a min heap by passing adding two  
// parameters, vector and greater(). 
#include <bits/stdc++.h> 
  
using namespace std; 
  
typedef pair<int, int> pi; 
  
// Driver program to test methods of graph class 
int main() 
{ 
    // By default a min heap is created ordered 
    // by first element of pair. 
    priority_queue<pi, vector<pi>, greater<pi> > pq; 
  
    pq.push(make_pair(10, 200)); 
    pq.push(make_pair(20, 100)); 
    pq.push(make_pair(15, 400)); 
  
    pair<int, int> top = pq.top(); 
    cout << top.first << " " << top.second; 
    return 0; 
} 

 // Note that by default C++ creates a max-heap 
// for priority queue 
#include <iostream> 
#include <queue> 
  
using namespace std; 
  
void showpq(priority_queue <int> gq) 
{ 
    priority_queue <int> g = gq; 
    while (!g.empty()) 
    { 
        cout << '\t' << g.top(); 
        g.pop(); 
    } 
    cout << '\n'; 
} 
  
int main () 
{ 
    priority_queue <int> gquiz; // 从小到大
    gquiz.push(10); 
    gquiz.push(30); 
    gquiz.push(20); 
    gquiz.push(5); 
    gquiz.push(1);

priority_queue <int, vector<int>, greater<int>> gquiz; //从大到小
    gquiz.push(10); 
    gquiz.push(30); 
    gquiz.push(20); 
    gquiz.push(5); 
    gquiz.push(1);

    cout << "The priority queue gquiz is : "; 
    showpq(gquiz); 
  
    cout << "\ngquiz.size() : " << gquiz.size(); 
    cout << "\ngquiz.top() : " << gquiz.top(); 
  
  
    cout << "\ngquiz.pop() : "; 
    gquiz.pop(); 
    showpq(gquiz); 
  
    return 0; 
}

List

#include <iostream> 
#include <list> 
#include <iterator> 
using namespace std; 
  
//function for printing the elements in a list 
void showlist(list <int> g) 
{ 
    list <int> :: iterator it; 
    for(it = g.begin(); it != g.end(); ++it) 
        cout << '\t' << *it; 
    cout << '\n'; 
} 
  
int main() 
{ 
  
    list <int> gqlist1, gqlist2; 
  
  
    for (int i = 0; i < 10; ++i) 
    { 
        gqlist1.push_back(i * 2); 
        gqlist2.push_front(i * 3); 
    } 
    cout << "\nList 1 (gqlist1) is : "; 
    showlist(gqlist1); 
  
    cout << "\nList 2 (gqlist2) is : "; 
    showlist(gqlist2); 
  
    cout << "\ngqlist1.front() : " << gqlist1.front(); 
    cout << "\ngqlist1.back() : " << gqlist1.back(); 
  
    cout << "\ngqlist1.pop_front() : "; 
    gqlist1.pop_front(); 
    showlist(gqlist1); 
  
    cout << "\ngqlist2.pop_back() : "; 
    gqlist2.pop_back(); 
    showlist(gqlist2); 
  
    cout << "\ngqlist1.reverse() : "; 
    gqlist1.reverse(); 
    showlist(gqlist1); 
  
    cout << "\ngqlist2.sort(): "; 
    gqlist2.sort(); 
    showlist(gqlist2); 
  
    return 0; 
  
} 


String


string& string::insert (size_type idx, const string& str)


// CPP code for insert (size_type idx, const string& str) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str1, string str2) 
{ 
   
    // Inserts str2 in str1 starting  
    // from 6th index of str1 
    str1.insert(6, str2); 
    cout << "Using insert : "; 
    cout << str1; 
} 
  
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks "); 
   
    cout << "Original String : " << str1 << endl; 
    insertDemo(str1, str2); 
   
    return 0; 
} 

map, vector sort

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// vectors,lists can compare as the datatype of pair
// call compare as a function pointer
bool compare(const pair<int, int> p1, const pair<int,int> p2)
{
    if (p1.second == p2.second)
    {
        return p1.first > p2.first;
    }
    return p1.second < p2.second;
}


// struct compare needs call vecCmp(): 
// it will also return function pointers since () overloading in this structure
struct vecCmp
{
    bool operator() (const pair<int, int> p1, const pair<int,int> p2)
    {
        if (p1.second == p2.second)
        {
            return p1.first > p2.first;
        }
        return p1.second < p2.second;
    }
};

// can only compare keys in maps, parameters is the type of keys
struct cmp
{
    bool operator() (const int& p1, const int& p2)
    {
        return p1 > p2;
    }
};


int main()
{
    // sort by keys: less<int> by default, or greater<int> 
    // or write a struct with bool operator() () {} function inside 
    //map<int,int,cmp> hash = {100,4},{103,5},{108,5},{102,5};
    map<int,int>::iterator it = hash.begin();
    vector<pair<int,int>> vec(hash.begin(), hash.end());
    for (auto item: hash)
    {
        cout << item.first << ":" << item.second << endl;
    }
    // we cannot sort map by values directly since it is a key-value related container
    // first, initial a vector<pair<T1, T2>> by map.begin(), map.end() 
    sort(vec.begin(),vec.end(), vecCmp());
    for (auto item: vec)
    {
        cout << item.first << ":" << item.second << endl;
    }
    return 0;
}