C++ Map 中的 insert函数

首页 / C++入门教程 / C++ Map 中的 insert函数

C++映射 insert()函数用于在映射中插入新元素

由于元素键在映射中唯一,因此插入操作首先检查给定键是否已存在于映射中,如果键已存在于映射中,则不会将其插入映射中并返回现有键的迭代器,否则将新元素插入到映射中。

insert - 语法

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

insert - 参数

val            -  要插入映射的键值。

position -  提示插入元素的位置。

first        - 要插入的参数的开始。

last         - 要插入的参数的末尾。

il              - 初始化列表。

insert - 返回值

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

insert - 例子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

在上面的示例中,它只是插入具有给定键值对的元素。

insert - 例子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}中插入元素。

insert - 例子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映射的内容。

insert - 例子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()函数将元素插入到映射中。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

零基础学Java -〔臧萌〕

后端技术面试 38 讲 -〔李智慧〕

摄影入门课 -〔小麥〕

WebAssembly入门课 -〔于航〕

零基础入门Spark -〔吴磊〕

快手 · 音视频技术入门课 -〔刘歧〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

AI大模型企业应用实战 -〔蔡超〕

好记忆不如烂笔头。留下您的足迹吧 :)