Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to make dictionary of numbers in c++

// CPP Program to demonstrate the implementation in Map
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
 
int main()
{
 
    // empty map container
    map<int, int> gquiz1;
 
    // insert elements in random order
    gquiz1.insert(pair<int, int>(1, 40));
    gquiz1.insert(pair<int, int>(2, 30));
    gquiz1.insert(pair<int, int>(3, 60));
    gquiz1.insert(pair<int, int>(4, 20));
    gquiz1.insert(pair<int, int>(5, 50));
    gquiz1.insert(pair<int, int>(6, 50));
    gquiz1.insert(pair<int, int>(7, 10));
 
    // printing map gquiz1
    map<int, int>::iterator itr;
    cout << "
The map gquiz1 is : 
";
    cout << "	KEY	ELEMENT
";
    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
        cout << '	' << itr->first << '	' << itr->second
             << '
';
    }
    cout << endl;
 
    // assigning the elements from gquiz1 to gquiz2
    map<int, int> gquiz2(gquiz1.begin(), gquiz1.end());
 
    // print all elements of the map gquiz2
    cout << "
The map gquiz2 after"
         << " assign from gquiz1 is : 
";
    cout << "	KEY	ELEMENT
";
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
        cout << '	' << itr->first << '	' << itr->second
             << '
';
    }
    cout << endl;
 
    // remove all elements up to
    // element with key=3 in gquiz2
    cout << "
gquiz2 after removal of"
            " elements less than key=3 : 
";
    cout << "	KEY	ELEMENT
";
    gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
        cout << '	' << itr->first << '	' << itr->second
             << '
';
    }
 
    // remove all elements with key = 4
    int num;
    num = gquiz2.erase(4);
    cout << "
gquiz2.erase(4) : ";
    cout << num << " removed 
";
    cout << "	KEY	ELEMENT
";
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
        cout << '	' << itr->first << '	' << itr->second
             << '
';
    }
 
    cout << endl;
 
    // lower bound and upper bound for map gquiz1 key = 5
    cout << "gquiz1.lower_bound(5) : "
         << "	KEY = ";
    cout << gquiz1.lower_bound(5)->first << '	';
    cout << "	ELEMENT = " << gquiz1.lower_bound(5)->second
         << endl;
    cout << "gquiz1.upper_bound(5) : "
         << "	KEY = ";
    cout << gquiz1.upper_bound(5)->first << '	';
    cout << "	ELEMENT = " << gquiz1.upper_bound(5)->second
         << endl;
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: range based for loop c++ 
Cpp :: find positive number factorial in C++ 
Cpp :: c++ program to convert celsius to kelvin 
Cpp :: c++ std string to float 
Cpp :: Exit Button c++ code 
Cpp :: cpp read from file 
Cpp :: fstream read write mode 
Cpp :: max pooling in c++ 
Cpp :: c++ class 
Cpp :: c++ delay 
Cpp :: how to declare a 2d vector stack 
Cpp :: flutter single instance app 
Cpp :: string concatenation operator overloading c++ 
Cpp :: remove something from stringstream 
Cpp :: 344. reverse string c++ 
Cpp :: c++ std map initializer list 
Cpp :: tabeau pseudo dynamique sur c++ 
Cpp :: unordered_map c++ 
Cpp :: c++ memory address 
Cpp :: use set to get duplicates in c++ 
Cpp :: copy vector c++ 
Cpp :: abs in c++ used for 
Cpp :: if else c++ 
Cpp :: Write a C++ program using constructor 
Cpp :: c++ overloading by ref-qualifiers 
Cpp :: using-controller-and-qt-worker-in-a-working-gui-example 
Cpp :: c++ profiling tools 
Cpp :: return multiple values c++ 
Cpp :: Variabili globali c++ 
Cpp :: how to signify esc key in cpp 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =