关于map的下标操作的2个例子
生活随笔
收集整理的這篇文章主要介紹了
关于map的下标操作的2个例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
int main()
{
map<string,string> m1;
//關鍵字 "str1" 并不在m1中,那么會對 m1["str1"] 執行值初始化,也就是 “”if(m1["str1"] == "")cout << "if the keyword is not in the map,then it will be initial the value." <<endl;cout << "Hello World";return 0;
}
下面再舉一個例子,來說明map的下標操作的過程,
#include <iostream> #include <map> #include <set> #include <string> using namespace std; int main() { map<int,int> m; //下面的這句會執行什么操作呢?如下解釋 //(1)先搜索m中是否存在關鍵字0,如果未找到,執行下面操作 //(2)將一個新的關鍵字-值對插入到m中,關鍵字是一個const int,保存0,值進行值 //初始化,本例中意味者0 //(3)提取新插入的元素,并將值1賦予它m[0] = 1;return 0; }?
總結
以上是生活随笔為你收集整理的关于map的下标操作的2个例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python从入门到实践 第12章 武装
- 下一篇: 问题:出现在哪个地方?关于map的搜索问