STL中map的使用要点
生活随笔
收集整理的這篇文章主要介紹了
STL中map的使用要点
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們都知道, 當map不存在某key時, 如果用下標操作, 便會產(chǎn)生新key。 因此, 要特別注意
#include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, string> m; m["k1"] = "good"; if(m["k3"] == "") { cout << "no k3" << endl; // do things } else { cout << "has k3" << endl; // do things } if(m.find("k3") == m.end()) { cout << "no k3, to do things" << endl; // do things } else { cout << "has k3, to do things" << endl; // do things } return 0; } 先說說結果:
no k3
has k3, to do things
? ? ? 最好對it->second是否為empty進行判斷。 ?作為程序員, 不要依賴于未知假設。
總結
以上是生活随笔為你收集整理的STL中map的使用要点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 瀑布模型、V模型、原型模型、增量模型、螺
- 下一篇: 【pytorch目标检测】开山之作:R-