map下标操作
? ? ? ? ? ? ? ???強烈推薦人工智能學(xué)習(xí)網(wǎng)站??
? ? ? ? ? ? ? 之前提到過有關(guān)map下標(biāo)操作,但是今天這個更復(fù)雜一點了,寫下來學(xué)習(xí)一下。
struct Node {int a;int b; };int main() {map<int, Node> mapTest = { { 1,{ 11, 111}} , { 2, { 22, 222 }}};map<int, Node>::iterator IT;IT=mapTest.find(2);//find函數(shù)返回一個迭代器if (mapTest.end() != mapTest.find(1)){cout << IT->second.a;}cout << mapTest[2].a << endl;printf("%d\n",mapTest[1].b/mapTest[1].a);//正確用法mapTest[1].a += 4;cout << mapTest[1].a;//正確用法return 0; }打印:22
?
? ? ? ? ? ? 22
? ? ? ? ? ? 10
? ? ? ? ? ? 15
如何找出22這個位置對于的值,上述代碼中給出了2種方法。今天工作中,有人說cout<<mapTest[1].a;是錯誤用法,現(xiàn)在測試了一下,發(fā)現(xiàn)是正確的。
?
? ? ? ? ?
?
?
總結(jié)
- 上一篇: sprintf,sscanf,snpri
- 下一篇: string操作小汇总