C++ Vector STL lower_bound 메서드 사용 프로그램

C++Beginner
지금 연습하기

소개

이 랩에서는 C++ 의 Vector STL 에서 lower_bound() 메서드를 사용하는 방법을 배웁니다. 이 메서드의 실질적인 구현은 랩에서 보여질 것입니다.

벡터 생성 및 정수로 채우기

벡터란 무엇인가요?

벡터는 요소 삽입 또는 삭제 시 크기를 자동으로 조정하고 자체적으로 크기를 조절할 수 있는 동적 배열입니다. 벡터는 일반적인 정적 배열에 비해 더 많은 유연성을 제공합니다.

이 단계에서는 빈 벡터를 생성하고 일부 요소를 삽입합니다. 벡터는 동적이므로 요소를 삽입하기 전에 벡터의 크기를 지정할 필요가 없습니다.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    // create an empty vector
    vector<int> v;

    // insert elements into the vector
    v.push_back(10);
    v.push_back(12);
    v.push_back(35);
    v.push_back(65);
    v.push_back(21);
    v.push_back(90);
}

벡터 요소 정렬

이 단계에서는 <algorithm> 라이브러리의 sort() 함수를 사용하여 벡터의 요소를 오름차순으로 정렬합니다.

// sorting the vector in ascending order
sort(v.begin(), v.end());

Vector STL 에서 lower_bound() 메서드 사용

이제 lower_bound() 메서드를 사용하여 주어진 값보다 작지 않은 값을 가진 첫 번째 요소를 가리키는 반복자 (iterator) 를 찾습니다.

// define the iterator low
vector<int>::iterator low;

// use lower_bound to find the first element that's not less than 35
low = lower_bound(v.begin(), v.end(), 35);

결과 출력

이 단계에서는 35 의 lower bound 의 인덱스와 결과에 대한 몇 가지 설명을 출력합니다.

// print the index of the lower bound of 35
cout << "\nThe index (starting from 0) of the lower_bound of 35 is: " << (low - v.begin()) << '\n';

// print some notes
cout << "\nNote that as per the definition, it also considers the number itself.\n\n";

전체 코드

다음은 main.cpp 파일의 전체 코드입니다.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    // create an empty vector
    vector<int> v;

    // insert elements into the vector
    v.push_back(10);
    v.push_back(12);
    v.push_back(35);
    v.push_back(65);
    v.push_back(21);
    v.push_back(90);

    // print the elements of the vector
    cout << "The elements of the Vector are: ";

    for (int i : v)
    {
        cout << i << " ";
    }

    // sorting the vector in ascending order
    sort(v.begin(), v.end());

    // print the sorted vector
    cout << "\n\nThe elements of the Vector after Sorting are: ";
    for (int i : v)
    {
        cout << i << " ";
    }

    // define the iterator low
    vector<int>::iterator low;

    // use lower_bound to find the first element that's not less than 35
    low = lower_bound(v.begin(), v.end(), 35);

    // print the index of the lower bound of 35
    cout << "\n\nThe index (starting from 0) of the lower_bound of 35 is: " << (low - v.begin()) << '\n';

    // print some notes
    cout << "\nNote that as per the definition, it also considers the number itself.\n\n";
    return 0;
}

프로그램 실행

프로그램을 실행하려면 터미널 창을 열고 main.cpp 파일이 있는 디렉토리로 이동합니다. 다음 명령을 사용하여 프로그램을 컴파일하고 실행합니다.

g++ main.cpp -o main && ./main

이렇게 하면 프로그램이 컴파일되고 실행되며, 다음과 같은 출력을 볼 수 있습니다.

The elements of the Vector are: 10 12 35 65 21 90

The elements of the Vector after Sorting are: 10 12 21 35 65 90

The index (starting from 0) of the lower_bound of 35 is: 3

Note that as per the definition, it also considers the number itself.

요약

축하합니다! 이 랩에서는 C++ 의 Vector STL 에서 lower_bound() 메서드에 대해 배우고, 벡터를 사용한 구현을 살펴보았습니다. 이러한 기술을 통해 벡터에서 요소를 생성, 정렬 및 정밀하게 검색할 수 있습니다. 수고하셨습니다!