Boostライブラリとは:現代C++開発における重要性
Boostライブラリは、現代のC++開発において必要不可欠なオープンソースのライブラリ群です。STL(Standard Template Library)を補完し、さらに拡張する形で、高品質で再利用可能なコンポーネントを提供しています。
STLを超える強力な機能群がプロジェクトにもたらす価値
Boostライブラリが開発現場にもたらす具体的な価値は以下の点に集約されます:
- 生産性の向上
- 車輪の再発明を防ぎ、開発時間を短縮
- 高度に最適化された実装をすぐに利用可能
- 豊富なドキュメントとサンプルコードによる学習効率の向上
- コードの品質向上
// 従来のポインタ管理 MyClass* ptr = new MyClass(); // メモリリーク防止のために必要な解放処理 delete ptr; // Boostのスマートポインタを使用した安全な実装 boost::shared_ptr<MyClass> ptr(new MyClass()); // 自動的にメモリ管理が行われる
- クロスプラットフォーム対応
- Windows、Linux、macOSなど、主要なプラットフォームで動作
- プラットフォーム固有の実装を抽象化
- **最新の言語機能の先取り
- C++標準に採用される機能を先行して実装
- 将来の互換性を確保
なぜ多くの企業がBoostを採用するのか
企業がBoostライブラリを採用する主な理由は以下の通りです:
1. 品質と信頼性
- ピアレビューによる厳格な品質管理
- 広範なテストスイートによる安定性の確保
- アクティブなコミュニティによるサポート
2. コスト削減効果
- 独自実装のコストと時間の削減
- バグ修正や機能改善のコスト低減
- メンテナンスコストの削減
3. 実績のある採用実績
// 大規模プロジェクトでの採用例
// - データベース処理
boost::pool<> pool(sizeof(DatabaseConnection));
auto* conn = static_cast<DatabaseConnection*>(pool.malloc());
// - 並行処理
boost::thread_group threads;
for(int i = 0; i < 4; ++i) {
threads.create_thread(boost::bind(&WorkerFunction, i));
}
4. 将来性
- C++標準への採用実績
shared_ptrfunction- ラムダ式
- 継続的な機能追加と改善
多くのエンタープライズシステムやミッションクリティカルな製品で採用されている実績は、Boostライブラリの信頼性と有用性を証明しています。例えば、金融系システムでの高頻度取引、組み込みシステムでのリアルタイム制御、ゲームエンジンでの物理演算など、様々な分野で活用されています。
Boostライブラリのセットアップと基本
環境構築で失敗しない導入手順
1. 事前準備
開発環境に応じて、以下のツールを用意します:
- C++コンパイラ(GCC, Clang, MSVCなど)
- CMakeもしくはその他のビルドシステム
- Git(ソース取得用)
2. Boostのインストール方法(OS別)
Windows環境の場合:
# 1. vcpkgを使用する場合(推奨)
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat
vcpkg install boost:x64-windows
# 2. インストーラを使用する場合
# boost公式サイトからインストーラをダウンロード
# boost_{version}-msvc-{version}.exe を実行
Linux環境の場合:
# Ubuntu/Debian sudo apt-get update sudo apt-get install libboost-all-dev # CentOS/RHEL sudo yum install boost-devel # ソースからビルドする場合 wget https://boostorg.jfrog.io/artifactory/main/release/1.81.0/source/boost_1_81_0.tar.gz tar -xzf boost_1_81_0.tar.gz cd boost_1_81_0 ./bootstrap.sh ./b2 install
macOS環境の場合:
# Homebrewを使用する場合 brew install boost # MacPorts使用する場合 sudo port install boost
3. インストール確認
#include <boost/version.hpp>
#include <iostream>
int main() {
std::cout << "Boost version: "
<< BOOST_VERSION / 100000 << "."
<< BOOST_VERSION / 100 % 1000 << "."
<< BOOST_VERSION % 100 << std::endl;
return 0;
}
プロジェクトで使用する際の設定のベストプラクティス
1. CMakeでの設定例
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)
# Boostの検索
find_package(Boost 1.81.0 REQUIRED COMPONENTS
system
filesystem
thread
)
# 実行ファイルの設定
add_executable(myapp main.cpp)
# インクルードディレクトリとライブラリのリンク
target_include_directories(myapp PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(myapp PRIVATE ${Boost_LIBRARIES})
2. プロジェクト構成のベストプラクティス
推奨ディレクトリ構造:
project/ ├── CMakeLists.txt ├── src/ │ ├── main.cpp │ └── other_source_files.cpp ├── include/ │ └── header_files.hpp ├── libs/ │ └── boost/ # 必要な場合 ├── tests/ └── build/
3. コンパイラフラグの最適化
# 最適化フラグの設定
if(MSVC)
add_compile_options(/W4 /EHsc)
else()
add_compile_options(-Wall -Wextra -O2)
endif()
4. よくあるトラブルと解決策
- リンクエラーの対処
// Windows環境でのダイナミックリンク指定 #define BOOST_ALL_DYN_LINK
- バージョン互換性の確保
// 最小バージョンの確認 #if BOOST_VERSION < 107100 #error Boost version too old #endif
- マルチスレッド対応
# CMakeでのマルチスレッドサポート
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
5. パフォーマンス最適化のための設定
- ヘッダオンリーモード vs 事前ビルド
// ヘッダオンリーモードの指定 #define BOOST_DATE_TIME_HEADER_ONLY
- デバッグビルドの最適化
# デバッグビルドでの最適化レベル設定
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
これらの設定は、プロジェクトの規模や要件に応じて適切にカスタマイズしてください。特に大規模プロジェクトでは、ビルド時間とパフォーマンスのバランスを考慮した設定が重要です。
実践的なBoostライブラリの活用実践
文字列処理を劇的に効率化するboost::string_algorithms
string_algorithmsライブラリは、文字列操作を簡潔かつ効率的に行うための多様な関数を提供します。
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
void string_processing_example() {
std::string text = " Hello, Boost World! ";
// 前後の空白を削除
boost::trim(text);
// 結果: "Hello, Boost World!"
// 大文字変換
boost::to_upper(text);
// 結果: "HELLO, BOOST WORLD!"
// 文字列分割
std::vector<std::string> tokens;
boost::split(tokens, text, boost::is_any_of(" ,"));
// 結果: ["HELLO", "", "BOOST", "WORLD!"]
// 文字列置換
boost::replace_all(text, "BOOST", "C++");
// 結果: "HELLO, C++ WORLD!"
// 部分文字列検索
bool contains = boost::contains(text, "C++");
// 結果: true
}
パフォーマンスの最適化ポイント
- 大規模テキスト処理では
boost::string_refを使用して不要なコピーを防ぐ - 複数の操作を連結する場合は中間文字列を避ける
- 正規表現が不要な場合は単純な文字列操作を優先
マルチスレッドプログラミングを簡単にするboost::thread
boost::threadは、マルチスレッドプログラミングを安全かつ効率的に実装するための機能を提供します。
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <queue>
class ThreadSafeQueue {
private:
std::queue<int> queue;
mutable boost::mutex mutex;
boost::condition_variable condition;
public:
void push(int value) {
boost::lock_guard<boost::mutex> lock(mutex);
queue.push(value);
condition.notify_one();
}
int pop() {
boost::unique_lock<boost::mutex> lock(mutex);
// データが追加されるまで待機
condition.wait(lock, [this] { return !queue.empty(); });
int value = queue.front();
queue.pop();
return value;
}
};
void thread_example() {
ThreadSafeQueue queue;
// プロデューサースレッド
boost::thread producer([&queue]() {
for (int i = 0; i < 10; ++i) {
queue.push(i);
boost::this_thread::sleep_for(
boost::chrono::milliseconds(100)
);
}
});
// コンシューマースレッド
boost::thread consumer([&queue]() {
for (int i = 0; i < 10; ++i) {
int value = queue.pop();
std::cout << "Consumed: " << value << std::endl;
}
});
producer.join();
consumer.join();
}
スレッド管理のベストプラクティス
- RAIIパターンを活用したリソース管理
- 適切なスコープでのロック管理
- デッドロック防止のための一貫したロック順序の維持
メモリ管理の悩みを解決するsmart_ptr
スマートポインタを使用することで、メモリリークを防ぎつつ、効率的なリソース管理が可能になります。
#include <boost/smart_ptr.hpp>
#include <boost/make_shared.hpp>
class Resource {
public:
Resource(const std::string& name) : name_(name) {
std::cout << "Resource " << name_ << " created" << std::endl;
}
~Resource() {
std::cout << "Resource " << name_ << " destroyed" << std::endl;
}
private:
std::string name_;
};
void smart_pointer_example() {
// 共有所有権のポインタ
boost::shared_ptr<Resource> shared =
boost::make_shared<Resource>("Shared");
{
// 参照カウントが増加
auto shared2 = shared;
std::cout << "Reference count: "
<< shared.use_count() << std::endl;
} // shared2のスコープ終了時に参照カウント減少
// 単独所有権のポインタ
boost::scoped_ptr<Resource> scoped(
new Resource("Scoped")
);
// weak_ptrによる循環参照の防止
boost::weak_ptr<Resource> weak = shared;
if (auto locked = weak.lock()) {
std::cout << "Resource still exists" << std::endl;
}
}
メモリ管理の最適化テクニック
- 適切なスマートポインタの選択
shared_ptr: 共有所有権が必要な場合scoped_ptr: 単独所有権で十分な場合weak_ptr: 循環参照を防ぐ必要がある場合
- パフォーマンス考慮事項
make_sharedを使用して割り当てを最適化- 不必要な参照カウント操作を避ける
- メモリフラグメンテーションを考慮したアロケーション
- スレッドセーフティ
shared_ptrの参照カウントはスレッドセーフ- 複数スレッドからのアクセスには適切な同期が必要
これらの実践的な例を基に、プロジェクトの要件に応じて適切な機能を選択し、効率的な実装を行うことが可能です。
パフォーマンス最適化のためのBoost活用術
メモリ使用量を削減する手法
1. プールアロケータの活用
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
class SmallObject {
int data[10];
public:
SmallObject(int value) {
std::fill_n(data, 10, value);
}
};
void pool_allocation_example() {
// 固定サイズのメモリプール
boost::pool<> pool(sizeof(SmallObject));
// 大量のオブジェクト生成
std::vector<void*> objects;
for(int i = 0; i < 1000; ++i) {
objects.push_back(pool.malloc());
}
// オブジェクトプールを使用した最適化
boost::object_pool<SmallObject> obj_pool;
auto* obj = obj_pool.malloc(); // 新規割り当て
obj = obj_pool.construct(42); // 構築も同時に行う
}
2. メモリアロケーション戦略
- スタックアロケーション
#include <boost/container/small_vector.hpp>
void stack_allocation_example() {
// 小さな配列はスタックに確保
boost::container::small_vector<int, 16> vec;
for(int i = 0; i < 10; ++i) {
vec.push_back(i); // スタック上に保存
}
}
- メモリアライメント最適化
#include <boost/align.hpp>
void alignment_optimization() {
// アライメントを指定したメモリ確保
std::size_t size = 1024;
std::size_t alignment = 16;
void* ptr = boost::alignment::aligned_alloc(alignment, size);
// 使用後は必ず解放
boost::alignment::aligned_free(ptr);
}
処理速度を向上させるBoostの機能活用法
1. ロックフリーデータ構造の活用
#include <boost/lockfree/queue.hpp>
#include <boost/lockfree/stack.hpp>
void lockfree_example() {
// ロックフリーキュー
boost::lockfree::queue<int> queue(128);
// 生産者スレッド
boost::thread producer([&queue]() {
for(int i = 0; i < 100; ++i) {
queue.push(i);
}
});
// 消費者スレッド
boost::thread consumer([&queue]() {
int value;
while(queue.pop(value)) {
process(value); // 値の処理
}
});
}
2. SIMD操作の最適化
#include <boost/simd/pack.hpp>
#include <boost/simd/algorithm.hpp>
void simd_optimization() {
std::vector<float> data(1024);
// SIMDパックを使用した高速な演算
namespace bs = boost::simd;
using pack_t = bs::pack<float>;
for(std::size_t i = 0; i < data.size(); i += pack_t::static_size) {
pack_t values(&data[i]);
values = bs::sqrt(values); // SIMD演算
values.store(&data[i]);
}
}
3. メモリアクセスパターンの最適化
#include <boost/multi_array.hpp>
void memory_access_optimization() {
// 2次元配列の効率的なアクセス
boost::multi_array<double, 2> array(
boost::extents[1024][1024]
);
// キャッシュフレンドリーなアクセス
for(size_t i = 0; i < array.shape()[0]; ++i) {
for(size_t j = 0; j < array.shape()[1]; ++j) {
array[i][j] = compute_value(i, j);
}
}
}
パフォーマンス改善のためのベストプラクティス
- メモリ管理の最適化
- スモールオブジェクト最適化の活用
- メモリプールの適切なサイズ設定
- アロケーションの削減
- 並列処理の効率化
- タスクの適切な粒度設定
- スレッドプールの活用
- ロックの最小化
- プロファイリングとベンチマーク
#include <boost/timer/timer.hpp>
void performance_measurement() {
boost::timer::cpu_timer timer;
// 計測したい処理
heavy_computation();
boost::timer::cpu_times elapsed = timer.elapsed();
// 経過時間の出力(wall/user/system)
std::cout << timer.format();
}
- コンパイル時の最適化
// テンプレートメタプログラミングによる最適化
template<int N>
struct Factorial {
static constexpr int value =
N * Factorial<N-1>::value;
};
template<>
struct Factorial<0> {
static constexpr int value = 1;
};
// コンパイル時に計算される
constexpr int fact = Factorial<5>::value;
これらの最適化テクニックを適切に組み合わせることで、アプリケーションの全体的なパフォーマンスを大きく向上させることが可能です。ただし、過度な最適化は可読性や保守性を損なう可能性があるため、プロファイリング結果に基づいて必要な箇所のみを最適化することが重要です。
Boost を使用する際の注意点と対策
バージョン互換性の問題を防ぐ方法
1. バージョン依存の機能検出
#include <boost/version.hpp>
#include <boost/config.hpp>
void version_compatibility_check() {
// バージョン確認
#if BOOST_VERSION >= 107100
// Boost 1.71以降の機能を使用
use_new_feature();
#else
// 下位互換性のある実装を使用
use_compatible_implementation();
#endif
// 特定の機能の可用性確認
#ifdef BOOST_HAS_THREADS
// マルチスレッド機能を使用
#else
// シングルスレッド実装を使用
#endif
}
2. コンパイラ互換性の確保
// コンパイラ固有の設定
#ifdef BOOST_MSVC
// Visual Studio固有の設定
#pragma warning(disable: 4996)
#endif
#ifdef BOOST_GCC
// GCC固有の設定
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
3. ABI互換性の管理
// DLL境界を越える場合の注意点
#ifdef BOOST_ALL_DYN_LINK
// 動的リンク時の設定
#ifdef BOOST_EXPORTS
#define BOOST_API __declspec(dllexport)
#else
#define BOOST_API __declspec(dllimport)
#endif
#else
#define BOOST_API
#endif
class BOOST_API ExportedClass {
// DLL境界を越えて使用されるクラス
};
デバッグ時に役立つトラブルシューティング手法
1. アサーションと実行時チェック
#include <boost/assert.hpp>
#include <boost/contract.hpp>
class Container {
std::vector<int> data;
public:
void add(int value) {
// 事前条件チェック
BOOST_ASSERT_MSG(value >= 0, "Value must be non-negative");
// 不変条件チェック
boost::contract::check c = boost::contract::function()
.precondition([&] {
BOOST_ASSERT(!data.empty() || size() == 0);
})
.postcondition([&] {
BOOST_ASSERT(size() > 0);
});
data.push_back(value);
}
size_t size() const { return data.size(); }
};
2. エラーハンドリングのベストプラクティス
#include <boost/exception/all.hpp>
// カスタムエラー情報
typedef boost::error_info<struct tag_errno, int> errno_info;
typedef boost::error_info<struct tag_file, std::string> file_info;
class file_error : public boost::exception { };
void file_operation() {
try {
// ファイル操作
throw file_error()
<< errno_info(errno)
<< file_info("config.txt");
}
catch(const file_error& e) {
// 詳細なエラー情報の取得
std::cerr << "Error in file: "
<< *boost::get_error_info<file_info>(e)
<< ", errno: "
<< *boost::get_error_info<errno_info>(e)
<< std::endl;
}
}
3. メモリリーク検出
#include <boost/core/lightweight_test.hpp>
#include <boost/smart_ptr/make_shared.hpp>
void memory_leak_detection() {
// スマートポインタを使用したメモリ管理
{
auto ptr = boost::make_shared<std::vector<int>>();
BOOST_TEST(ptr.use_count() == 1);
{
auto ptr2 = ptr;
BOOST_TEST(ptr.use_count() == 2);
}
BOOST_TEST(ptr.use_count() == 1);
}
// リークチェック
BOOST_TEST_EQ(boost::detail::heap_track::get_allocation_count(), 0);
}
4. よくあるトラブルと解決策
- ビルドエラー対策
// ヘッダ順序の依存関係 #include <boost/config.hpp> // 常に最初 #include <boost/version.hpp> // 基本定義 #include <boost/system/error_code.hpp> // システム依存 // プリプロセッサマクロの衝突防止 #ifdef min #undef min // Windows.hなどとの衝突回避 #endif
- 実行時エラーの診断
#include <boost/stacktrace.hpp>
void print_stacktrace() {
// スタックトレースの出力
std::cout << boost::stacktrace::stacktrace();
}
// シグナルハンドラの設定
void signal_handler(int signum) {
print_stacktrace();
std::exit(signum);
}
- パフォーマンス問題の診断
#include <boost/timer/timer.hpp>
void performance_diagnosis() {
boost::timer::cpu_timer timer;
{
boost::timer::auto_cpu_timer measure;
// 計測したい処理
heavy_computation();
} // スコープ終了時に自動で計測結果を出力
// 詳細な時間計測結果
boost::timer::cpu_times elapsed = timer.elapsed();
std::cout << "Wall time: " << elapsed.wall / 1e9 << "s\n"
<< "User time: " << elapsed.user / 1e9 << "s\n"
<< "System time: " << elapsed.system / 1e9 << "s\n";
}
これらのデバッグとトラブルシューティング手法を適切に活用することで、Boostライブラリを使用する際の問題を効果的に防ぎ、発生した問題も迅速に解決することが可能になります。特に大規模プロジェクトでは、これらの手法を組み合わせた体系的なデバッグ戦略の構築が重要です。
実践的なコード例で学ぶBoost活用法
日付処理を効率化するboost::datetime
boost::datetimeを使用することで、複雑な日付計算や時間操作を簡単に実装できます。
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::gregorian;
using namespace boost::posix_time;
class DateTimeManager {
public:
// 営業日計算
date calculate_business_days(const date& start_date, int days) {
date current_date = start_date;
int business_days = 0;
while (business_days < days) {
current_date += days(1);
// 土日を除外
if (current_date.day_of_week() != Saturday &&
current_date.day_of_week() != Sunday) {
++business_days;
}
}
return current_date;
}
// 期間の時間差計算
time_duration calculate_working_hours(
const ptime& start_time,
const ptime& end_time
) {
// 営業時間(9:00-17:00)内の実労働時間を計算
time_duration total_duration;
ptime current = start_time;
while (current < end_time) {
ptime day_start(current.date(), hours(9));
ptime day_end(current.date(), hours(17));
if (current.time_of_day() < hours(9)) {
current = day_start;
}
if (current.time_of_day() > hours(17)) {
current += days(1);
current = ptime(current.date(), hours(9));
continue;
}
ptime next = std::min(
day_end,
end_time
);
total_duration += next - current;
current = next;
if (current.time_of_day() == hours(17)) {
current += days(1);
current = ptime(current.date(), hours(9));
}
}
return total_duration;
}
};
// 使用例
void datetime_example() {
DateTimeManager dtm;
// 営業日計算
date today(2025, Jan, 8);
date future = dtm.calculate_business_days(today, 5);
std::cout << "5営業日後: " << future << std::endl;
// 作業時間計算
ptime start(date(2025, Jan, 8), hours(10));
ptime end(date(2025, Jan, 10), hours(15));
time_duration work_hours =
dtm.calculate_working_hours(start, end);
std::cout << "実労働時間: " << work_hours << std::endl;
}
ネットワークプログラミングを簡単にするboost::asio
boost::asioを使用して、効率的な非同期ネットワーク処理を実装できます。
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <queue>
using namespace boost::asio;
class AsyncTCPServer {
private:
io_service& io_service_;
ip::tcp::acceptor acceptor_;
std::queue<std::string> message_queue_;
std::vector<ip::tcp::socket> clients_;
public:
AsyncTCPServer(io_service& io_service, int port)
: io_service_(io_service),
acceptor_(io_service,
ip::tcp::endpoint(
ip::tcp::v4(), port)) {
start_accept();
}
private:
void start_accept() {
ip::tcp::socket* new_socket =
new ip::tcp::socket(io_service_);
acceptor_.async_accept(
*new_socket,
boost::bind(
&AsyncTCPServer::handle_accept,
this,
new_socket,
placeholders::error
)
);
}
void handle_accept(ip::tcp::socket* socket,
const boost::system::error_code& error) {
if (!error) {
clients_.push_back(std::move(*socket));
delete socket;
// 新しいクライアントの受け入れを継続
start_accept();
// 既存メッセージの送信
if (!message_queue_.empty()) {
broadcast_messages();
}
}
}
void broadcast_messages() {
while (!message_queue_.empty()) {
std::string message = message_queue_.front();
message_queue_.pop();
for (auto& client : clients_) {
async_write(
client,
buffer(message),
boost::bind(
&AsyncTCPServer::handle_write,
this,
placeholders::error,
placeholders::bytes_transferred
)
);
}
}
}
void handle_write(const boost::system::error_code& error,
size_t bytes_transferred) {
if (error) {
// エラー処理
std::cerr << "Write error: "
<< error.message() << std::endl;
}
}
};
// 使用例
void network_example() {
io_service io_service;
AsyncTCPServer server(io_service, 8080);
io_service.run();
}
正規表現を強力にサポートするboost::regex
boost::regexを使用して、高度な文字列パターンマッチングと置換を実装できます。
#include <boost/regex.hpp>
#include <string>
#include <vector>
class TextProcessor {
public:
// メールアドレスの検証
bool validate_email(const std::string& email) {
boost::regex email_regex(
"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
);
return boost::regex_match(email, email_regex);
}
// 電話番号の標準化
std::string normalize_phone_number(
const std::string& phone
) {
boost::regex phone_regex(
"\\+?(\\d{1,4})?[-.\\s]?(\\d{1,4})[-.\\s]?(\\d{4})"
);
return boost::regex_replace(
phone,
phone_regex,
"+$1-$2-$3"
);
}
// HTML태그の抽出
std::vector<std::string> extract_html_tags(
const std::string& html
) {
std::vector<std::string> tags;
boost::regex tag_regex("<([a-zA-Z][a-zA-Z0-9]*)[^>]*>");
boost::sregex_iterator it(
html.begin(),
html.end(),
tag_regex
);
boost::sregex_iterator end;
while (it != end) {
tags.push_back((*it)[1]);
++it;
}
return tags;
}
// URLの検証と正規化
std::string normalize_url(const std::string& url) {
boost::regex url_regex(
"^(https?://)?([\\w\\d.-]+\\.[a-zA-Z]{2,})(:\\d+)?(/.*)?$"
);
boost::smatch matches;
if (boost::regex_match(url, matches, url_regex)) {
std::string scheme = matches[1].length() ?
matches[1] : "http://";
std::string host = matches[2];
std::string port = matches[3].length() ?
matches[3] : "";
std::string path = matches[4].length() ?
matches[4] : "/";
return scheme + host + port + path;
}
throw std::invalid_argument("Invalid URL format");
}
};
// 使用例
void regex_example() {
TextProcessor processor;
// メールアドレス検証
std::cout << "Email valid: "
<< processor.validate_email(
"user@example.com"
) << std::endl;
// 電話番号の標準化
std::cout << "Normalized phone: "
<< processor.normalize_phone_number(
"080-1234-5678"
) << std::endl;
// HTMLタグの抽出
std::string html = "<div><p>Text</p><a href='#'>Link</a></div>";
auto tags = processor.extract_html_tags(html);
std::cout << "HTML tags: ";
for (const auto& tag : tags) {
std::cout << tag << " ";
}
std::cout << std::endl;
// URL正規化
std::cout << "Normalized URL: "
<< processor.normalize_url(
"example.com/path"
) << std::endl;
}
これらの実践的なコード例は、Boostライブラリの強力な機能を実際のプロジェクトで活用する方法を示しています。各例は実用的なシナリオを想定しており、必要に応じてカスタマイズして使用できます。また、エラー処理やパフォーマンス最適化など、実運用で重要な要素も考慮されています。