std::deque

std::deque(両端キュー)を試してみます。

 

 

 

 

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

 

std::deque (double-ended deque) は先頭と末尾の両方に高速な挿入と削除を行えるインデックス付きのシーケンスコンテナです。さらに、deque の先頭または末尾への挿入および削除は残りの要素に対するポインタや参照を決して無効化しません。

std::vector と異なり、deque の要素は隣接して格納されません。一般的な実装では、個別に確保された一連の固定サイズの列を用い、deque へのインデックスアクセスが2回のポインタ逆参照で行えるように追加の維持管理を行います。それに対して vector へのインデックスアクセスはポインタ1回逆参照するだけです。

 

 

 

1. 始めに

[概要]

ja.cppreference.com に記載されているサンプルプログラムから作成してみます。

https://ja.cppreference.com/w/cpp/container/deque

 

 

[環境]

コンパイラ : Visual Studio 2019 pro., Version 16.7.0
OS: Windows10 home, Version 1909

 

[プログラムソース]

#include <iostream>
#include <deque>

int main()
{
    // Create a deque containing integers
    std::deque<int> d = { 7, 5, 16, 8 };

    // Add an integer to the beginning and end of the deque
    d.push_front(13);
    d.push_back(25);

    // Iterate and print values of deque
    for (int n : d) {
        std::cout << n << '\n';
    }
}

 

[出力]

13
7
5
16
8
25

 

 

2. emplace_back (c++11)

[概要]

emplace_back を使った例を示します。

emplace_back, emplace_front は c++11 以降で使用可能です。
push_back を使用したときに必要なコピーやムーブを回避するために、どのように emplace_back を使用すれば良いかを示します。

 

[環境]

コンパイラ : Visual Studio 2019 pro., Version 16.7.0
OS: Windows10 home, Version 1909

 

[プログラムソース]

#include <iostream>
#include <deque>
#include <string>


struct President
{
    std::string name;
    std::string country;
    int year;

    President(std::string p_name, std::string p_country, int p_year)
        : name(std::move(p_name)), country(std::move(p_country)), year(p_year)
    {
        std::cout << "I am being constructed.\n";
    }

    President(President&& other) noexcept
        : name(std::move(other.name)), country(std::move(other.country)), year(other.year)
    {
        std::cout << "I am being moved.\n";
    }

    President& operator=(const President& other) = default;
};

std::ostream& operator << (std::ostream& os, const President& president)
{
    os << president.name << ", " << president.country << ", " << president.year;
    return os;
}


int main()
{
    std::deque<President> elections;
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);

    std::deque<President> reElections;
    std::cout << "\npush_back:\n";
    reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));


    std::cout << "\nElections:" << std::endl;
    for (auto const& president : elections) {
        std::cout << president << std::endl;
    }
    std::cout << "\nRe-Elections:" << std::endl;
    for (auto const& president : reElections) {
        std::cout << president << std::endl;
    }


    std::cout << "\nContents:\n";
    for (President const& president : elections) {
        std::cout << president.name << " was elected president of "
            << president.country << " in " << president.year << ".\n";
    }

    for (President const& president : reElections) {
        std::cout << president.name << " was re-elected president of "
            << president.country << " in " << president.year << ".\n";
    }
}

 

[出力]

emplace_back:
I am being constructed.

push_back:
I am being constructed.
I am being moved.

Elections:
Nelson Mandela, South Africa, 1994

Re-Elections:
Franklin Delano Roosevelt, the USA, 1936

Contents:
Nelson Mandela was elected president of South Africa in 1994.
Franklin Delano Roosevelt was re-elected president of the USA in 1936.

 

 

ライセンス

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

The MIT License (MIT)

Copyright © 2020-2022 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.

 

 

 

参考

 


 

変更履歴

2022-08-30 - ページデザイン更新
2020-08-10 - 新規作成

 

Programming Items トップページ

プライバシーポリシー