std::list



list

双方向の線形リスト. vectorと同様にシーケンスコンテナである. listではコンテナの任意位置への要素の挿入,削除を定数時間で行うことができる. これはlistがリンクリストとして実装されており,挿入・削除は単なるリンクの書き換えだけで済むためである. また,insertやeraseを行ってもイテレータは有効なままである(もちろん削除した要素のイテレータは無効になる).

インクルード

#include <list>

初期化(コンストラクタ)

explicit list(const Allocator& = Allocator());
explicit list(size_type n, const T& value = T(), const Allocator& = Allocator());
template < class InputIterator >
 list(InputIterator first, InputIterator last, const Allocator& = Allocator());
list(const list<T,Allocator>& x);

要素アクセス(front,back,begin,end,rbegin,rend)

  • front() : コンテナの最初の要素にアクセスする関数
    reference front();
    const_reference front() const;
  • back() : コンテナの最後の要素にアクセスする関数
    reference back();
    const_reference back() const;
  • begin(),end() : コンテナの最初の要素を示すイテレータ,最後の次のイテレータを返す.イテレータを用いた要素アクセスに使用
    iterator begin ();
    const_iterator begin () const;
  • rbegin(),rend() : コンテナの最後と最初の要素を示す逆イテレータ.イテレータを用いた要素逆順アクセスに使用
    reverse_iterator rend();
    const_reverse_iterator rend() const;

領域確保(resize)

  • resize(n) : コンテナのサイズをnに変更する関数
    void resize(size_type sz, T c = T());

サイズ確認(size,max_size,empty)

  • size() : コンテナのサイズを返す関数
    size_type size() const;
  • max_size() : コンテナが確保可能な最大サイズを返す関数.
    size_type max_size () const;
  • empty() : コンテナが空だったらtrueを返す関数.size == 0かどうかを確かめることと同義であるが,size()へのアクセスよりも高速.
    bool empty () const;

編集(push_back,pop_back,assign,insert,erase,clear,swap)

  • push_back(val) : コンテナの最後に要素を追加
    void push_back(const T& val);
  • push_front(val) : コンテナの最後に要素を追加
    void push_front(const T& val);
  • pop_back() : コンテナの最後の要素を削除
    void pop_back();
  • pop_front() : コンテナの最後の要素を削除
    void pop_front();
  • assign(n, val) : 値の割り当て
    template <class InputIterator>
     void assign(InputIterator first, InputIterator last);
    void assign(size_type n, const T& u);
  • insert(iter, val) : イテレータで示された位置に要素を挿入する.
    iterator insert(iterator position, const T& val);
    void insert(iterator position, size_type n, const T& val);
    template <class InputIterator>
     void insert(iterator position, InputIterator first, InputIterator last);
  • erase(iter) : イテレータで示された位置の要素を削除する.
    iterator erase(iterator position);
    iterator erase(iterator first, iterator last);
  • clear() : コンテナのクリア.全要素を削除する.
    void clear();
  • swap(v) : 2つのコンテナのスワップ.
    void swap(vector<T,Allocator>& vec);

リストの編集(merge,splice,sort,remove,remove_if,unique,reverse)

  • merge(lst) : ソート済みリストlstを,呼び出し元のソート済みリストにマージする.結果のリストはソートされており,値の大小を比較関数compで指定することもできる.また,マージされたリストlstは空になる.
    void merge(list<T,Allocator>& x);
    template <class Compare>
     void merge(list<T,Allocator>& x, Compare comp);
  • splice(iter, lst) : リストlstを位置iterに挿入する.要するにカットアンドペースト.
    void splice(iterator position, list<T,Allocator>& x);
    void splice(iterator position, list<T,Allocator>& x, iterator i);
    void splice(iterator position, list<T,Allocator>& x, iterator first, iterator last);
  • sort() : リストをソートする.比較関数compを指定することも可能.
    void sort();
    template <class Compare>
     void sort(Compare comp);
  • remove(val) : valの値を持つ要素を削除する.
    void remove(const T& value);
  • remove_if(pr) : 削除条件をprで指定する.単項述語関数prがtrueになる要素が削除される.
    template <class Predicate>
     void remove_if(Predicate pred);
  • unique() : 連続する重複要素を削除する.ソートした後,これを実行することで重複要素を完全に排除できる.
    void unique();
    template <class BinaryPredicate>
     void unique(BinaryPredicate binary_pred);
  • reverse() : リストの反転
    void reverse();
    使用例
    #include <iostream>
    #include <list>
    #include <cstdlib>
    using namespace std;
    
    // リストの内容の画面出力
    template<class T> 
    inline void output(list<T> x)
    {
    	if(x.empty()){
    		cout << "(none)";
    	}
    	else{
    		list<T>::iterator iter;
    		for(iter = x.begin(); iter != x.end(); ++iter){
    			cout << *iter << " ";
    		}
    	}
    	cout << endl;
    }
    
    // リストの初期化(乱数使用)
    template<class T> 
    inline void init(list<T> &x, T offset)
    {
    	if(!x.empty()) x.clear();
    	for(int i = 0; i < 5; ++i){
    		x.push_back(rand()%5+offset);
    	}
    	output(x);
    }
    
    bool GreaterInt(int a, int b)
    {
    	return a > b;
    }
    
    bool IsEven(int a)
    {
    	return (a%2 == 0);
    }
    
    int main(void)
    {
    	srand(1234);
    	list<int> x, y;
    
    	//
    	// リストのソート
    	init(x, 0); init(y, 3);
    
    	x.sort();
    	y.sort();
    
    	cout << "sort : ";
    	output(x);
    	cout << "sort : ";
    	output(y);
    
    	//
    	// 2つのソートされたリストのマージ
    	x.merge(y);
    
    	cout << "merge : ";
    	output(x);
    	cout << "merge : ";
    	output(y);
    	cout << endl;
    
    	//
    	// リストのソート(比較関数を用いた場合)
    	init(x, 0); init(y, 3);
    
    	x.sort(greater<int>());
    	y.sort(GreaterInt);
    
    	cout << "sort : ";
    	output(x);
    	cout << "sort : ";
    	output(y);
    
    	// 
    	// 2つのソートされたリストのマージ(比較関数を用いた場合)
    	x.merge(y, GreaterInt);
    
    	cout << "merge : ";
    	output(x);
    	cout << "merge : ";
    	output(y);
    	cout << endl;
    
    	//
    	// スプライス
    	init(x, 0); init(y, 10);
    	list<int>::iterator iter = x.begin();
    	iter++; 
    
    	x.splice(iter, y);	// yをxの2番目に挿入
    
    	cout << "splice1 : ";
    	output(x); output(y);
    	cout << endl;
    
    	// 要素指定スプライス
    	init(x, 0); init(y, 10);
    	iter = y.begin();
    	iter++; 
    
    	x.splice(x.begin(), y, iter);	// yの2番目の要素をxの最初に挿入
    
    	cout << "splice2 : ";
    	output(x); output(y);
    	cout << endl;
    
    	// 要素範囲指定スプライス
    	init(x, 0); init(y, 10);
    	iter = y.begin();
    	iter++; iter++;
    
    	x.splice(x.begin(), y, iter, y.end());	// yの3番目から最後まで要素をxの最初に挿入
    
    	cout << "splice3 : ";
    	output(x); output(y);
    
    	cout << endl;
    
    	//
    	// 要素削除
    	x.clear();
    	for(int i = 0; i < 10; ++i) x.push_back(i);
    	output(x); 
    
    	x.remove(0);			// 値が0の要素を削除
    
    	cout << "remove : ";
    	output(x);
    
    	x.remove_if(IsEven);	// 偶数を削除
    
    	cout << "remove_if : ";
    	output(x);
    	cout << endl;
    
    	//
    	// 重複削除
    	x.clear();
    	int x0[]= {1, 2, 2, 3, 4, 2, 1, 1};
    	x.assign(x0, x0+8);
    	output(x);
    
    	x.unique();	// 連続する重複要素を削除
    
    	cout << "unique : ";
    	output(x);
    
    	x.sort();
    	x.unique();	// 重複完全削除(ソートしてから重複削除を実行)
    
    	cout << "sort+unique : ";
    	output(x);
    	cout << endl;
    
    	//
    	// 反転
    	init(x, 0);
    
    	x.reverse();
    
    	cout << "reverse : ";
    	output(x);
    
    	return 0;
    }
    実行結果
    3 3 1 3 1
    5 7 6 4 7
    sort : 1 1 3 3 3
    sort : 4 5 6 7 7
    merge : 1 1 3 3 3 4 5 6 7 7
    merge : (none)
    
    0 3 1 3 3
    4 6 7 5 3
    sort : 3 3 3 1 0
    sort : 7 6 5 4 3
    merge : 7 6 5 4 3 3 3 3 1 0
    merge : (none)
    
    4 4 4 2 2
    10 10 10 13 10
    splice1 : 4 10 10 10 13 10 4 4 2 2
    (none)
    
    3 4 3 4 1
    14 11 14 10 12
    splice2 : 11 3 4 3 4 1
    14 14 10 12
    
    3 4 3 0 4
    12 13 10 11 13
    splice3 : 10 11 13 3 4 3 0 4
    12 13
    
    0 1 2 3 4 5 6 7 8 9
    remove : 1 2 3 4 5 6 7 8 9
    remove_if : 1 3 5 7 9
    
    1 2 2 3 4 2 1 1
    unique : 1 2 3 4 2 1
    sort+unique : 1 2 3 4
    
    3 4 0 2 2
    reverse : 2 2 0 4 3

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-03-08 (金) 18:06:08