std::mapinsert函数详解

发布时间:2023-05-17

一、std::map和std::map::insert函数简介

std::map是C++ STL(Standard Template Library)中的一个容器类模板,它用于以键值对的方式存储数据,允许使用键来访问值,它是一个元素无序的集合类模板。而std::map::insert函数是std::map中的一个关键函数,用于向std::map容器中插入元素。

// std::map::insert函数定义
std::pair<iterator,bool> insert(const value_type& val);
std::pair<iterator,bool> insert(value_type&& val);
iterator insert(const_iterator hint, const value_type& val);
iterator insert(const_iterator hint, value_type&& val);
template <class InputIterator>
void insert(InputIterator first, InputIterator last);
void insert(std::initializer_list<value_type> il);

std::map::insert函数可接受不同类型的参数,包括一个值对、一个左值或右值,还支持插入一段范围内的值对。其中std::pair<iterator,bool> 是返回类型,iterator表示插入位置,bool表示是否插入成功。

二、std::map::insert函数的使用方法

1、通过std::make_pair插入值对

使用std::make_pair函数可以将两个值合并成一个值对,然后使用insert函数将值对插入到std::map容器中。

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
    map<int,string> mymap;
    mymap.insert(make_pair(1,"one"));
    mymap.insert(make_pair(2,"two"));
    mymap.insert(make_pair(3,"three"));
    map<int,string>::iterator it;
    for(it=mymap.begin(); it!=mymap.end(); it++)
        cout << it->first << " : " << it->second << endl;
    return 0;
}

输出结果:

1 : one
2 : two
3 : three

2、直接使用值对插入

可以直接使用std::map::insert函数将一个键值对插入std::map容器中。

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
    map<int,string> mymap;
    mymap.insert(pair<int,string>(1,"one"));
    mymap.insert(pair<int,string>(2,"two"));
    mymap.insert(pair<int,string>(3,"three"));
    map<int,string>::iterator it;
    for(it=mymap.begin(); it!=mymap.end(); it++)
        cout << it->first << " : " << it->second << endl;
    return 0;
}

输出结果:

1 : one
2 : two
3 : three

3、使用emplace插入值对

使用std::map的emplace函数可以将键和值组装成一个值对,直接插入到std::map容器中。

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
    map<int,string> mymap;
    mymap.emplace(1,"one");
    mymap.emplace(2,"two");
    mymap.emplace(3,"three");
    map<int,string>::iterator it;
    for(it=mymap.begin(); it!=mymap.end(); it++)
        cout << it->first << " : " << it->second << endl;
    return 0;
}

输出结果:

1 : one
2 : two
3 : three

4、使用insert插入多个值对

使用std::map::insert函数可以批量插入多个值对。

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
    map<int,string> mymap;
    mymap.insert({{1,"one"},{2,"two"},{3,"three"}});
    map<int,string>::iterator it;
    for(it=mymap.begin(); it!=mymap.end(); it++)
        cout << it->first << " : " << it->second << endl;
    return 0;
}

输出结果:

1 : one
2 : two
3 : three

三、std::map::insert函数的相关注意点

1、插入的键值对必须唯一

std::map::insert函数插入的键值对必须是唯一的,否则旧值将被新值替换,或者插入失败。

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
    map<int,string> mymap;
    mymap.insert({{1,"one"},{2,"two"},{3,"three"}});
    mymap.insert(make_pair(2,"new two"));
    map<int,string>::iterator it;
    for(it=mymap.begin(); it!=mymap.end(); it++)
        cout << it->first << " : " << it->second << endl;
    return 0;
}

输出结果:

1 : one
2 : new two
3 : three

2、使用std::map::insert函数时,插入操作的时间复杂度为O(log N)

由于std::map采用了红黑树(一种自平衡的二叉查找树)来存储数据,因此插入操作的时间复杂度为O(log N)。

3、使用std::map::insert插入元素时,会自动排序

由于std::map采用红黑树存储数据,它会自动对插入的元素进行排序,因此每次访问std::map时,访问顺序都是有序的。

四、总结

std::map::insert函数是std::map容器的关键函数,它可以很方便地将键值对插入到std::map容器中。使用std::make_pair、直接使用pair、emplace等方法插入值对都是可以的,同时需要注意插入的键值对必须唯一且插入操作的时间复杂度为O(log N)。