当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ map insert()用法及代码示例


map::insert()是C++ STL中的内置函数,用于在Map容器中插入具有特定键的元素。

  1. 用法:
    iterator map_name.insert({key, element})
    

    参数:该函数接受一对,该对包括要插入Map容器的键和元素。如果键已经存在于Map中,则该函数不会在Map中插入键和元素。

    返回值:该函数返回一个指向容器中新元素的迭代器。


    下面是上述语法的说明:

    // C++ program to illustrate 
    // map::insert({key, element}) 
    #include <bits/stdc++.h> 
    using namespace std; 
      
    int main() 
    { 
      
        // initialize container 
        map<int, int> mp; 
      
        // insert elements in random order 
        mp.insert({ 2, 30 }); 
        mp.insert({ 1, 40 }); 
        mp.insert({ 3, 60 }); 
      
        // does not inserts key 2 with element 20 
        mp.insert({ 2, 20 }); 
        mp.insert({ 5, 50 }); 
      
        // prints the elements 
        cout << "KEY\tELEMENT\n"; 
        for (auto itr = mp.begin(); itr != mp.end(); ++itr) { 
            cout << itr->first 
                 << '\t' << itr->second << '\n'; 
        } 
        return 0; 
    }
    输出:
    KEY    ELEMENT
    1    40
    2    30
    3    60
    5    50
    
  2. 用法:
    iterator map_name.insert(iterator position, {key, element})
    

    参数:该函数接受两个参数,如下所述:

    • {{key, element}这指定了一个由键和要插入Map容器中的元素组成的对。
    • position:这并不指定要插入的位置,它仅指向要开始进行插入搜索操作的位置,以加快处理速度。插入是根据Map容器遵循的顺序完成的。

    返回值:该函数返回一个指向容器中新元素的迭代器。

    下面是上述语法的说明:

    // C++ program to illustrate 
    // map::insert({key, element}) 
    #include <bits/stdc++.h> 
    using namespace std; 
      
    int main() 
    { 
      
        // initialize container 
        map<int, int> mp; 
      
        // insert elements in random order 
        mp.insert({ 2, 30 }); 
        mp.insert({ 1, 40 }); 
      
        auto it = mp.find(2); 
      
        // inserts {3, 6} starting the search from 
        // position where 2 is present 
        mp.insert(it, { 3, 60 }); 
      
        // prints the elements 
        cout << "KEY\tELEMENT\n"; 
        for (auto itr = mp.begin(); itr != mp.end(); ++itr) { 
            cout << itr->first 
                 << '\t' << itr->second << '\n'; 
        } 
        return 0; 
    }
    输出:
    KEY    ELEMENT
    1    40
    2    30
    3    60
    
  3. 用法:
    iterator map_name.insert(iterator position1, iterator position2)
    

    参数:该函数接受两个参数position1和position2,它们指定元素的范围。范围[position1,last)内的所有元素都插入到另一个Map容器中。

    返回值:该函数返回一个指向容器中新元素的迭代器。

    下面是上述语法的说明:

    // C++ program to illustrate 
    // map::insert({key, element}) 
    #include <bits/stdc++.h> 
    using namespace std; 
      
    int main() 
    { 
      
        // initialize container 
        map<int, int> mp, mp1; 
      
        // insert elements in random order 
        mp.insert({ 2, 30 }); 
        mp.insert({ 1, 40 }); 
      
        // inserts all elements in range 
        // [begin, end) in mp1 
        mp1.insert(mp.begin(), mp.end()); 
      
        // prints the elements 
        cout << "Elements in mp1 are\n"; 
        cout << "KEY\tELEMENT\n"; 
        for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) { 
            cout << itr->first 
                 << '\t' << itr->second << '\n'; 
        } 
        return 0; 
    }
    输出:
    Elements in mp1 are
    KEY    ELEMENT
    1    40
    2    30
    


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 map insert() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。