c ++中stl_在地图中插入元素| C ++ STL

c ++中stl

Maps are associative containers. Maps store elements in form of a pair of key value and there mapped value and the key value are stored in a sorted manner. The time taken to insert an element in the map is generally O(logn). There are various ways to insert elements in a map, some methods are discussed in this article.

地图是关联容器。 映射以一对键值的形式存储元素,并且已映射的值和键值以排序的方式存储。 在地图中插入元素所需的时间通常为O(logn)。 有多种方法可以在地图中插入元素,本文讨论了一些方法。

1)使用insert()函数 (1) Using insert() function)

std::map::insert() function is built in function in C++ STL and used for inserting the elements in map. We can use this function in following ways:

std :: map :: insert()函数是C ++ STL中的内置函数,用于在map中插入元素。 我们可以通过以下方式使用此功能:

  • insert(pair): to insert a pair in map.

    insert(pair) :在地图中插入一对。

  • insert(pos, pair): to insert a pair at specified position in a map.

    insert(pos,pair) :在地图中的指定位置插入一个对。

  • insert(beg, end): it is used to copy contents of one map to other.

    insert(beg,end) :用于将一个地图的内容复制到另一个地图。

Example:

例:

#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{
    map< char, int > myMap; 
      
    // declaring iterators 
    map<char, int>::iterator it ; 
    map<char, int>::iterator it1;
 
 	// example of insert(pair)
    myMap.insert( pair<char, int>('P', 100) ); 
      
    // printing elements of map after insertion 
    cout << "The elements of map after insertion : \n"; 
      
    for (it1 = myMap.begin(); it1!=myMap.end(); ++it1) 
        cout << it1->first << "  :  " << it1->second << endl; 
      
    it = myMap.begin(); 
      
    // example of insert(pos, pair) 
    // inserting map pair by specifying position
    myMap.insert(it, pair<char, int>('Q', 101) ); 
      
    cout << endl ; 
      
    //elements in map after insertion
    cout << "The elements of map after insertion using insert(pos, pair) : \n"; 
      
    for (it1 = myMap.begin(); it1!=myMap.end(); ++it1) 
        cout << it1->first << "  :  " << it1->second << endl; 
      
    // initializing another map  
    map<char, int> myMap2; 
      
    // using insert(beg_iter, end_iter) to copy all elements 
    myMap2.insert(myMap.begin(), myMap.end()); 
      
    cout << endl ; 
      
    // printing elements of map
    cout << "The elements of new map after using insert(beg_iter, end_iter) : \n"; 
      
    for (it1 = myMap2.begin(); it1!=myMap2.end(); ++it1) 
        cout << it1->first << "  :  " << it1->second << endl; 
     
	return 0;  
} 

Output

输出量

The elements of map after insertion :
P  :  100

The elements of map after insertion using insert(pos, pair) :
P  :  100
Q  :  101

The elements of new map after using insert(beg_iter, end_iter) :
P  :  100
Q  :  101


2)使用[]运算子 (2) Using [] Operator)

Using [] operator, we can directly insert an element into a map. It returns the pointer to the newly constructed element and size of map is always increased by 1.

使用[]运算符 ,我们可以直接将元素插入地图。 它将指针返回到新构造的元素,并且地图的大小始终增加1。

Example:

例:

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main ()
{
  map<char,string> mymap;

  // inserting elements
  mymap['i']="include";
  mymap['h']="help";
  
  //printng the elements of map
  cout << "mymap['i'] is " << mymap['i'] << endl;
  cout << "mymap['h'] is " << mymap['h'] << endl;

  cout << "now mymap contains " << mymap.size() << " elements."<<endl;

  return 0;
}

Output

输出量

mymap['i'] is include
mymap['h'] is help
now mymap contains 2 elements.


翻译自: https://www.includehelp.com/stl/insert-elements-in-map.aspx

c ++中stl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值