map使用不存在的下标
生活随笔
收集整理的這篇文章主要介紹了
map使用不存在的下标
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ? ? ?當用訪問map存在的下標時,大家都指的答案。當訪問不存在的下標時,又會發生什么呢?來看下
#include <iostream> #include <map> #include <string>using namespace std;int main() {map<int,string> m;m[2] = "good";cout << m[2] << endl;cout << m[3] << endl;if(m.find(3) != m.end()){cout << "has m[3]"<< endl;}map<int,int> m1;m1[2] = 10;cout << m1[2] << endl;cout << m1[3] << endl;return 0; }打印:
good?
?
has m[3]
10
0
由打印的結果可知,當訪問一個map不存在的下標時,便會產生新的key。string類型打印的值是空,int類型打印的值是0。
總結
以上是生活随笔為你收集整理的map使用不存在的下标的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git下载指定分支代码到本地
- 下一篇: uptime命令分析负载