c++ iterator " />

扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

鎬庝箞鐢╟++杩唬鍣╥terator閬嶅巻map

扬州沐宇科技
2023-10-14 08:32:34
c++ iterator

浣跨敤C++杩唬鍣╥terator閬嶅巻map鐨勬柟娉曞涓嬶細

  1. 浣跨敤begin()鍑芥暟鑾峰彇map鐨勮捣濮嬭凯浠e櫒銆?/p>

  2. 浣跨敤end()鍑芥暟鑾峰彇map鐨勭粨鏉熻凯浠e櫒銆?/p>

  3. 浣跨敤寰幆缁撴瀯锛堝for寰幆銆亀hile寰幆锛夊拰杩唬鍣ㄩ€愪釜閬嶅巻map涓殑鍏冪礌锛岀洿鍒拌凯浠e櫒杈惧埌缁撴潫浣嶇疆涓烘銆?/p>

  4. 鍦ㄥ惊鐜腑浣跨敤杩唬鍣ㄧ殑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

扫码添加客服微信