C++ Map insert()
 
 
C++ Map insert()函数
 
  C++映射 insert()函数用于插入新元素 
 
 由于元素键在地图中唯一,因此插入操作首先检查给定键是否已存在于地图中(如果键已存在于地图中)。 map,则不会将其插入地图中,并返回现有键的迭代器,否则将在地图中插入新元素。
 
语法
 
 
 
  single element (1)     pair<iterator,bool> insert (const value_type& val);   //until C++ 11
with hint (2)  iterator insert (iterator position, const value_type& val);   //until C++ 11
range (3) template <class InputIterator>
         void insert (InputIterator first, InputIterator last);        //until C++ 11
single element (1)  pair<iterator,bool> insert (const value_type& val);
      template <class P> pair<iterator,bool> insert (P&& val); //since C++ 11
            
with hint (2)  iterator insert (const_iterator position, const value_type& val);
       template <class P> iterator insert (const_iterator position, P&& val);
range (3) template <class InputIterator>
         void insert (InputIterator first, InputIterator last); //since C++ 11
initializer list (4)  void insert (initializer_list<value_type> il);   //since C++ 11 
   
  
参数
 
  val : 要插入地图的键值。
 
  pos: 提示要插入元素的位置。
 
  first: 要插入范围的开始。
 
  last: 范围的结束插入。
 
  il : 初始化列表。
 
返回值
 
 它返回一个布尔对来表示
 
示例1 
 
 让我们看一个简单的示例,将元素插入到地图中。 
 
 
 
  #include <iostream>
#include <map>
using namespace std;
int main() {
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            };
   // inserting new element
   m.insert(pair<char, int>('d', 4));
   m.insert(pair<char, int>('e', 5));
   cout << "Map contains following elements" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
} 
   
  
  输出: 
 
 
 
  Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
 
   
  
 在上面的示例中,它只是插入具有给定键值对的元素。
 
示例2 
 
 让我们看一个简单的示例来插入元素位于指定pos: 
 
 
 
  #include <iostream>
#include <map>
using namespace std;
int main(void) {
   map<char, int> m = {
            {'b', 2},
            {'c', 3},
            {'d', 4},
            };
   //inserting element with the given position
   m.insert(m.begin(), pair<char, int>('a', 1));  
   m.insert(m.end(), pair<char, int>('e', 5));
   cout << "Map contains following elements" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
} 
   
  
  输出: 
 
 
 
  Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
 
   
  
 在上面的示例中,将元素插入到定义的位置,即在开始元素{'a',1}中插入元素,在结束元素{'e',5}中插入。
 
示例3 
 
 让我们看一个简单的示例,将一个地图的元素插入另一个地图。
 
 
 
  #include <iostream>
#include <map>
using namespace std;
int main() {
   
   map<char, int> m1 = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };
map<char, int> m2;  // creating new map m2
m2.insert(m1.begin(), m1.end());   //inserting the elements of m1 to m2 from begin to end
   cout << "Map contains following elements" << endl;
   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
} 
   
  
  输出: 
 
 
 
  Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
 
   
  
 在上面的示例中,映射m1具有五个元素,而映射m2为空。 insert()函数用于从m1的开头到m1的结尾插入m1到m2的元素,并显示m2映射的内容。
 
示例4 
 
 让我们看一个插入元素的简单示例。
 
 
 
  #include <iostream>
#include <map>
using namespace std;
int main(void) {
   map<int , string> m = {
            {1, "Java"},
            {2, "C++"},
            {3, "SQL"},
            };
   m.insert({{4,"VB"}, {5, "Oracle"}});
   cout << "Map contains following elements" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " : " << it->second << endl;
   return 0;
} 
   
  
  输出: 
 
 
 
  Map contains following elements
1 : Java
2 : C++
3 : SQL
4 : VB
5 : Oracle
 
   
  
 在上面的示例中,使用另一种形式的insert()函数将元素插入到地图中。