Cpp STL

提供:yonewiki

C++に戻る


本来の表記は「C++ STL」です。この記事に付けられた題名はテンプレート:記事名の制約から不正確なものとなっています。

※このページではC++にのみ存在する機能として、記事タイトルがC++ STLになっています。

STL

 STLはStandard Template Libraryの略です。C++のテンプレートの技術を使った標準の関数が存在しています。100近くの関数があるとされていますが、何をもってSTLとするのかの境界自体が曖昧になりつつあるのだそうです。その中で、有名?重要?そんな感じのモノを十数個だけ紹介します。掘り下げは各自で好きなようにやってもらえればと思います。紹介だけですね。STLの解説だけで本が2・3冊かけるみたいです。


1.動的な配列: std::vector

機能: 動的なサイズの配列を提供する。

ヘッダーファイル: <vector>

例:

#include <vector>
std::vector<int> myVector = {1, 2, 3, 4, 5};

 

2.連想配列: std::map

機能: キーと値のペアを持つ連想配列を提供する。

ヘッダーファイル: <map>

例:

#include <map>
std::map<std::string, int> myMap = {{"one", 1}, {"two", 2}, {"three", 3}};

 

3.セット: std::set

機能: 重複を許さない要素の集合を提供する。

ヘッダーファイル: <set>

例:

#include <set>
std::set<int> mySet = {3, 1, 4, 1, 5, 9, 2};

 

4.文字列処理: std::string

機能: 文字列の操作および処理を提供する

ヘッダーファイル: <string>

例:

#include <string>
std::string myString = "Hello, World!";

 

5.キュー: std::queue

機能: キュー(先入れ先出し)を提供する。

ヘッダーファイル: <queue>

例:

#include <queue>
std::queue<int> myQueue;

 

6.スタック: std::stack

機能: スタック(後入れ先出し)

ヘッダーファイル: <stack>

例:

#include <stack>
std::stack<int> myStack;

 

7.アルゴリズム: std::algorithm

機能: 様々なアルゴリズム(ソート、検索、変換など)を提供する。

ヘッダーファイル: <algorithm>

例:

#include <algorithm>
std::sort(myVector.begin(), myVector.end());

 

8.反復子: std::iterator

機能: イテレータを提供し、データの反復処理をサポートする。

ヘッダーファイル: <iterator>

例:

#include <iterator>
std::ostream_iterator<int> outItr(std::cout, " ");

 

9.デキュー: std::deque

機能: デキュー(両端キュー)を提供する。

ヘッダーファイル: <deque>

例:

#include <deque>
std::deque<int> myDeque = {1, 2, 3, 4, 5};

 

10.動的ビットセット: std::bitset

機能: 固定サイズのビット集合を提供する。

ヘッダーファイル: <bitset>

例:

#include <bitset>
std::bitset<8> myBitset("10101010");

 

11.優先度付きキュー: std::priority_queue

機能: 優先度付きキューを提供する

ヘッダーファイル: <queue>

例:

#include <queue>
std::priority_queue<int> myPriorityQueue;

 

12.配列: std::array

機能: 固定サイズの配列を提供する。

ヘッダーファイル: <array>

例:

#include <array>
std::array<int, 5> myArray = {1, 2, 3, 4, 5};

 

13.イテレータアダプタ: std::back_inserter

機能: 容器の末尾に新しい要素を追加するためのイテレータアダプタを提供する。

ヘッダーファイル: <iterator>

例:

#include <iterator>
std::back_insert_iterator<std::vector<int>> backInserter(myVector);

 

14.ヒープアルゴリズム: std::make_heap, std::push_heap, std::pop_heap

機能: ヒープデータ構造を操作するアルゴリズムを提供する。

ヘッダーファイル: <algorithm>

例:

#include <algorithm>
std::vector<int> myVector = {3, 1, 4, 1, 5, 9, 2};
std::make_heap(myVector.begin(), myVector.end());

 

15.文字列ストリーム: std::stringstream

機能: 文字列からデータ型への変換や、逆にデータ型から文字列への変換を行うストリームを提供する。

ヘッダーファイル: <sstream>

例:

#include <sstream>
std::stringstream myStringStream("123");
int myInt;
myStringStream >> myInt;

 

16.スマートポインタ: std::shared_ptr, std::unique_ptr

機能: メモリ管理を行うスマートポインタを提供する。

ヘッダーファイル: <memory>

例:

#include <memory>
std::shared_ptr<int> sharedPtr = std::make_shared<int>(42);

 

17.ラムダ式: [captcha](params) -> return_type { /* body */ }

機能: 無名関数を定義するための構文を提供する。

ヘッダーファイル: 不要(言語機能)

例:

auto add = [](int a, int b) -> int { return a + b; };
int result = add(3, 4);

 

C++に戻る