C++Listの使い方
34501 ワード
1 Lists . (vectors) , , .
2
3 assign() list
4 back()
5 begin()
6 clear()
7 empty() list true
8 end()
9 erase()
10 front()
11 get_allocator() list
12 insert() list
13 max_size() list
14 merge() list
15 pop_back()
16 pop_front()
17 push_back() list
18 push_front() list
19 rbegin()
20 remove() list
21 remove_if()
22 rend() list
23 resize() list
24 reverse() list
25 size() list
26 sort() list
27 splice() list
28 swap() list
29 unique() list
30
31 :
32 [cpp] view plain copy
33 #include
34 #include
35 #include
36 #include
37 using namespace std;
38
39 // list LISTINT
40 typedef list<int> LISTINT;
41 // list LISTCHAR
42 typedef list<int> LISTCHAR;
43
44 void main()
45 {
46 // list
47 // LISTINT listOne list
48 LISTINT listOne;
49 // i
50 LISTINT::iterator i;
51
52 // listOne
53 listOne.push_front (2);
54 listOne.push_front (1);
55
56 // listOne
57 listOne.push_back (3);
58 listOne.push_back (4);
59
60 // listOne
61 cout<<"listOne.begin()--- listOne.end():"<<endl;
62 for (i = listOne.begin(); i != listOne.end(); ++i)
63 cout << *i << " ";
64 cout << endl;
65
66 // listOne
67 LISTINT::reverse_iterator ir;
68 cout<<"listOne.rbegin()---listOne.rend():"<<endl;
69 for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
70 cout << *ir << " ";
71 }
72 cout << endl;
73
74 // STL accumulate( )
75 int result = accumulate(listOne.begin(), listOne.end(),0);
76 cout<<"Sum="<endl;
77 cout<<"------------------"<<endl;
78
79 //--------------------------
80 // list
81 //--------------------------
82
83 // LISTCHAR listOne list
84 LISTCHAR listTwo;
85 // i
86 LISTCHAR::iterator j;
87
88 // listTwo
89 listTwo.push_front ('A');
90 listTwo.push_front ('B');
91
92 // listTwo
93 listTwo.push_back ('x');
94 listTwo.push_back ('y');
95
96 // listTwo
97 cout<<"listTwo.begin()---listTwo.end():"<<endl;
98 for (j = listTwo.begin(); j != listTwo.end(); ++j)
99 cout << char(*j) << " ";
100 cout << endl;
101
102 // STL max_element listTwo
103 j=max_element(listTwo.begin(),listTwo.end());
104 cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
105 }
106
107 :
108 listOne.begin()--- listOne.end():
109 1 2 3 4
110 listOne.rbegin()---listOne.rend():
111 4 3 2 1
112 Sum=10
113 ------------------
114 listTwo.begin()---listTwo.end():
115 B A x y
116 The maximum element in listTwo is: y
117 Press any key to continue
118
119 :
120 [cpp] view plain copy
121 #include
122 #include
123
124 using namespace std;
125 typedef list<int> INTLIST;
126
127 // list
128 void put_list(INTLIST list, char *name)
129 {
130 INTLIST::iterator plist;
131
132 cout << "The contents of " << name << " : ";
133 for(plist = list.begin(); plist != list.end(); plist++)
134 cout << *plist << " ";
135 cout<<endl;
136 }
137
138 // list
139 void main(void)
140 {
141 //list1
142 INTLIST list1;
143 //list2 10 6
144 INTLIST list2(10,6);
145 //list3 3 6
146 INTLIST list3(list2.begin(),--list2.end());
147
148 // i
149 INTLIST::iterator i;
150
151 // list
152 put_list(list1,"list1");
153 put_list(list2,"list2");
154 put_list(list3,"list3");
155
156 // list1
157 list1.push_back(2);
158 list1.push_back(4);
159 cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
160 put_list(list1,"list1");
161
162 // list1
163 list1.push_front(5);
164 list1.push_front(7);
165 cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
166 put_list(list1,"list1");
167
168 // list1
169 list1.insert(++list1.begin(),3,9);
170 cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;
171 put_list(list1,"list1");
172
173 //
174 cout<<"list1.front()="<endl;
175 cout<<"list1.back()="<endl;
176
177 // list1
178 list1.pop_front();
179 list1.pop_back();
180 cout<<"list1.pop_front() and list1.pop_back():"<<endl;
181 put_list(list1,"list1");
182
183 // list1 2
184 list1.erase(++list1.begin());
185 cout<<"list1.erase(++list1.begin()):"<<endl;
186 put_list(list1,"list1");
187
188 // list2
189 list2.assign(8,1);
190 cout<<"list2.assign(8,1):"<<endl;
191 put_list(list2,"list2");
192
193 //
194 cout<<"list1.max_size(): "<endl;
195 cout<<"list1.size(): "<endl;
196 cout<<"list1.empty(): "<endl;
197
198 //list
199 put_list(list1,"list1");
200 put_list(list3,"list3");
201 cout<<"list1>list3: "<list3)<<endl;
202 cout<<"list1"<endl;
203
204 // list1
205 list1.sort();
206 put_list(list1,"list1");
207
208 //
209 list1.splice(++list1.begin(), list3);
210 put_list(list1,"list1");
211 put_list(list3,"list3");
212 }
213
214 :
215 The contents of list1 :
216 The contents of list2 : 6 6 6 6 6 6 6 6 6 6
217 The contents of list3 : 6 6 6 6 6 6 6 6 6
218 list1.push_back(2) and list1.push_back(4):
219 The contents of list1 : 2 4
220 list1.push_front(5) and list1.push_front(7):
221 The contents of list1 : 7 5 2 4
222 list1.insert(list1.begin()+1,3,9):
223 The contents of list1 : 7 9 9 9 5 2 4
224 list1.front()=7
225 list1.back()=4
226 list1.pop_front() and list1.pop_back():
227 The contents of list1 : 9 9 9 5 2
228 list1.erase(++list1.begin()):
229 The contents of list1 : 9 9 5 2
230 list2.assign(8,1):
231 The contents of list2 : 1 1 1 1 1 1 1 1
232 list1.max_size(): 1073741823
233 list1.size(): 4
234 list1.empty(): 0
235 The contents of list1 : 9 9 5 2
236 The contents of list3 : 6 6 6 6 6 6 6 6 6
237 list1>list3: 1
238 list10
239 The contents of list1 : 2 5 9 9
240 The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
241 The contents of list3 :
242 Press any key to continue