소개
이 랩에서는 STL 의 Unordered Multiset 의 작동 방식을 보여주는 C++ 프로그램을 만들 것입니다. Unordered Multiset 의 개념과 C++ 프로그래밍 언어에서의 구현에 대해 배우게 됩니다.
헤더 파일 포함
이 단계에서는 프로그램에 필요한 헤더 파일을 포함합니다. iostream 및 unordered_set 헤더 파일을 포함합니다.
#include<iostream>
#include<unordered_set>
using namespace std;
Multiset 및 Vector 를 표시하는 함수 정의
이 단계에서는 Unordered Multiset 과 Vector 의 내용을 표시하는 두 개의 함수를 정의합니다.
void showMultiset(unordered_multiset<int> s)
{
unordered_multiset<int>::iterator i;
for (i = s.begin(); i != s.end(); i++)
{
cout << *i << " ";
}
}
void showVector(vector<int> v)
{
vector<int>::iterator i;
for (i = v.begin(); i != v.end(); i++)
{
cout << *i << " ";
}
}
Unordered Multiset 선언 및 채우기
이 단계에서는 Unordered Multiset 을 선언하고 insert() 메서드를 사용하여 일부 요소로 채웁니다.
unordered_multiset<int> s;
s.insert(50);
s.insert(30);
s.insert(50);
s.insert(80);
s.insert(30);
s.insert(60);
Unordered Multiset 및 크기 표시
이 단계에서는 showMultiset() 함수와 size() 메서드를 사용하여 Unordered Multiset 과 그 크기를 표시합니다.
cout << "\n\nUnordered Multiset 의 요소 수는: " << s.size();
cout << "\n\nUnordered Multiset 의 요소는: ";
showMultiset(s);
Vector 를 사용하여 Unordered Multiset 정렬 및 표시
이 단계에서는 Unordered Multiset 의 요소를 Vector 에 복사하고 Vector 를 정렬합니다. 그런 다음 showVector() 함수를 사용하여 정렬된 Unordered Multiset 의 요소를 표시합니다.
vector<int> v(s.begin(), s.end());
sort(v.begin(), v.end());
cout << "\n\nVector 를 사용하여 정렬한 후 Unordered Multiset 의 요소는: ";
showVector(v);
코드 실행
이 단계에서는 g++ 명령을 사용하여 코드를 컴파일한 다음 ./a.out 명령을 사용하여 실행합니다.
g++ main.cpp -o main && ./main
출력 결과는 다음과 같습니다.
The number of elements in the Unordered Multiset are: 6
The elements of the Unordered Multiset are: 50 30 80 50 30 60
The elements of the Unordered Multiset after sorting using a vector are: 30 30 50 50 60 80
요약
이 랩에서는 Unordered Multiset 의 개념과 STL 라이브러리를 사용하여 C++ 에서 구현하는 방법에 대해 배웠습니다. Unordered Multiset 을 선언하고 초기화하는 방법, 내용을 표시하는 방법, 그리고 Vector 를 사용하여 정렬하는 방법을 살펴보았습니다.
이 랩이 Unordered Multiset in C++ 에 대해 배우는 데 유용했기를 바랍니다.



