STL - Container/Container - Map
Map - find()
Programmer.
2016. 2. 1. 08:00
소스)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<iostream> #include<string> #include<map> void print(std::map<int, std::string> Target_Map) { for (std::map<int, std::string>::iterator IterPos = Target_Map.begin(); IterPos != Target_Map.end(); ++IterPos) std::cout << "Key->" << IterPos->first << ", Value->" << IterPos->second << " "; std::cout << std::endl; } void print(std::pair<int, std::string> Target_Pair) { std::cout << Target_Pair.first << " " << Target_Pair.second << std::endl; } void main() { std::map<int, std::string> Map; Map[1] = "First"; Map[2] = "Second"; Map[3] = "Third"; Map[4] = "Fourth"; print(Map); if (Map.find(3) != Map.end()) print(*Map.find(3)); else std::cout << "No Data" << std::endl; print(Map); } | cs |
내용)
find(x)함수는 map에서 x와 같은 key를 가진 요소를 발견하면 그 값을 리턴합니다. 만약 존재하지 않는 경우엔 end()를 리턴합니다.
결과)