STL - 31 (map -5)

Youngmin Choi·2021년 8월 9일
0

STL

목록 보기
31/34
post-thumbnail

1. 검색(find)

  • map에서 검색key값을 대상으로 한다.
    key와 같은 요소를 찾으면 그 요소의 반복자를 리턴하고,
    찾지 못한 경우에는 end()를 가리키는 반복자를 리턴한다. (기억하자!)


    원 형 :
iterator find( const Key& _Key );
const_iterator find( const Key& _Key ) const;

두 방식의 차이는 리턴된 반복자가 const냐 아니냐의 차이이다.
첫 번째 방식은 const가 아니므로 찾은 요소의 value를 변경할 수 있다.
(참고로 절대로 key는 변경할 수 없다!)
그러나 두 번째 방식value를 변경할 수 없다.


1.

// key가 10인 요소 찾기
map< int, int >::Iterator FindIter = map1.find(10);
// 찾았다면 value를 1000으로 변경
if(FindIter != map1.end())
{
	FindIter->second = 1000;
}
profile
Always, Continually, In all circumstance

0개의 댓글