日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

map以及类似指针iterator

發(fā)布時間:2025/3/21 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 map以及类似指针iterator 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. map的構造函數
Map<int, string> mapStudent;
2. 數據的插入
在構造 map容器后
第一種:用insert函數插入pair數據
#pragma warning (disable:4786) )
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce std;
Int main()
{
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->se cond<<end;
}
}
第二種:用insert函數插入value_type數據,下面舉例說明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce std;
Int main()
{
Map<int, string> mapStudent;
mapStudent.insert( map<int, string>::value_type (1, “student_one”));
mapStudent.insert( map<int, string>::value_type (2, “student_two”));
mapStudent.insert( map<int, string>::value_type (3, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->se cond<<end;
}
}
第三種:用數組方式插入數據,下面舉例說明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce std;
Int main()
{
Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
mapStudent[3] = “student_three”;
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->se cond<<end;
}
}
以上三種用法,雖然都可以實現數據的插入,但是它們是有區(qū)別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數插入數據,在數據的插入上涉及到集合的唯一性這個概念,即當 map中有這個關鍵字時,insert操作是插入數據不了的,但是用數組方式就不同了,它可以覆蓋以前該關鍵字對應的值,用程序說明
mapStudent.insert( map<int, string>::value_type (1, “student_one”));
mapStudent.insert( map<int, string>::value_type (1, “student_two”));
上面這兩條語句執(zhí)行后, map中1這個關鍵字對應的值是“student_one”,第二條語句并沒有生效,那么這就涉及到我們怎么知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程序如下
Pair< map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert( map<int, string>::value_type (1, “student_one”));
我們通過pair的第二個變量來知道是否插入成功,它的第一個變量返回的是一個 map的迭代器,如果插入成功的話Insert_Pair.se cond應該是true的,否則為false。
下面給出完成代碼,演示插入成功與否問題
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce std;
Int main()
{
Map<int, string> mapStudent;
Pair< map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
If(Insert_Pair.se cond == true)
{
Cout<<”Insert Su ccessfully”<<endl;
}
Else
{
Cout<<”Insert Failure”<<endl;
} 《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的map以及类似指针iterator的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。