소개
이 랩에서는 C++ 프로그래밍 언어에서 정렬되지 않은 집합 (unordered set) 을 정렬하는 방법을 배우게 됩니다. 정렬되지 않은 집합의 기본 개념과 구현에 대해 배우게 됩니다.
이 랩에서는 C++ 프로그래밍 언어에서 정렬되지 않은 집합 (unordered set) 을 정렬하는 방법을 배우게 됩니다. 정렬되지 않은 집합의 기본 개념과 구현에 대해 배우게 됩니다.
시작하려면 터미널을 열고 다음 명령을 입력하여 project 디렉토리로 이동합니다.
cd project
이제 project 디렉토리에 main.cpp라는 새 파일을 생성합니다. 이 파일에는 정렬되지 않은 집합을 정렬하는 코드가 포함됩니다.
touch main.cpp
다음 코드를 복사하여 main.cpp 파일에 붙여넣습니다.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool cmp(int x, int y)
{
if (x > y)
return true;
else
return false;
}
//Function to print the elements of the unordered set using an iterator
void show(unordered_set<int> s)
{
//declaring an iterator to iterate through the unordered set
unordered_set<int>::iterator i;
for (i = s.begin(); i != s.end(); i++)
{
cout << *i << " "; //accessing the elements of the unordered set using * as i stores the address to each element
}
cout << endl;
}
int main()
{
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to demonstrate the Sorting of an Unordered Set, in CPP ===== \n\n\n\n";
cout << " *** Unordered Set automatically removes the duplicate elements and maintains a random ordering. *** \n\n";
cout << " *** This random ordering depends on the hash function that is used internally. *** \n\n";
cout << " *** Unordered set can be sorted by copying its elements to a Vector. *** \n\n";
//Unordered Set declaration (Unordered Set of integers)
unordered_set<int> s;
//Filling the elements by using the insert() method.
cout << "\n\nFilling the Unordered Set with integers in random order."; //Unlike Set, this is not automatically sorted
s.insert(5);
s.insert(39);
s.insert(64);
s.insert(82);
s.insert(35);
s.insert(54);
cout << "\n\nThe elements of the Unordered Set before sorting are: ";
show(s);
//Declaring a vector and initializing it with the elements of the unordered set
vector<int> v(s.begin(), s.end());
//Sorting the vector elements in descending order using a custom comparator
sort(v.begin(), v.end(), cmp);
cout << "\n\nThe elements of the Unordered Set after sorting in descending Order using a Custom sort method are: \n";
//declaring an iterator to iterate through the unordered set
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++)
{
cout << *it << " "; //accessing the elements of the vector using * as i stores the address to each element
}
cout << "\n\n\n";
return 0;
}
프로그램을 컴파일하고 실행 파일을 생성하려면 터미널에서 다음 명령을 실행합니다.
g++ main.cpp -o main
이 명령은 main이라는 실행 파일을 생성합니다.
코드를 실행하려면 다음 명령을 실행합니다.
./main
터미널에서 프로그램의 출력을 확인할 수 있습니다.
최종 코드를 ~/project/main.cpp 파일에 복사합니다.
// ~/project/main.cpp
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool cmp(int x, int y)
{
if (x > y)
return true;
else
return false;
}
//Function to print the elements of the unordered set using an iterator
void show(unordered_set<int> s)
{
//declaring an iterator to iterate through the unordered set
unordered_set<int>::iterator i;
for (i = s.begin(); i != s.end(); i++)
{
cout << *i << " "; //accessing the elements of the unordered set using * as i stores the address to each element
}
cout << endl;
}
int main()
{
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to demonstrate the Sorting of an Unordered Set, in CPP ===== \n\n\n\n";
cout << " *** Unordered Set automatically removes the duplicate elements and maintains a random ordering. *** \n\n";
cout << " *** This random ordering depends on the hash function that is used internally. *** \n\n";
cout << " *** Unordered set can be sorted by copying its elements to a Vector. *** \n\n";
//Unordered Set declaration (Unordered Set of integers)
unordered_set<int> s;
//Filling the elements by using the insert() method.
cout << "\n\nFilling the Unordered Set with integers in random order."; //Unlike Set, this is not automatically sorted
s.insert(5);
s.insert(39);
s.insert(64);
s.insert(82);
s.insert(35);
s.insert(54);
cout << "\n\nThe elements of the Unordered Set before sorting are: ";
show(s);
//Declaring a vector and initializing it with the elements of the unordered set
vector<int> v(s.begin(), s.end());
//Sorting the vector elements in descending order using a custom comparator
sort(v.begin(), v.end(), cmp);
cout << "\n\nThe elements of the Unordered Set after sorting in descending Order using a Custom sort method are: \n";
//declaring an iterator to iterate through the unordered set
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++)
{
cout << *it << " "; //accessing the elements of the vector using * as i stores the address to each element
}
cout << "\n\n\n";
return 0;
}
이 랩에서는 C++ 프로그래밍 언어에서 정렬되지 않은 집합 (unordered set) 을 정렬하는 방법을 배웠습니다. 정렬되지 않은 집합의 기본 개념과 벡터 및 STL sort 메서드를 사용한 구현에 대해 배웠습니다. 또한 정렬되지 않은 집합을 선언하고 요소로 채우는 방법도 배웠습니다.