モーダルを閉じる工作HardwareHub ロゴ画像

工作HardwareHubは、ロボット工作や電子工作に関する情報やモノが行き交うコミュニティサイトです。さらに詳しく

利用規約プライバシーポリシー に同意したうえでログインしてください。

STL/リスト (C++をもう一度)

モーダルを閉じる

ステッカーを選択してください

お支払い手続きへ
モーダルを閉じる

お支払い内容をご確認ください

購入商品
」ステッカーの表示権
メッセージ
料金
(税込)
決済方法
GooglePayマーク
決済プラットフォーム
確認事項

利用規約をご確認のうえお支払いください

※カード情報はGoogleアカウント内に保存されます。本サイトやStripeには保存されません

※記事の執筆者は購入者のユーザー名を知ることができます

※購入後のキャンセルはできません

作成日作成日
2014/12/28
最終更新最終更新
2017/05/04
記事区分記事区分
一般公開

目次

    低レイヤーのプログラミングとOS開発が趣味。C言語を使っています。

    サンプルコード

    #include <iostream>
    #include <list>
    using namespace std;
    
    void Show(list<int>::iterator begin, list<int>::iterator end) {
        for( list<int>::iterator it = begin; it != end; ++it) {
            cout << *it << ' ' << flush;
        }
        cout << endl;
    }
    
    int main() {
        list<int> lst; // 双方向リスト。リストは配列と比較して:
        // - 要素の削除や追加のコストは低い
        // - 要素アクセスのコストは高い
        // - メモリもポインタの分だけ余分に必要
        cout << lst.size() << endl; //=> 0
    
        // 追加
        lst.push_front(1); // [1]
        lst.push_back(1); // [1,1]
    
        // 削除 (返り値なし)
        lst.pop_front(); // [1]
        lst.pop_back(); // []
    
        // すべて削除
        lst.clear();
        cout << lst.size() << endl; //=> 0
        cout << boolalpha << lst.empty() << endl; //=> true
    
        // リストを長くする (既定値の設定が可能)
        lst.resize(1, -1); // [-1]
        lst.resize(3, -2); // [-1,-2,-2]
    
        // リストを短くする
        lst.resize(2); // [-1,-2]
    
        // 要素へのアクセス
        Show(lst.begin(), lst.end()); //=> -1 -2
        cout << lst.front() << endl; //=> -1
        cout << lst.back() << endl; //=> -2
    
        // 指定位置に値を挿入
        list<int>::iterator pos = lst.begin();
        lst.insert(pos, -3);
        Show(lst.begin(), lst.end()); //=> -3 -1 -2
    
        // 指定位置の要素を削除
        cout << *pos << endl; //=> -1
        lst.erase(pos);
        Show(lst.begin(), lst.end()); //=> -3 -2
    
        // 指定範囲の要素を削除
        lst.push_back(0); //=> -3 -2 0
        lst.erase(++lst.begin(), lst.end());
        Show(lst.begin(), lst.end()); //=> -3
    
        // リストからリストへの要素の移動 (接合)
        list<int> other(5, 1); // 1 1 1 1 1
    
        // lst.splice(lst.begin(), other); // 指定位置にすべて移動
        // Show(lst.begin(), lst.end()); //=> 1 1 1 1 1 -3
        // Show(other.begin(), other.end()); //=> (空)
    
        // lst.splice(lst.begin(), other, other.begin()); // 指定位置に指定位置の要素を移動
        // Show(lst.begin(), lst.end()); //=> 1 -3
        // Show(other.begin(), other.end()); //=> 1 1 1 1
    
        lst.splice(lst.begin(), other, other.begin(), other.end()); // 指定位置に指定範囲の要素を移動
        Show(lst.begin(), lst.end()); //=> 1 1 1 1 1 -3
        Show(other.begin(), other.end()); //=> (空)
    
        // 並べ替え
        lst.push_front(-2); //=> -2 1 1 1 1 1 -3
        lst.sort(); // 既定は昇順
        // lst.sort(less<int>()); // 明示的に昇順
        // lst.sort(greater<int>()); // 降順
        Show(lst.begin(), lst.end()); //=> -3 -2 1 1 1 1 1
    
        // 重複要素を削除
        lst.unique();
        Show(lst.begin(), lst.end()); //=> -3 -2 1
    
        return 0;
    }
    
    Likeボタン(off)0
    詳細設定を開く/閉じる
    アカウント プロフィール画像

    低レイヤーのプログラミングとOS開発が趣味。C言語を使っています。

    記事の執筆者にステッカーを贈る

    有益な情報に対するお礼として、またはコメント欄における質問への返答に対するお礼として、 記事の読者は、執筆者に有料のステッカーを贈ることができます。

    >>さらに詳しくステッカーを贈る
    ステッカーを贈る コンセプト画像

    Feedbacks

    Feedbacks コンセプト画像

      ログインするとコメントを投稿できます。

      ログインする

      関連記事