C++ map insert()函数

C++ map insert()函数

C++ map的 insert() 函数用于在map中插入一个新元素。

由于map中的元素键是 唯一的 ,插入操作首先检查给定的键是否已经在map中存在,如果键在map中存在,则不会将其插入在map中,并返回指向已存在键的迭代器,否则将新元素插入在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 : 要插入映射表中的键值。

position : 用于插入元素的位置提示。

first : 要插入范围的开始。

last : 要插入范围的结束。

il : 初始化列表。

返回值

它返回一个bool对,表示插入是否发生,并返回一个指向新插入元素的迭代器。

示例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

让我们看一个简单的示例,在指定的位置插入元素:

#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

在上面的示例中,map m1有五个元素,map m2为空。使用insert()函数将m1中的元素从m1的开始到m1的结束插入到m2中,并显示m2 map的内容。

示例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()函数将元素插入到map中。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程