关于c++:C-STL-map插入数据

一、前言

后面曾经说过,能够通过[]运算符批改或者增加键值对,在这里就不说这种用法了。

二、办法

1、insert

insert办法是专门用来向 map 容器中插入新的键值对的。这里的”插入”指的是 insert() 办法能够将新的键值对插入到 map 容器中的指定地位。如果毁坏了map容器的有序性,map容器会对新键值对的地位进行调整,也就是说,尽管insert能够将键值对插入指定的地位,然而插入之后map容器会查看插入的键值对是否合乎有序性,不合乎的话insert指定的地位就不是插入键值对真正的地位了。

1)不指定地位,直接插入

格局 阐明
pair<iterator,bool> insert (const value_type& val); 援用传递一个键值对
template <class P> pair<iterator,bool> insert (P&& val); 以右值援用的形式传递键值对

区别:传递参数的形式不同。无论是部分定义的键值对变量还是全局定义的键值对变量,都采纳一般援用传递的形式;而对于长期的键值对变量,则以右值援用的形式传参。

std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };

//格局1
std::pair<int, string> mapData = { 4, "wu" };
std::pair<std::map<int, string>::iterator, bool> ret;
ret = mapInfo.insert(mapData);
std::cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << std::endl;

//格局2
ret = mapInfo.insert({ 5,"ouyang" });
//等价于
//ret = mapInfo.insert(pair<int, string>{5, "ouyang"});
//ret = mapInfo.insert(make_pair(5, "ouyang"));
std::cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << std::endl;

//插入失败
ret = mapInfo.insert({ 1,"lu" });
std::cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << std::endl;
    
std::map<int, string>::iterator mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}
   

后果如下:

由后果可知:
①返回值是一个 pair 对象,其中 pair.first 示意一个迭代器,pair.second 为一个 bool 类型变量:
如果胜利插入 val,则该迭代器指向新插入的 val,bool 值为 true;
如果插入 val 失败,则表明以后 map 容器中存有和 val 的键雷同的键值对(用 p 示意),此时返回的迭代器指向 p,bool 值为 false。

2)指定地位插入

格局 阐明
iterator insert (const_iterator position, const value_type& val); 以一般援用的形式传递 val 参数
template <class P> iterator insert (const_iterator position, P&& val); 以右值援用的形式传递 val 键值对参数
std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };

//格局1
std::pair<int, string> mapData = { 4, "wu" };
std::map<int, string>::iterator mapIter = mapInfo.begin();
std::map<int, string>::iterator insertIter = mapInfo.insert(++mapIter, mapData);
std::cout << insertIter->first << "  " << insertIter->second << std::endl;

//格局2
insertIter = mapInfo.insert(++mapIter, std::pair<int, string>(5, "kai"));
std::cout << insertIter->first << "  " << insertIter->second << std::endl;

//插入失败
insertIter = mapInfo.insert(++mapIter, std::pair<int,string>(3,"ouyang"));
std::cout << insertIter->first << "  " << insertIter->second << std::endl;

mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}

后果如下:

由后果可知:
①和不指定地位的插入的办法的区别,这里返回的是迭代器
如果插入胜利,insert() 办法会返回一个指向 map 容器中已插入键值对的迭代器;
如果插入失败,insert() 办法同样会返回一个迭代器,该迭代器指向 map 容器中和 val 具备雷同键的那个键值对。

3)向以后 map 容器中插入其它 map 容器指定区域内的所有键值对

格局
template <class InputIterator> void insert (InputIterator first, InputIterator last);
std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };
std::map<int, string> mapInfo2;

std::map<int, string>::iterator first = ++mapInfo.begin();
std::map<int, string>::iterator last = mapInfo.end();
   
mapInfo2.insert(first, last);

std::map<int, string>::iterator mapIter = mapInfo2.begin();
for (; mapIter != mapInfo2.end(); mapIter++)
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}
   

后果如下:

由后果可知:
①须要其余map容器的开始和完结的迭代器,组成一个指定区域

4)一次向 map 容器中插入多个键值对

表列 A
void insert ({val1, val2, …});
std::map<int, string> mapInfo;
mapInfo.insert({ {1,"test"},{2,"lin"},{3,"wei"} });

std::map<int, string>::iterator mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}

后果如下:

insert的介绍就到这里,接下来讲讲比insert高效的办法

2、emplace

| 表列 A |
| —– |
| template <class… Args> pair<iterator,bool> emplace (Args&&… args); |

std::map<int, string> mapInfo;
pair<map<int, string>::iterator, bool> ret = mapInfo.emplace(1, "test");
std::cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << std::endl;
    
ret = mapInfo.emplace(2, "lin");
std::cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << std::endl;

ret = mapInfo.emplace(2, "kai");
std::cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << std::endl;
std::map<int, string>::iterator mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}

后果如下:

由后果可知:
①应用emplace办法时,将创立新键值对所需的数据作为参数间接传入即可,返回值是一个pair对象,和不指定地位的insert办法的返回值一样

3、emplace_hint

格局
template <class… Args> iterator emplace_hint (const_iterator position, Args&&… args);

该办法不仅要传入创立键值对所须要的数据,还须要传入一个迭代器作为第一个参数,指明要插入的地位。

std::map<int, string> mapInfo;
map<int, string>::iterator iter = mapInfo.emplace_hint(mapInfo.begin(), 1, "test");
std::cout << iter->first << "  " << iter->second << std::endl;

iter = mapInfo.emplace_hint(mapInfo.begin(), 2, "lin");
cout << iter->first << " " << iter->second << endl;

iter = mapInfo.emplace_hint(mapInfo.begin(), 2, "kai");
cout << "insert 2 kai  finial " << iter->first << " " << iter->second << endl;
std::map<int, string>::iterator mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}

后果如下:

①返回值是一个迭代器。当胜利插入新键值对时,返回的迭代器指向新插入的键值对;反之,如果插入失败,则表明 map 容器中存有雷同键的键值对,返回的迭代器就指向这个键值对

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理