std::list

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

 

 

 

1. 始めに

[概要]

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

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

 

[環境]

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

 

[プログラムソース]

#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // Create a list containing integers
    std::list<int> l = { 7, 5, 16, 8 };
 
    // Add an integer to the front of the list
    l.push_front(25);
    // Add an integer to the back of the list
    l.push_back(13);
 
    // Insert an integer before 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // Iterate and print values of the list
    for (int n : l) {
        std::cout << n << '\n';
    }
}

 

[出力]

25
7
5
42
16
8
13

 

 

2. emplace_back (c++11)

[概要]

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

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

 

[環境]

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

 

[プログラムソース]

#include <iostream>
#include <list>
#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::list<President> elections;
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);

    std::list<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 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.

 

 

参考

 


 

変更履歴

2025-03-03 - ページデザイン更新
2020-08-13 - 新規作成

 

Programming Items トップページ

プライバシーポリシー