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

📅  最后修改于: 2021-05-30 13:44:25             🧑  作者: Mango

先决条件:在STL中映射

顾名思义,Map是一个容器,用于存储键值对。 Map比其他容器具有优势,因为在“ key”定义的map搜索仅花费O(1)时间复杂度,因此使其在各种编码领域中都非常有用。本文将讨论该插入。

  1. 使用insert() :Insert函数用于在地图中插入键值对。插入后,将对元素进行重新排序,并使用键对映射进行排序。
    此函数通过3种方式实现:
    • insert(pair) :此函数将对插入地图。插入只会发生时传递的键是不是已经在集。
      它返回一个指针对。指向已经存在或新插入的对的第一个元素。返回布尔状态为“ true”或“ false”的第二个元素。
      时间复杂度:log(n),其中n是地图的大小
    • insert(hint,pair) :在此实现中,提示指针与要插入的对一起发送。提示指针的使用是为了帮助insert()知道实际的插入位置。因此,尝试减少分配对的时间。
      提示指针不会在特定位置强制插入。此函数将指针返回到插入对的位置
      时间复杂度:log(n)其中n是地图的大小,如果提示是最佳的,则O(1)
    • insert(beg_ptr,end_ptr) :需要这种类型的插入才能将其他容器对插入到map中。如果目标容器中存在重复的对,则不会将其插入。
      时间复杂度:k * log(n),其中n是地图的大小,k是否。插入的元素数
    // C++ code to demonstrate the working of insert()
      
    #include
    #include // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        // of char and int
        map< char, int > mp;
          
        // declaring iterators
        map::iterator it ;
        map::iterator it1;
        map::iterator it2;
          
        // declaring pair for return value of map containing
        // map iterator and bool
        pair ::iterator, bool> ptr;
          
        // using insert() to insert single pair
        // inserting 'a' with 20
        ptr = mp.insert( pair('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('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 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
    
  2. 使用emplace :emplace也用于将配对插入地图。此函数类似于上面讨论的“ insert()”,唯一的区别是,对的“就地”构造发生在元素插入的位置,而不是复制或电影现有对象的insert()。
    • emplace() :使用就地构造策略插入对。将地图的大小增加1.。返回一个指针对。第一个元素是迭代器,它指向插入对的位置。 2nd返回一个布尔变量,指示已经存在或新创建的对。
      时间复杂度: log(n) (n是地图的大小)
    • emplace_hint() :使用“ hint_iterator”来获得插入位置的提示,以可能减少插入插入的配对所需的时间。这不会影响插入位置。它发生在内部定义的地方。
      时间复杂度: log(n) (n是地图的大小),如果提示是最佳的,则O(1)
    // C++ code to demonstrate the working of emplace()
    // and emplace_hint()
    #include
    #include // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        map mp;
          
        // declaring iterators
        map::iterator it;
        map::iterator it1;
        map::iterator it2;
          
        // declaring pair for return value of map containing
        // map iterator and bool
        pair< map::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
    
  3. 使用运算符[] :“ []”也可以用于在map中插入元素。与上述功能类似,将指针返回到新构造的元素。区别在于此运算符始终构造一个新元素,即,即使未将值映射到键,也将调用默认构造函数,并为键分配“空”或“空”值。地图的大小总是增加1
    时间复杂度:log(n),其中n是地图的大小
    // C++ code to demonstrate the working of operator[]
      
    #include
    #include // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        map 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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”