std::set

std::set を試してみます。

 

 

 

std::set
ヘッダ <set> で定義
template<
    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>
> class set;
(1)
namespace pmr {
    template <class Key, class Compare = std::less<Key>>
    using set = std::set<Key, Compare, std::pmr::polymorphic_allocator<Key>>;
}
(2)(c++17以上)
テンプレート引数
Key - キーの型
Compare - キー比較関数
狭義弱順序を提供する Compare 型。
Allocator - メモリを確保/解放したり、そのメモリに要素を構築/破棄したりするために使用されるアロケータ。 この型は Allocator の要件を満たさなければなりません。 Allocator::value_type と Key が同じでない場合、動作は未定義です。

 

std::set は Key 型の一意なオブジェクトのソートされた集合を格納する連想コンテナです。 ソートはキー比較関数 Compare を使用して行われます。 検索、削除、挿入の操作の計算量は対数時間です。 set は通常赤黒木として実装されます。

標準ライブラリが Compare 要件を使用する場所ではすべて、一意性は同等関係を使用して決定されます。 不正確な言葉で言うと、2つのオブジェクト a と b は、どちらがどちらより小さいわけでもない (!comp(a, b) && !comp(b, a)) 場合に等しいとみなされます。

std::set は Container, AllocatorAwareContainer, AssociativeContainer, ReversibleContainer の要件を満たします。

 

 

 

1. 始めに

[概要]

適当に簡単なサンプルプログラムを作成してみます。

 

 

[環境]

コンパイラ : g++, 9.3.0
OS : Ubuntu (WSL2), 20.04

 

 

[プログラムソース "set_01.cpp"]

#include <iostream>
#include <string>
#include <set>

using namespace std;

template <class T, class U>
void output(const set<T, U>& container)
{
    cout << '{';
    for (T data: container){
        cout << data << ' ';
    }
    cout << '}' << endl;
}

int main()
{
    set<char> data_a;
    set<char> data_b = {'X', 'Y', 'Z'};
    set<char> data_c = {'B', 'A', 'C'};
    set<char, std::greater<char>> data_d = {'B', 'A', 'C'};
    set<int>  data_e = {5, 100, 1, 4, 3};
    set<int, std::greater<int>> data_f = {5, 100, 1, 4, 3};

    output(data_a);
    output(data_b);
    output(data_c);
    output(data_d);
    output(data_e);
    output(data_f);

    return EXIT_SUCCESS;
}

 

[出力]

{}
{X Y Z }
{A B C }
{C B A }
{1 3 4 5 100 }
{100 5 4 3 1 }

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

MIT License

Copyright (c) 2021 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 

参考

 


 

記載

2021-08-12 - 新規作成

 

Programming Items トップページ

プライバシーポリシー