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.

参考


記載

2020-08-13 - 新規作成