Skip to content

Latest commit

History

History
29 lines (22 loc) 路 892 Bytes

insert.md

File metadata and controls

29 lines (22 loc) 路 892 Bytes

insert

Description : This function is used to insert elements with a particular key in the unordered_map container, increases the container size by 1, and does not insert any duplicate entries. These are the following variants of this function. All of them are overloaded functions.

Example :

// Demonstrates insert() 
#include <iostream>
#include <unordered_map>

int main(){
    //declares an empty map. O(1)
    std::unordered_map<char, int> mymap; 
    
    // inserting in to unordered_map with O(1) time on average
    mymap.insert({'A', 1});
    mymap.insert({'b', 2});
    mymap.insert({'c', 3});
  
    //print unordered_map elements
    for (auto it = mymap.begin(); it != mymap.end(); ++it){ 
        std::cout << it->first << " = "<< it->second << '\n'; 
    }
    return 0;
}

Run Code