1. map :: insert( )

    1.1 iterator map_name.insert({key, element})

    1. // C++ program to illustrate
    2. // map::insert({key, element})
    3. #include <bits/stdc++.h>
    4. using namespace std;
    5. int main()
    6. {
    7. // initialize container
    8. map<int, int> mp;
    9. // insert elements in random order
    10. mp.insert({ 2, 30 });
    11. mp.insert({ 1, 40 });
    12. mp.insert({ 3, 60 });
    13. // does not inserts key 2 with element 20
    14. mp.insert({ 2, 20 });
    15. mp.insert({ 5, 50 });
    16. // prints the elements
    17. cout << "KEY\tELEMENT\n";
    18. for (auto & itr : mp) {
    19. cout << itr.first
    20. << '\t' << itr.second << '\n';
    21. }
    22. return 0;
    23. }
    24. ====================================================================================================
    25. KEY ELEMENT
    26. 1 40
    27. 2 30
    28. 3 60
    29. 5 50

    1.2 iterator map_name.insert(iterator position, {key, element})

    1. // C++ program to illustrate
    2. // map::insert(iteratorposition, {key, element})
    3. #include <bits/stdc++.h>
    4. using namespace std;
    5. int main()
    6. {
    7. // initialize container
    8. map<int, int> mp;
    9. // insert elements in random order
    10. mp.insert({ 2, 30 });
    11. mp.insert({ 1, 40 });
    12. auto it = mp.find(2);
    13. // inserts {3, 60} starting the search from
    14. // position where 2 is present
    15. mp.insert(it, { 3, 60 });
    16. // prints the elements
    17. cout << "KEY\tELEMENT\n";
    18. for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    19. cout << itr->first
    20. << '\t' << itr->second << '\n';
    21. }
    22. return 0;
    23. }
    24. ==================================================================================================== KEY ELEMENT
    25. 1 40
    26. 2 30
    27. 3 60

    1.3 iterator map_name.insert(iterator position1, iterator position2)

    1. // C++ program to illustrate
    2. // map::insert(iteratorposition1, iteratorposition2)
    3. #include <bits/stdc++.h>
    4. using namespace std;
    5. int main()
    6. {
    7. // initialize container
    8. map<int, int> mp, mp1;
    9. // insert elements in random order
    10. mp.insert({ 2, 30 });
    11. mp.insert({ 1, 40 });
    12. // inserts all elements in range
    13. // [begin, end) in mp1
    14. mp1.insert(mp.begin(), mp.end());
    15. // prints the elements
    16. cout << "Elements in mp1 are\n";
    17. cout << "KEY\tELEMENT\n";
    18. for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) {
    19. cout << itr->first
    20. << '\t' << itr->second << '\n';
    21. }
    22. return 0;
    23. }
    24. ====================================================================================================
    25. Elements in mp1 are
    26. KEY ELEMENT
    27. 1 40
    28. 2 30