1.자신들의 원소를 주어진 정렬 기준에 따라 자동적으로 정렬하는 컨테이너이다.
multiset은 중복된 값을 허용하나 set은 허용하지 않는다.
모든 표준 연관 컨테이너와 마찬가지로 균형 바이너리 트리로 구성되어 있다.
2. 원소의 값을 변경하기 위해서는 예전의 원소를 제거하고, 새로운 값으로 변경된 새로운 원소를 삽입해야만 한다.
직접적인 원소 액세스를 허락하지 않는다.
3. 정렬기준을 2가지 방식으로 지정할 수 있다.
a) 템플릿의 파라미터로 넣는 방식이다.
ex) std::set<int,std::greater<int>> coll;
b) 생성자의 파라미터로 넣는 방식이다.
typedef std::set<int,std::greater<int>> intset;
intset coll;
4. 비교동작은 같은 타입의 컨테이너에 대해서만 동작된다.
std::set<float> c1;
std::set<float,std::greater<float>> c2;
if(c1 == c2) //에러
5. 빠른 검색을 위해 최적화가 되어 있다.
count(elem) //원소의 갯수를 반환
find(elem) // elem값을 가지는 첫 번째의 원소를 반환한다.존재하지 않는다면 end()를 반환한다.
lower_bound(elem) //elem값보다 크거나 같은 값을 가지는 원소의 위치를 반환한다.
upper_bound(elem) //elem값보다 큰 값을 가지는 원소의 위치를 반환한다.
equal_range(elem) //정렬된 상태를 깨트리지 않고 elem이 삽입될 수 있는 첫 번째 위치와 마지막 위치를 반환한다.
ex)
set <int> c;
c.insert(1);
c.insert(2);
c.insert(4);
c.insert(5);
c.insert(6);
cout <<"lower_bound(3) : " << *c.lower_bound(3) <<endl;
cout <<"upper_bound(3) : " << *c.upper_bound(3) <<endl;
cout <<"equal_bound(3) : " << *c.equal_bound(3).first <<" "
<< *c.equal_bound(3).second <<endl;
cout <<"lower_bound(5) : " << *c.lower_bound(5) <<endl;
cout <<"upper_bound(5) : " << *c.upper_bound(5) <<endl;
cout <<"equal_bound(5) : " << *c.equal_bound(5).first <<" "
<< *c.equal_bound(5).second <<endl;
lower_bound(3) : 4
upper_bound(3) : 4
equal_bound(3) : 4 4
lower_bound(5) : 5
upper_bound(5) : 6
equal_bound(5) : 5 6
//equal_bound()는 인자로 들어온 값이 기존의 정렬 상태를 깨트리지 않고 삽입할 수 있는 위치를 가르쳐 주는 것이다.
6. 반복자 함수
c.begin(),c.end(),c.rbegin(),c.rend()
7. 원소의 삽입과 삭제
c.insert(elem) //삽입하고 새로운 원소의 위치를 반환
c.insert(pos,elem) //elem의 복사본을 삽입한다. 새로운 원소의 위치를 반환한다.
c.insert(beg,end) //범위의 모든 원소들을 복사하여 삽입한다. 반환값은 없다.
c.erase(elem) //elem인 모든 원소들을 제거한다. 제거된 원소의 갯수를 반환한다.
c.erase(pos) //pos위치의 원소를 제거한다.
c.erase(beg,end) //범위의 모든 원소를 제거한다.
c.clear() //모든 원소 제거
삽입함수의 반환값이 set과 multiset에 따라 다르다는 점에 주의 하자
set - pair<interator,bool> insert(const value_type& elem);
iterator insert(iterator pos_hint, const value_type& elem);
multiset - iterator insert(const value_type& elem);
iterator insert(iterator pos_hint, const value_type& elem);
set의 반환 타입은 pair를 사용하여 반환한다.
a) second멤버는 삽입연산의 성공 여부를 반환한다. fail이면 first멤버는 기존에 존재하고 있었던 원소의 위치반환
b) first멤버는 새롭게 삽입된 원소의 위치를 반환한다.
1) 시퀀스 컨테이너의 경우 다음과 같은 erase()를 제공한다.
iterator erase(iterator pos);
iterator erase(iterator beg,iterator end);
2) 연관 컨테이너의 경우 다음과 같은 erase()를 제공한다.
void erase(iterator pos);
void erase(iterator beg,iterator end);
ex)
typedef set<int,greater<int>> intset;
intset coll1;
coll1.insert(4);
coll1.insert(3);
coll1.insert(5);
coll1.insert(1);
coll1.insert(6);
coll1.insert(2);
coll1.insert(5);
intset::iterator pos;
for(pos = coll1.begin();pos != coll1.end();++pos){
cout << *pos << ' ';
}
cout << endl;
pair<intset::iterator,bool> status = coll1.insert(4);
if(status.second){
성공!!
}
else {
이미 존재!!
}
set<int> coll2(coll1.begin(),coll1.end());
//모든 원소들을 출력
copy(coll2.being(),coll2.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
//처음부터 coll2.find(3)범위의 모든 원소 제거
coll2.erase(coll2.begin(),coll2.find(3));
int num = coll2.erase(5); //값이 5인 모든 원소들을 제거
//할당연산자는 원소만 할당하는 것이 아니라 정렬 기준도 할당한다는 것을 반드시 기억해야 한다.
multiset은 중복된 값을 허용하나 set은 허용하지 않는다.
모든 표준 연관 컨테이너와 마찬가지로 균형 바이너리 트리로 구성되어 있다.
2. 원소의 값을 변경하기 위해서는 예전의 원소를 제거하고, 새로운 값으로 변경된 새로운 원소를 삽입해야만 한다.
직접적인 원소 액세스를 허락하지 않는다.
3. 정렬기준을 2가지 방식으로 지정할 수 있다.
a) 템플릿의 파라미터로 넣는 방식이다.
ex) std::set<int,std::greater<int>> coll;
b) 생성자의 파라미터로 넣는 방식이다.
typedef std::set<int,std::greater<int>> intset;
intset coll;
4. 비교동작은 같은 타입의 컨테이너에 대해서만 동작된다.
std::set<float> c1;
std::set<float,std::greater<float>> c2;
if(c1 == c2) //에러
5. 빠른 검색을 위해 최적화가 되어 있다.
count(elem) //원소의 갯수를 반환
find(elem) // elem값을 가지는 첫 번째의 원소를 반환한다.존재하지 않는다면 end()를 반환한다.
lower_bound(elem) //elem값보다 크거나 같은 값을 가지는 원소의 위치를 반환한다.
upper_bound(elem) //elem값보다 큰 값을 가지는 원소의 위치를 반환한다.
equal_range(elem) //정렬된 상태를 깨트리지 않고 elem이 삽입될 수 있는 첫 번째 위치와 마지막 위치를 반환한다.
ex)
set <int> c;
c.insert(1);
c.insert(2);
c.insert(4);
c.insert(5);
c.insert(6);
cout <<"lower_bound(3) : " << *c.lower_bound(3) <<endl;
cout <<"upper_bound(3) : " << *c.upper_bound(3) <<endl;
cout <<"equal_bound(3) : " << *c.equal_bound(3).first <<" "
<< *c.equal_bound(3).second <<endl;
cout <<"lower_bound(5) : " << *c.lower_bound(5) <<endl;
cout <<"upper_bound(5) : " << *c.upper_bound(5) <<endl;
cout <<"equal_bound(5) : " << *c.equal_bound(5).first <<" "
<< *c.equal_bound(5).second <<endl;
lower_bound(3) : 4
upper_bound(3) : 4
equal_bound(3) : 4 4
lower_bound(5) : 5
upper_bound(5) : 6
equal_bound(5) : 5 6
//equal_bound()는 인자로 들어온 값이 기존의 정렬 상태를 깨트리지 않고 삽입할 수 있는 위치를 가르쳐 주는 것이다.
6. 반복자 함수
c.begin(),c.end(),c.rbegin(),c.rend()
7. 원소의 삽입과 삭제
c.insert(elem) //삽입하고 새로운 원소의 위치를 반환
c.insert(pos,elem) //elem의 복사본을 삽입한다. 새로운 원소의 위치를 반환한다.
c.insert(beg,end) //범위의 모든 원소들을 복사하여 삽입한다. 반환값은 없다.
c.erase(elem) //elem인 모든 원소들을 제거한다. 제거된 원소의 갯수를 반환한다.
c.erase(pos) //pos위치의 원소를 제거한다.
c.erase(beg,end) //범위의 모든 원소를 제거한다.
c.clear() //모든 원소 제거
삽입함수의 반환값이 set과 multiset에 따라 다르다는 점에 주의 하자
set - pair<interator,bool> insert(const value_type& elem);
iterator insert(iterator pos_hint, const value_type& elem);
multiset - iterator insert(const value_type& elem);
iterator insert(iterator pos_hint, const value_type& elem);
set의 반환 타입은 pair를 사용하여 반환한다.
a) second멤버는 삽입연산의 성공 여부를 반환한다. fail이면 first멤버는 기존에 존재하고 있었던 원소의 위치반환
b) first멤버는 새롭게 삽입된 원소의 위치를 반환한다.
1) 시퀀스 컨테이너의 경우 다음과 같은 erase()를 제공한다.
iterator erase(iterator pos);
iterator erase(iterator beg,iterator end);
2) 연관 컨테이너의 경우 다음과 같은 erase()를 제공한다.
void erase(iterator pos);
void erase(iterator beg,iterator end);
ex)
typedef set<int,greater<int>> intset;
intset coll1;
coll1.insert(4);
coll1.insert(3);
coll1.insert(5);
coll1.insert(1);
coll1.insert(6);
coll1.insert(2);
coll1.insert(5);
intset::iterator pos;
for(pos = coll1.begin();pos != coll1.end();++pos){
cout << *pos << ' ';
}
cout << endl;
pair<intset::iterator,bool> status = coll1.insert(4);
if(status.second){
성공!!
}
else {
이미 존재!!
}
set<int> coll2(coll1.begin(),coll1.end());
//모든 원소들을 출력
copy(coll2.being(),coll2.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
//처음부터 coll2.find(3)범위의 모든 원소 제거
coll2.erase(coll2.begin(),coll2.find(3));
int num = coll2.erase(5); //값이 5인 모든 원소들을 제거
//할당연산자는 원소만 할당하는 것이 아니라 정렬 기준도 할당한다는 것을 반드시 기억해야 한다.