鎬庝箞鐢╟++杩唬鍣╥terator閬嶅巻map
浣跨敤C++杩唬鍣╥terator閬嶅巻map鐨勬柟娉曞涓嬶細
-
浣跨敤begin()鍑芥暟鑾峰彇map鐨勮捣濮嬭凯浠e櫒銆?/p>
-
浣跨敤end()鍑芥暟鑾峰彇map鐨勭粨鏉熻凯浠e櫒銆?/p>
-
浣跨敤寰幆缁撴瀯锛堝for寰幆銆亀hile寰幆锛夊拰杩唬鍣ㄩ€愪釜閬嶅巻map涓殑鍏冪礌锛岀洿鍒拌凯浠e櫒杈惧埌缁撴潫浣嶇疆涓烘銆?/p>
-
鍦ㄥ惊鐜腑浣跨敤杩唬鍣ㄧ殑first鍜宻econd鎴愬憳璁块棶map涓殑閿拰鍊笺€?/p>
浠ヤ笅鏄竴涓ず渚嬩唬鐮侊細
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
myMap[1] = "One";
myMap[2] = "Two";
myMap[3] = "Three";
// 浣跨敤杩唬鍣ㄩ亶鍘唌ap
std::map<int, std::string>::iterator it;
for (it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << "Key: " << it->first << " Value: " << it->second << std::endl;
}
return 0;
}
杩愯缁撴灉锛?/p>
Key: 1 Value: One
Key: 2 Value: Two
Key: 3 Value: Three