std::list
- list
- インクルード
- 初期化(コンストラクタ)
- 要素アクセス(front,back,begin,end,rbegin,rend)
- 領域確保(resize)
- サイズ確認(size,max_size,empty)
- 編集(push_back,pop_back,assign,insert,erase,clear,swap)
- リストの編集(merge,splice,sort,remove,remove_if,unique,reverse)
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)†
領域確保(resize)†
サイズ確認(size,max_size,empty)†
編集(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();
使用例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| | #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);
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);
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);
cout << "splice1 : ";
output(x); output(y);
cout << endl;
init(x, 0); init(y, 10);
iter = y.begin();
iter++;
x.splice(x.begin(), y, iter);
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());
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);
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
|