在std::map中插入元素(插入,插入和运算符[])

2021年3月17日18:33:45 发表评论 617 次浏览

先决条件:在STL中映射

顾名思义, 地图是一个容器, 存储键值对。地图具有以下优势:在地图上搜寻, 由"键"定义仅O(1)时间复杂度, 因此使其在各种编码领域中都非常有用。本文讨论插入。

使用insert()

:插入功能用于在地图中插入键值对。插入后, 将对元素进行重新排序, 并使用键对地图进行排序。

此功能通过3种方式实现:

  • 插入(对):此函数将配对插入地图。插入仅在密钥通过时发生不是已经设定。
    它返回一个指针对。指向已经存在或新插入的对的第一个元素。返回布尔状态为" true"或" false"的第二个元素。
    时间复杂度:log(n), 其中n是地图的大小
  • 插入(提示, 一对):在此实现中, 提示指针与要插入的线对一起发送。提示指针的使用是为了帮助insert()知道实际的插入位置。因此, 尝试减少分配对的时间。
    提示指针不会在特定位置强制插入。该功能将指针返回到该位置插入对的位置。
    时间复杂度:log(n)其中n是地图的大小, 如果提示是最佳的, 则O(1)
  • 插入(beg_ptr, end_ptr):这种类型的插入是必需的插入其他容器对进入地图。如果重复对在目标容器中存在, 则不会插入。
    时间复杂度:k * log(n), 其中n是地图的大小, k为否。插入的元素数
// C++ code to demonstrate the working of insert()
  
#include<iostream>
#include<map> // for map operations
using namespace std;
  
int main()
{
     // declaring map
     // of char and int
     map< char , int > mp;
      
     // declaring iterators
     map< char , int >::iterator it ;
     map< char , int >::iterator it1;
     map< char , int >::iterator it2;
      
     // declaring pair for return value of map containing
     // map iterator and bool
     pair <map< char , int >::iterator, bool > ptr;
      
     // using insert() to insert single pair
     // inserting 'a' with 20
     ptr = mp.insert( pair< char , int >( 'a' , 20) );
      
     // checking if the key was already present or newly inserted
     if (ptr.second)
         cout << "The key was newly inserted" ;
     else 
         cout << "The key was already present" ;
      
     cout << endl ;
      
     // printing map pairs after insertion
     cout << "The map pairs after 1st insertion are : \n" ;
      
     for (it1 = mp.begin(); it1!=mp.end(); ++it1)
         cout << it1->first << "->" << it1->second << endl;
      
     it = mp.begin();
      
     // inserting map pair using hint 
     mp.insert(it, pair< char , int >( 'b' , 24) );
      
     cout << endl ;
      
     // printing map pairs after insertion
     cout << "The map pairs after 2nd insertion are : \n" ;
      
     for (it1 = mp.begin(); it1!=mp.end(); ++it1)
         cout << it1->first << "->" << it1->second << endl;
      
     // initializing another map 
     map< char , int > mp2;
      
     // using insert(beg_iter, end_iter) to copy all elements
     mp2.insert(mp.begin(), mp.end());
      
     cout << endl ;
      
     // printing new map pairs after insertion
     cout << "The new map pairs after insertion are : \n" ;
      
     for (it1 = mp2.begin(); it1!=mp2.end(); ++it1)
         cout << it1->first << "->" << it1->second << endl;
      
}

输出如下:

The key was newly inserted
The map pairs after 1st insertion are : 
a->20

The map pairs after 2nd insertion are : 
a->20
b->24

The new map pairs after insertion are : 
a->20
b->24

使用Emplace

emplace也用于将配对插入地图。此函数类似于上面讨论的" insert()", 唯一的区别是

"就地"建设

对的发生在与插入或复制现有对象的insert()相反的元素插入位置。

  • emplace():使用就地构建策略插入对。将映射的大小增加1.。返回一个指针对。第一个元素是迭代器, 它指向插入对的位置。 2nd返回一个布尔变量, 指示已经存在或新创建的对。
    时间复杂度:日志(n)(n是地图的大小)
  • emplace_hint():需要" hint_iterator"以获得插入位置的提示, 以可能减少插入已插入对的时间。这不会影响插入位置。它发生在内部定义的地方。
    时间复杂度:日志(n)(n是地图的大小), 如果提示是最佳的, 则O(1)
// C++ code to demonstrate the working of emplace()
// and emplace_hint()
#include<iostream>
#include<map> // for map operations
using namespace std;
  
int main()
{
     // declaring map
     map< char , int > mp;
      
     // declaring iterators
     map< char , int >::iterator it;
     map< char , int >::iterator it1;
     map< char , int >::iterator it2;
      
     // declaring pair for return value of map containing
     // map iterator and bool
     pair< map< char , int >::iterator, bool > ptr;
      
     // using emplace() to insert pair element
     // inserting 'a' to 24
     // no "pair" needed, in-place construction
     ptr = mp.emplace( 'a' , 24);
      
     // checking if the pair was already present or newly inserted
     // returns true. newly inserted
     if (ptr.second)
         cout << "The key was newly inserted" ;
     else 
         cout << "The key was already present" ;
      
     cout << endl;
      
     // printing map pairs after insertion
     cout << "The map pairs after 1st insertion are : \n" ;
      
     for (it1 = mp.begin(); it1!=mp.end(); ++it1)
         cout << it1->first << "->" << it1->second << endl;
      
     cout << endl ;
      
     // using emplace() to insert single pair
     // inserting a to 24 // not inserted this time
     ptr = mp.emplace( 'a' , 24);
      
     // checking if the key was already present or newly inserted
     // returns false. already inserted
     if (ptr.second)
         cout << "The key was newly inserted" ;
     else 
         cout << "The key was already present" ;
      
     cout << endl ;
      
     // printing map pairs after insertion
     cout << "The map pairs after 2nd insertion are : \n" ;
      
     for (it1 = mp.begin(); it1!=mp.end(); ++it1)
         cout << it1->first << "->" << it1->second << endl;
      
     it = mp.begin();
      
     // inserting map pair using hint
     mp.emplace_hint(it, 'b' , 20);
      
     cout << endl ;
      
     // printing map pairs after insertion
     cout << "The map pairs after 3rd insertion are : \n" ;
      
     for (it1 = mp.begin(); it1!=mp.end(); ++it1)
         cout << it1->first << "->" << it1->second << endl;
  
      
}

输出如下:

The key was newly inserted
The map pairs after 1st insertion are : 
a->24

The key was already present
The map pairs after 2nd insertion are : 
a->24

The map pairs after 3rd insertion are : 
a->24
b->20

使用运算符[]

:" []"也可用于在地图中插入元素。与上述函数类似, 将指针返回到新构造的元素。区别在于此运算符

总是构造一个新元素

即, 即使未将值映射到键, 也会调用默认构造函数, 并将默认值" null"或"空"分配给键。

尺寸

的地图是

总是增加1

.

时间复杂度:log(n), 其中n是地图的大小

// C++ code to demonstrate the working of operator[]
  
#include<iostream>
#include<map> // for map operations
using namespace std;
  
int main()
{
     // declaring map
     map< char , int > mp;
      
     // using [] to assign key to value 
     mp[ 'a' ] = 5;
     mp[ 'b' ] = 6;
     mp[ 'c' ] = 2;
      
     // printing values
     cout << "The element keys to a is : " ;
     cout << mp[ 'a' ] << endl;
      
     cout << "The element keys to b is : " ;
     cout << mp[ 'b' ] << endl;
      
     cout << "The element keys to c is : " ;
     cout << mp[ 'c' ] << endl;
      
     // default constructor is called
     // prints 0
     cout << "The element keys to d is : " ;
     cout << mp[ 'd' ] << endl;
      
      
}

输出如下:

The element keys to a is : 5
The element keys to b is : 6
The element keys to c is : 2
The element keys to d is : 0

相关文章:在Map STL C ++中搜索

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: