Make Unreal REAL.
article thumbnail

 

입력을 처리하기 전에 중복이 존재하는지 확인해야 할 때, map<T, bool> 타입으로 bool 값을 따로 저장하지 않아도 된다.

 

set과 unordered_set은 insert() 함수의 반환 값으로 삽입된(혹은 이미 있는) 원소의 반복자와 중복을 확인한 성공 여부를 pair<set<T>::iterator, bool> 쌍으로 반환한다.

 

#include <iostream>
#include <set>

using namespace std;

void main()
{
    set<string> st;
    
    pair<set<string>::iterator, bool> reuslt = st.insert("LoL!!");
    cout << *(reuslt.first) << " " << boolalpha << reuslt.second << endl;
    
    reuslt = st.insert("LoL!!");
    cout << *(reuslt.first) << " " << boolalpha << reuslt.second << endl;
}

 

출력

LoL!! true
LoL!! false
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그