28.數值算法小結 -开发者知识库

28.數值算法小結 -开发者知识库,第1张

  1 #include <vector>
  2 #include <list>
  3 #include <algorithm>
  4 #include <iostream>
  5 using namespace std;
  6 
  7 void main()
  8 {
  9     //拷貝數據,與反向拷貝
 10     {
 11         vector<int> myint{ 1,2,3,4,5 };
 12         list<int> mylist{ 6,7,8,9,10 };
 13         //指定長度進行拷貝(注意,不會把容器長度增長)
 14         /*copy(myint.begin(), myint.end()-2, mylist.begin());*/
 15         //從尾部到頭部進行拷貝(反向拷貝)
 16         copy_backward(myint.begin(), myint.end(), mylist.end());
 17         for (auto i : mylist)
 18         {
 19             cout << i << endl;
 20         }
 21     }
 22     
 23     //交換數據
 24     {
 25         double a = 10;
 26         double b = 20;
 27         swap(a, b);
 28         cout << a << b << endl;
 29     }
 30     
 31     //交換容器
 32     {
 33         vector<int> myint{ 1,2,3,4 };
 34         vector<int> mynewint{ 6,7,8,9 };
 35         list<int> mylist{ 1,2,3,9 };
 36         swap(myint, mynewint);//交換數據
 37         //類型不一致,不允許交換數據
 38         //swap(myint,mylist);
 39     }
 40 
 41     //交換指針
 42     {
 43         int a = 5;
 44         int b = 10;
 45         //交換指針的地址
 46         iter_swap(&a, &b);
 47         cout << a << b << endl;
 48     }
 49 
 50     //交換容器中的兩個數據
 51     {
 52         vector<int> myint{ 1,2,3,4,5 };
 53 
 54         iter_swap(myint.begin(), myint.begin()   2);
 55         for (auto i : myint)
 56         {
 57             cout << i << endl;
 58         }
 59     }
 60 
 61     //交換兩個容器間的一部分
 62     {
 63         vector<int> myint1{ 1,2,3,4 };
 64         vector<int> myint2{ 6,7,8,9 };
 65         //交換部分
 66         swap_ranges(myint1.begin(), myint1.begin()   2, myint2.begin()   2);
 67 
 68         for (auto i : myint2)
 69         {
 70             cout << i << endl;
 71         }
 72     }
 73 
 74     //批量修改容器中的元素
 75     {
 76         vector<int> myint{ 1,2,3,4,5 };
 77         transform(myint.begin(), myint.end(), myint.begin(), [](int x) {return x * x; });
 78         for (auto i : myint)
 79         {
 80             cout << i << endl;
 81         }
 82     }
 83 
 84     //批量替換
 85     {
 86         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
 87         replace(myint.begin(), myint.end(), 3, 300);
 88         for (auto i : myint)
 89         {
 90             cout << i << endl;
 91         }
 92     }
 93 
 94     //根據條件全部替換
 95     {
 96         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
 97         replace_if(myint.begin(), myint.end(), [](int x)->bool {return x % 2; }, 0);
 98         for (auto i : myint)
 99         {
100             cout << i << endl;
101         }
102     }
103 
104     //批量替換到副本中
105     {
106         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
107         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
108         replace_copy(myint.begin(), myint.end(), myintlast.begin(), 3, 300);
109         for (auto i : myintlast)
110         {
111             cout << i << endl;
112         }
113     }
114 
115     //根據條件全部替換到副本中
116     {
117         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
118         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
119         replace_copy_if(myint.begin(), myint.end(), myintlast.begin(), [](int x)->bool {return x % 2; }, 0);
120         for (auto i : myintlast)
121         {
122             cout << i << endl;
123         }
124     }
125     
126     //填充一個容器
127     {
128         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
129         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
130         fill(myintlast.begin(), myintlast.end(), 199);
131 
132         fill_n(myint.begin(), 3, 100);
133         for (auto i : myintlast)
134         {
135             cout << i << endl;
136         }
137 
138         for (auto i : myint)
139         {
140             cout << i << endl;
141         }
142     }
143 
144     //刪除容器中指定的數據(不是徹底刪除,刪除后從后往前移動)
145     {
146         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
147         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
148 
149         //it保存最后一個元素的位置
150         auto it = remove(myint.begin(), myint.end(), 3);
151         //此種方式會顯示后面的空間的數據
152         /*for (auto i : myint)
153         {
154             cout << i << endl;
155         }*/
156         for (auto ib = myint.begin(); ib != it; ib  )
157         {
158             cout << *ib << endl;
159         }
160     }
161 
162     //刪除容器中滿足條件的數據(不是徹底刪除,刪除后從后往前移動)
163     {
164         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
165         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
166 
167         //it保存最后一個元素的位置
168         auto it = remove_if(myint.begin(), myint.end(), [](int x)->bool {return x % 2; });
169         //此種方式會顯示后面的空間的數據
170         /*for (auto i : myint)
171         {
172         cout << i << endl;
173         }*/
174         for (auto ib = myint.begin(); ib != it; ib  )
175         {
176             cout << *ib << endl;
177         }
178     }
179 
180     //對副本進行刪除
181     {
182         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
183         vector<int> myintlast;
184         for (int i = 0; i < 9; i  )
185         {
186             myintlast.push_back(0);
187         }
188 
189         remove_copy(myint.begin(), myint.end(), myintlast.begin(), 3);
190         for (auto i : myintlast)
191         {
192             cout << i << endl;
193         }
194         
195     }
196 
197     //拷貝的時候刪除連續重復的
198     {
199         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
200         unique(myint.begin(), myint.end());
201         for (auto i : myint)
202         {
203             cout << i << endl;
204         }
205     }
206 
207     //拷貝的時候刪除連續重復的(對副本)
208     {
209         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
210         int a[9]{ 0 };
211         unique_copy(myint.begin(), myint.end(), a);
212         for (auto i : a)
213         {
214             cout << i << endl;
215         }
216     }
217 
218     //容器反轉
219     {
220         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
221         reverse(myint.begin(), myint.end());
222         for (auto i : myint)
223         {
224             cout << i << endl;
225         }
226     }
227 
228     
229 
230     //區間交換到副本中
231     {
232         vector<int> myint{ 1,2,3,4,5,6,7,8,9 };
233         rotate(myint.begin(), myint.begin()   4, myint.end());
234         for (auto i : myint)
235         {
236             cout << i << endl;
237         }
238     }
239     
240     //區間交換
241     {
242         vector<int> myint{ 1,2,3,4,5,6,7,8,9 };
243         int a[9]{ 0 };
244         //begin取數組的頭結點
245         rotate_copy(myint.begin(), myint.begin()   4, myint.end(), begin(a));
246         for (auto i : a)
247         {
248             cout << i << endl;
249         }
250     }
251 
252     //洗牌算法
253     {
254         vector<int> myint{ 1,2,3,4,5,6,7,8,9 };
255         random_shuffle(myint.begin(), myint.end());
256         for (auto i : myint)
257         {
258             cout << i << endl;
259         }
260     }
261 
262     //根據條件進行分段(例如小的在左邊,大的在右邊)   (相對順序會發生變化)
263     {
264         vector<int> myint{ 1,9,2,8,3,7,4,6,5,10 };
265         //it的位置是分界點
266         auto it = partition(myint.begin(), myint.end(), [](int x)->int {return x <= 5 ? 1 : 0; });
267         for (auto i : myint)
268         {
269             cout << i << endl;
270         }
271     }
272 
273     //根據條件進行分段(對副本進行操作)(例如小的在一個容器,大的在一個容器)   (相對順序會發生變化)
274     {
275         vector<int> myint{ 1,9,2,8,3,7,4,6,5,10 };
276         vector<int> myintX{ 0,0,0,0,0,0,0,0,0,0 };
277         vector<int> myintY{ 0,0,0,0,0,0,0,0,0,0 };
278         
279         //兩段分別拷貝到兩個容器
280         auto it = partition_copy(myint.begin(), myint.end(),myintX.begin(),myintY.begin(), [](int x)->int {return x <= 5 ? 1 : 0; });
281         for (auto i : myintX)
282         {
283             cout << i << endl;
284         }
285 
286         for (auto i : myintY)
287         {
288             cout << i << endl;
289         }
290     }
291 
292     //根據條件進行分段(例如小的在左邊,大的在右邊)   (相對順序不發生變化)
293     {
294         vector<int> myint{ 1,9,2,8,3,7,4,6,5,10 };
295         //it的位置是分界點
296         auto it = stable_partition(myint.begin(), myint.end(), [](int x)->int {return x <= 5 ? 1 : 0; });
297         for (auto i : myint)
298         {
299             cout << i << endl;
300         }
301     }
302 
303     //填充
304     {
305         vector<int> myint(10);
306         //指定數據填充
307         generate(myint.begin(), myint.end(), []() {return 10; });
308         //隨機數填充
309         generate(myint.begin(), myint.end(), rand);
310         //指定位置填充n個
311         generate_n(myint.begin() 2, 3, rand);
312         for (auto i : myint)
313         {
314             cout << i << endl;
315         }
316 
317     }
318 
319     cin.get();
320 }

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复