Vector STL の lower_bound メソッドを使用した CPP プログラム

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() メソッドを使って、与えられた値以上の値を持つ最初の要素を指すイテレータを見つけます。

// 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 の下限のインデックスと結果に関するいくつかのメモを表示します。

// 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() メソッドについて学び、ベクトルを使ったその実装を見ました。これらのスキルを使えば、ベクトル内の要素を精度よく作成、ソート、検索することができます。素晴らしい仕事です!